Horizontal Menus That Grow on You

In this quick tutorial, I'm going to discuss how to create a neat little menu effect. I'm dubbing it the liDock, because elements in the menu resize as the mouse moves over them, similar to the dock on OS-X, and because it's a list menu. :)

We're going to create a horizontal row of boxes containing link text. When we move the mouse over one of these boxes, it's going to get taller and the text larger and bold. I'll link to a finished example at the end of the tutorial.

Let's begin by setting up the XHTML for the menu. This is pretty straightforward and simple. We just want to make an unordered list and give it an id of "navlist".

HTML:
  1. <ul id="navlist">
  2.     <li><a href="#">One</a></li>
  3.     <li><a href="#">Two</a></li>
  4.     <li><a href="#">Three</a></li>
  5.     <li><a href="#">Four</a></li>
  6.     <li><a href="#">Five</a></li>
  7.     <li><a href="#">Six</a></li>
  8. </ul>

As I said, nothing much to see there, just your basic list. Let's move on.

Now we get to the interesting part. We need to give our list menu a little CSS love.

Let's start by applying our styles for the list container itself. All we're going to do here is tell it to display the list (and its subsequently inherited list items) as inline elements and to not show any bullets or list markers.

CSS:
  1. ul#navlist {
  2.     display: inline;
  3.     list-style: none;
  4. }

Now we need to set up the styles for the list items. We're not doing a whole lot here but setting some basic height and widths. The rest of our visual styles will actually be applied to the links inside of the list item.

CSS:
  1. ul#navlist li {
  2.     float: left;
  3.     width: 60px;
  4.     height:60px;
  5. }

As you can see, we're creating a 60×60px square and then floating all of the elements so they'll line up horizontally.

Now that we've done that, we need to style our links. This is where it gets long and complicated (well, as complicated as this tutorial gets, anyway), so we'll break it down a bit.

We're going to start by defining our width, height and padding. We want the overall width and height here to be 10px less than our container (li) width and height, because we're adding a 5px margin that gets added in. We also need to make it display as a block level element, or the height property won't be applied correctly.

CSS:
  1. ul#navlist li a {
  2.     width: 50px;
  3.     height:50px;
  4.     padding:5px;
  5.     display:block;

Now let's do some visual styling on the font. We also set the line height to the height of our element (a), so that the text is centered vertically in the box. Finally we give it a centered horizontal alignment as well.

CSS:
  1. color:#fff;
  2.     text-decoration: none;
  3.     font-size:7pt;
  4.     font-family:arial;
  5.     line-height:50px;
  6.     text-align:center;

Now let's do the borders. We want a 1px border on the left and right sides of our boxes. In this case, we'll use white, because that's our background color. We also want a 5px border on the top and bottom that will also be white. Changing the color of the horizontal borders is how we're going to get our "resize" effect (Note: the reason I'm using borders on the left and right is because that lets us have some visual space between elements without running into IE's double margin/float bug). I'm also going to set the background color for the link element.

CSS:
  1. border-right:1px solid #fff;
  2.     border-left:1px solid #fff;
  3.     border-top: 5px solid #fff;
  4.     border-bottom: 5px solid #fff;
  5.     background: #003663;
  6. }

Got all that? Ok, good. That's the hard part and it's over with. We only have one more style to create the list item link's :hover event. It's fairly straightforward. We want to change the color of the top and bottom borders so our box appears to resize, and we want to change the font size and weight so that it grows. We're also going to do a subtle change in the background color (which the borders need to match).

CSS:
  1. ul#navlist li a:hover {
  2.     border-top: 5px solid #004a80;
  3.     border-bottom: 5px solid #004a80;
  4.     background:#004a80;
  5.     font-size:9pt;
  6.     font-weight:bold;
  7. }

There you have it. Our finished menu. You view the final example and get a look at the completed code.

9 Responses to “Horizontal Menus That Grow on You”

Posted by: Dixie on February 7th, 2007 at 2:11 am

Nice Tutorial!
^-^

Posted by: web designer roxy on August 19th, 2007 at 5:04 pm

good tutorial...!!

Posted by: Law on August 26th, 2007 at 12:01 am

Not bad!!

Posted by: Lacey on September 13th, 2007 at 1:05 pm

That was fun, thank you.

Posted by: Glosyz Tutorials » Blog Archive on September 16th, 2007 at 5:47 pm

[...] Tutorial Result [...]

Posted by: Thea on November 29th, 2007 at 4:25 pm

How do you center it ? I've tried everything

Posted by: CSSHowTo.com on December 6th, 2007 at 11:05 pm

Its a little difficult to explain quickly, but I will try.

Essentially you need to put the menu inside two divs, and float the outside div 50% left and the inner div -50%. You also need to add position relative. Im not sure how you have implemented the menu so its hard to give exact advice, but hopefully the following code might help. Use firebug and have a play with changing the settings.

.outer{
position:relative;
left:50%;
float:left;
clear:both;
}
.inner{
position:relative;
left:-50%;
}

Posted by: Sandy on July 30th, 2008 at 5:21 pm

Is there a way to set up this menu so that when it's wider than the browser window it doesn't break down but instead the user has to scroll horizontally?

Posted by: khan on September 3rd, 2008 at 8:37 am

Excellent tutorial!



Make Your Mark

Allowed HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>