Vertical Align Unordered List Nav Links?

2019-04-13 03:35发布

问题:

I see that this question has been asked many times but I think my case is slightly different.

The navigation for the site I am working on is constructed with an unordered list like so:

<div class="nav_root nav_area_top">
    <ul class="nav_root_wrap">
        <li class="nav_parent first">
            <a href="california.providence.org/torrance/pages/Locations.aspx">
                Locations
            </a>
        </li>
        <li class="nav_parent first">
            <a href="california.providence.org/torrance/pages/Locations.aspx">
                Health Services
            </a>
        </li>
    </ul>
</div>

...

.nav_area_top ul.nav_root_wrap > li
{
  background-image: url(../images/vert_bg_blue.jpg);
  background-color: #0C83BB;
  padding: 4px 15px 4px 15px;
  float: left;
  font-size: 13px;
  margin-left: 3px;
  -moz-border-radius: 5px 5px 0 0;
  -webkit-border-radius: 5px 5px 0 0;
  border-radius: 5px 5px 0 0;
  min-height: 36px;
  text-align: center;
  border: 0px;
}
.nav_area_top ul.nav_root_wrap > li > a 
{
    color:#fff;
    padding: 0px;
    line-height: 18px;
}

Which renders to:

As you can see some of the nav items are one line and some are two.

Is it possible for me to vertical-align: middle the one line items?

回答1:

add this style (override if necessary)

.nav_area_top ul.nav_root_wrap > li {
   line-height: 36px;
}

.nav_area_top ul.nav_root_wrap > li > a {
   line-height: normal; /* or just choose another value: e.g. 1.5; */
   vertical-align: middle;
   display: inline-block;
   *zoom: 1;
   *display: inline;
}

last two properties are inline hacks necessary for IE<8 to properly render inline-blocks element



回答2:

It's hard without a fixed height for your <li>. If you do have a fixed height you could:

<style>
      #centerMe{
            line-height:4em
           }
</style>

<p id="centerMe">
    This line is vertically centered!
</p>

You'd set the line height to your liking.

I also saw this from Nerds to Geeks:

#container li{
display: table-cell;
vertical-align: middle;
text-align: center;
font-size:28px;
}

I Changed the p element to li; it should still work.