CSS-Tricks PSD to HTML

Prevent Menu “Stepdown”

preventing-stepdown.png

If you are familiar with the concepts of “floats”, you know that if you float a page element to the left, that the next page element will move up next to that element on the right, if possible. But have you ever seen your floated elements “stepdown”?

This is a fairly common problem you can run into when creating a horizontal menu. Like most menus, you create an unordered list:

<ul id="menu">
	<li><a href="#">My</a></li>
	<li><a href="#">Little</a></li>
	<li><a href="#">Menu</a></li>
</ul>

You want the menu items to be large clickable blocks, so you write CSS like this:

ul#menu li a {
	display: block;
	width: 130px;
	text-align: center;
	font-weight: bold;
	float: left;
	color: white;
	font-size: 1.2em;
	text-decoration: none;
	background: #600;
}

Those blocks are floated the left, so they should all line up in a row, right? Nope, that’s gonna get you some stepdown action. The problem is the list elements wrapping the anchor elements. These are also block-level elements but they are not floated. This confuses things, because block elements naturally have a break after them (like an invisible <br />). That pushes the next one down whatever the current line-height is, which is what causes the stepdown.

Here is the remedy:

ul#menu li {
	display: inline; /* Prevents "stepdown" */
}

Setting those list elements as inline will take away those breaks and make sure your menu stays nice, happy, and straight!

fixed.png


Theoretically Related Articles:

Discussion Elsewhere


Responses


  1. 1

    Gravatar

    Another option would be to float the li elements instead of the links. This will also give you a little more control over the positioning/spacing of each li.

    Also, just a little thing, but your css examples above refer to different elements.


    Comment by Dan Ott — February 6, 2008 @ 4:17 pm

  2. 2

    Gravatar

    Also, the prolem is disappeare if make line-height:0 to li elements


    Comment by Alex — February 7, 2008 @ 8:54 am

  3. 3

    Gravatar

    I use to apply the float to li elements, this way if i want to have some text on my menu bar (without it being a link, think in some kind of greeting or a tagline) i just put it into a li and i’m done.


    Comment by Alma — February 7, 2008 @ 9:15 am

  4. 4

    Gravatar

    I also give the float attribute to the LI elements.
    I don’t like the concept of an inline element containing a block element.


    Comment by Yoshcat — February 11, 2008 @ 1:24 pm

  5. 5

    Gravatar

    Am i wrong or is the problem of stepping menus IE6 only?
    I never encountered this kind of behavior in any other browser i work with, tbh.
    Styling and floating li’s works properly most of the time, although in my last project i had to drop some lists and replace them with several p’s each. Just a hotfix for compatibility…


    Comment by Jan — February 18, 2008 @ 4:29 pm


Leave a comment

Sick of typing in all this info everytime you comment? Register or Login and save yourself time!

Live Comment Preview


Thank you for visiting CSS-Tricks! I'm glad you found an article useful enough to print out! Remember to visit css-tricks.com often for more fresh content.