tags:

views:

35

answers:

4

I have list items that displayed inline. I want to align them vertically inside the green div.

<div id="topMenu" class="topMenu">
 <ul>
   <li><a href="#">Home</a></li>
   <li><a href="#">About</a></li>
   <li><a href="#">Documents</a></li>
   <li><a href="#">Articles</a></li>
   <li><a href="#">Info</a></li>
  </ul>
</div>

.topMenu li
{
    display: inline;
    list-style-type: none;
    padding-right: 20px;
}

.topMenu a
{
    color: White;
    font-weight: bold;
    text-decoration: none;

}
.topMenu
{
    background-position: center;
    background-color: Green;

    height: 30px;
    font-family: arial, Helvetica, sans-serif;
    font-size: 0.8em;
    vertical-align: middle;
    text-align:center;

}

online demo

A: 

You can just the display of your <li> elements a bit, like this:

.topMenu li
{
    display: inline-block;
    list-style-type: none;
    padding: 6px 10px;
}

Check out an updated demo here

Alternatively, you could add the padding to the <ul> with a new rule:

.topMenu ul {
    padding-top: 6px;
}

Check out that version here

In either case you may want to remove the height from .topMenu and let the top/bottom padding determine it, so when the page scales with zoom on older browsers it still looks "right". ​

Nick Craver
is there a more general way without hardcoding the padding?
ilann
I guess I need the height because i will use background image in that div so i need specific height.
ilann
A: 

You have to go with the padding property if you want to be strict xhtml and delete vertical-align.

Furthermore it makes no sense to try to align something vertically, that is displayed inline.

Just consider: padding is the inner space between the element and the boxmodel border.

ChrisBenyamin
what do you mean no sense? I want items to appear in the center of the div.
ilann
A: 

Internet Explorer didn't support inline-block until version 8.

You might try the work-around here.

DOK
display: inline; is not supported by IE 7?
ilann
@ilann I believe IE7 supports inline but not inline-block. Here's one source http://www.quirksmode.org/css/display.html. And another http://www.webdeveloper.com/forum/showthread.php?t=192848
DOK
but i didn't use inline-block...
ilann
@ilann please see the answer from Nick Craver, which suggests using inline-block.
DOK
+1  A: 

You could add line-height:30px; to your li elements, (the same as the height of the menu bar)

Demo

Gaby