<ul> horizontal nav bar... vertically-align element

14,588

Solution 1

Try this:

.nav li {
  display:inline;
  vertical-align:middle;
  line-height:35px;
}

Solution 2

Give each of those li a's a class middle:

<li>
    <a class="middle" href="aboutme.html" style="REDACTED">about me</a>
</li>

Define middle like:

.middle {
    line-height: 35px; /** or same as ul height */
}

Inline elements are always vertically centered within their line-height.

DEMO

Share:
14,588
01jayss
Author by

01jayss

Updated on June 04, 2022

Comments

  • 01jayss
    01jayss almost 2 years

    I currently have the following code. I am trying to get the second, third, and fourth "li" elements to vertically-align in the middle of the nav bar. The first "li" is an image, and the second-fourth are all text.

    Here is a screenshot of it currently.

    http://i49.tinypic.com/bfesl3.png

    I have tried using "vertical-align:middle" and padding. Note that the second, third and fourth "li" elements appear vertically-aligned in the middle if viewed in Firefox but not in other browsers.

    Here is the code I have.

    <ul class = "nav">
    
    <li><a href="index.html" style="border-right:1px #FFFFFF solid; padding-top:4.6px; padding-bottom:17.3px;"><img src="img/randomtitle.png" style="padding-left:8px;padding-top:8px;"/></a></li>
    <li><a href="aboutme.html" style="vertical-align:middle;padding-top:32px;margin-left:-15px;padding-bottom:14px;padding-right:20px;border-right:1px #ffffff solid;">about me</a></li>
    <li><a href="films.html" style="vertical-align:middle;padding-top:32px;margin-left:1px;padding-bottom:14px;padding-right:30.5px;border-right:1px #ffffff solid;">films</a></li>
    <li><a href="contactme.html" style="vertical-align:middle;padding-top:32px;margin-left:-20px;padding-bottom:14px;padding-right:11px;border-right:1px #ffffff solid;">contact me</a></li>
    
    <span class="navlinkimages">
    <li><a href=  target="_blank"><img src="social/social_vimeo.png" height = "30px" style = "margin-right:-14px;"/></a></li>
    <li><a href= target="_blank"><img src="social/social_youtube.png" height = "30px" style = "margin-right:-14px;"/></a></li>
    <li><a href= target="_blank"><img src="social/social_facebook.png" height="30px" style = "margin-right:-14px;"/></a></li>
    <li><a href=  target="_blank"><img src="social/social_twitter.png" height = "30px" style = "margin-right:-14px;" /></a></li>
    </span>
    </ul>`
    

    CSS Code:

    .nav {
        list-style-type:none;
        padding-left:0;
        margin-left:0;
        font-family:DinC;
        padding-bottom:5px;
        background-color: #000000;
        border-radius:5px;
        height:35px;
    }
    
    .navlinkimages {
      float:right;
      padding-top:5px;
    }
    
    .nav li {
      display:inline;
      vertical-align:middle;
    }
    
    ul.nav a:hover {
      color:#FA4B2A;
    }
    
    .nav li img {
      vertical-align:middle;
    }
    
    ul.nav a{
      text-decoration:none;
      margin-right:27px;
      color:#FFFFFF;
    }
    

    Is there a way to make it vertically-aligned on all browsers?

    Thanks!

  • Dmytro Shevchenko
    Dmytro Shevchenko about 12 years
    Or maybe even display: inline-block. Aren't you only supposed to vertical-align in block-ish elements?