views:

83

answers:

3

I want my navigation links to be in the middle of the parent div, vertically. How do I do that?

HTML:

<div id="nav">
    <ul>
        <li><a href="#">Link</a></li>
        <li><a href="#">Link</a></li>
        <li><a href="#">Link</a></li>
        <li><a href="#">Link</a></li>
    </ul>
</div>

CSS:

#nav {
  background: #8DC3E9;
  width: 100%;
  height: 70px;
  border-bottom: 2px solid #4C88BE;
}

#nav ul {
  margin: 0;
}

#nav ul li {
  display: inline;
  font-size: 40px;
}
A: 

I would just add a 15 pixel padding to the nav div:

#nav {
  background: #8DC3E9;
  border-bottom: 2px solid #4C88BE;
  height: 70px;
  padding-bottom: 15px;
  padding-top: 15px;
  width: 100%;
}

Also, I would highly suggest ordering your attributes alphabetically (as I've done above) so that people can more easily find the attribute for which they are looking.

Topher Fangio
+1  A: 

According to your comment to the other answer, you want them vertically aligned in the middle. Since you have a fixed height already, this should work:

#nav ul {
    margin: 0;
    vertical-align: middle;
    line-height: 70px;
}
Lance McNearney
I can hardly ever get the `vertical-align` property to work right in all (or any) browsers. Any tips on how you use it?
Topher Fangio
@Topher - It worked in Chrome, FF, and IE 8 (browsers I tested on).@Lance - Thank you. :)
Andrew
I use it all the time. It's important to realize that it's used to change the vertical alignment of the element's children and not the element you're setting it. So it works perfectly for a <ul> container to align its child <li> elements.
Lance McNearney
A: 

Use vertical-align: middle; it is browser compatible.

Toktik