How to Style an Unordered List with CSS [con't]
If we use the CSS from example list one we get the following results.
The span information is displayed within each link, as you would expect. Our aim is to hide the span information and only show it on hover.
Hiding the Span Information
To hide the spans all we need to do is style them using display none:
Showing the Span Information on Hover
All that is necessary to make the span show up is to style it using display block.
It is important to note that IE6 has a
:hover bug such that it will only work if triggered by a change in properties of any of the following commonly used styles. In our case we have a change of background to trigger the
:hover pseudo class.
If you now hover over each link it will expand to show the contents of the span.
Moving the Span out of the Menu Links
This is not very user friendly as the menu items jump about and it's not very clear what is happening.
What we need is a method of placing the span information in a box of its own so that it doesn't affect the menu list. This can be done by giving the span an absolute position which will take it out of the normal page flow and allow us to position it where we like.
In order for us to control the absolute position relative to the menu we will need to give the menu a relative position:.
position:relative;- gives the #menu a relative position so that the absolute position of the span can be controlled.
position:absolute;- gives the span an absolute position.
left:10em;- sets the left position at 10em so that it will appear to the right of the menu and have a small clearance.
top:0;- sets the top of the span in line with the top of the menu.
The span information has now been moved out of the menu links and placed to the right, but it could use a bit more styling.
Setting up the Size and Color of the Span
text-align:left;- the span text is center aligned the same as the links. This will left align the text.
padding:0.5em;- adds a little padding to the span text.
width:24em;- gives the span a width of 24em.
background-color:#eee;- sets the background color to light grey.
color:#000;- makes the text color black.
border:1px solid #808;- adds a purple border to make the link bolder.
The span is now neatly styled in a light grey box with a purple border with the image under the painter's name. The text, however, starts at the bottom right of the image.
Styling the Span Image
It would be nice if the text were to start in line with the top of the image.
border:1px solid #000;- adds a 1px black border around the image.
float:left;- this will float the image to the left (it's already at the left hand side!) and will cause the text to start in line with the top of the image.
margin:0.25em 1em 0.5em 0;- adds a small margin around the image, except on the left hand side, so that the text has some clearance.
The text now starts in line with the top of the image.
Styling the Span First Line
Just one last step before we are finished. It would be nice if the first line of the span stood out a bit more.
We can use the pseudo class
:first-line to style just the painter's name and his birth/death dates:
font-weight:bold;- makes the first line text bold.
font-size:1.2em;- makes the font slightly larger.
color:#808;- changes the text color to purple.
It's done. We now have a pop up box with information about each menu item.
Re-styling the Pop-up
We don't need to stop there.
Wouldn't it be nice if the pop-up box were to track our menu items instead of staying fixed at the top of the menu?
If we had a long list of items we wouldn't want to look to the top of the list to see the information; it would be much more convenient if it were to appear alongside each item.
With a little change of style this too is possible.
The current position of the span information is determined by the relative position styling of the
#menu. If we were to give the links a relative position 'on hover' instead of the
#menu then the spans would be absolutely positioned relative to each link.
To do this, remove the position:relative; from the
#menu style and place it
#menu a, #menu a:visited style instead.
We now have a pop-up information box that tracks the menu items. The top of the pop-up box is now in line with the top of each menu item. The exact position can be controlled by changing the top and left values of the 'hover span' style.
From this tutorial you can see how easy it is to style this type of menu. I have given you the basic information which you can modify as you wish to fit in with the look of your page. The styling, as I have said in past articles, is left up to your imagination.
About the Author
Original: July 8, 2005