Creating Rollover
|
|
1. |
Create a set of links in the usual way in the (X)HTML document
(see page 103)
.
|
|
2. |
In the CSS, style the
a:link
and
a:visited
selectors with the "initial state" of the links by adding background
|
|
3. |
Modify the colors or backgrounds slightly for the
a:focus
and
a:hover
selectors so that the buttons change appearance when they get the focus or are pointed at.
|
|
4. |
If desired, select a third style for buttons when they're activated by setting the CSS for the
a:active
selector.
|
Tips
This is a very simple example. You can do wonders by designing and choosing special images. For more ideas, see the article by Douglas Bowman on Sliding Doors of CSS at A List Apart : http://www.alistapart.com/slidingdoors
I used the display:block to format the usually inline a elements as block-level elements, each beginning its own line. This is not necessary for rollover buttons; you could just as easily have them in a horizontal row.
Creating Pop-upsThere's nothing that says the second image in a rollover has to appear directly on top of the previous one. If you position the second image some distance away, you can give additional details about the hovered item. To create pop-ups:
Figure 12.9. You can see a fully commented version of this code on my Web site.
Figure 12.10. When the visitor hovers over a photo in the left column, a larger version appears in the right frame.
|
Creating Drop-Down
|
|
1. |
In the (X)HTML file
(Figure 12.11)
, create navigation links in the form of a nested list like the one on page 224. The first level items will always be visible, the second (and
Figure 12.11. Note that each of the submenus is a ul with its li elements within opening and closing tags of the top level li item. The links are empty to save space in this illustration, but clearly they'd have to have something there in the real world.
|
|
2. |
Enclose the entire list in a
div
with a
|
|
3. |
Enclose the rest of your page in its own
div
with a name like
content
.
|
|
4. |
In the CSS
(Figure 12.12)
, remove the default list formatting by using
#navbar ul {margin: 0; padding: 0; list-style: none;}
.
Figure 12.12. The basic code behind CSS drop-down menus is not complicated: remove the default formatting from the
ul
, float the list items horizontally, hide the second
|
|
5. |
|
|
6. |
To make the first level navbar items appear horizontally, type
#navbar li {float: left; width:10em;}
.
|
|
7. |
To hide the second level list except when hovered over, type
#navbar li ul {display: none}
.
|
|
8. |
To make the second level list appear when hovered over, type
#navbar li: hover ul {display: block;width:10em;
.
|
|
9. |
To keep the rest of your page from bouncing around when the second level lists appear, add
position: absolute;
} to the previous line.
|
|
10. |
To keep the rest of your page from floating next to the navbar, clear the float from the
content div
by typing
div.content {clear:left}
.
|
|
11. |
Add extra formatting to make the menus pretty
(Figure 12.15)
. You can find the CSS file used for formatting on the Web site
(see page 26)
.
|
Tips
This technique only works on browsers like Firefox and Opera that fully support CSS' :hover pseudo-class (see page 146) . Unfortunately, Internet Explorer (up to and including version 7), only supports :hover with a elements. What a waste! One way to make CSS menus work in IE is to add a bit of JavaScript. See Patrick Griffiths' Son of Suckerfish Dropdowns for one such technique (www.htmldog.com/articles/suckerfish/dropdowns/) .
You might want to set a width (see page 174) or minimum width (also on page 174) for your page so that your navigation bars don't collapse when the page is too narrow. Unfortunately, as of version 7, IE doesn't support minimum width yet either.
You can accommodate browsers like IE that don't fully support :hover by making the top level items actual links to pages that contain the second level links. While IE users won't see your drop-down menus, they'll still be able to get to the pages where they lead.
You can use this technique to create vertical pull-out menus as well.