1

After checking similar questions to this, none of the recommended solutions worked for me. I am trying to align li elements to be vertically centered in their div:

HTML:

<div id="navi">
<ul id="navilist">
<li><a href="#home">
<img src="Images/homelnk.jpg" alt="Home"/></a></li>
<li><a href="#SGU">SGU</a></li>
<li><a href="#SGJR">SGJR</a></li>    
<li><a href="#REGISTRATION">REGISTRATION</a></li>
<!--<li><a href="#PHOTOS">PHOTO GALLERY</a></li>-->
<li><a href="#SCHOLARSHIP">SCHOLARSHIP</a></li>
<!--<li><a href="#CONTACT">CONTACT US</a></li>-->
<!--<li><a href="#SOCIALMEDIA">SOCIAL MEDIA</a></li>-->
<li><a href="#MERCHANDISE">MERCHANDISE</a></li>
<li><a href="#FORMS">FORMS</a></li>
<li><a href="#PAY">PAY FOR SGU</a></li>
</ul>
</div>

CSS:

#navi {
position: relative;
height: 50px;
width:auto;
background: #ed7a4f;
vertical-align:middle;
}

#navi li{
display: inline;
list-style-type:none;
padding: 0px 2px 5px 2px;
}

How would I maintain a horizontal list, but center the text vertically?

Flip
  • 6,233
  • 7
  • 46
  • 75
Chris569x
  • 335
  • 1
  • 3
  • 14

2 Answers2

1

There's some tricks to getting vertical alignment to work. Here's a good stackoverflow answer about it. There's a good comment to the answer which explains why you need the empty span...

Well, I figured I'd offer you an alternative. You use an empty <span> because vertical-align aligns elements relative to their siblings. If an element has no siblings, it will not be vertically aligned.

I also made an update to your fiddle

Here's a snippet of the css:

span { 
    vertical-align: middle;
    display: inline-block; }
ul{
    margin:0; padding:0;
    display:inline-block;
    list-style-type:none; 
    vertical-align:middle; }
#navi {
    height: 100px;
    background: #ed7a4f;
}
#navi li{   
    display: inline-block;
    padding: 5px; }
Community
  • 1
  • 1
Tim Mac
  • 1,149
  • 1
  • 8
  • 17
-1

You don't need vertical alignment, you just need to reset your CSS and use padding. You need to adjust accordingly to the size of your image.

#navi {
    background: #ed7a4f;
}

#navi ul, navi li {
  margin:0;  
  padding:0;
  list-style:none;      
}

#navi li {
    display: inline-block;
}

#navi a {
    display:block;
    padding: 0px 5px 5px 5px;
}
Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176