【问题标题】:Vertical Align Unordered List Nav Links?垂直对齐无序列表导航链接?
【发布时间】:2012-04-01 08:14:34
【问题描述】:

我看到这个问题已经被问过很多次了,但我认为我的情况略有不同。

我正在处理的网站的导航是用一个无序列表构建的,如下所示:

<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;
}

呈现为:

如您所见,有些导航项目是一行,有些是两行。

我可以垂直对齐:在一个行项目中间吗?

【问题讨论】:

    标签: html css html-lists alignment


    【解决方案1】:

    添加此样式(必要时覆盖)

    .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;
    }
    

    最后两个属性是 IE&lt;8 正确渲染 inline-blocks 元素所必需的内联技巧

    【讨论】:

      【解决方案2】:

      &lt;li&gt; 没有固定高度很难。如果你确实有一个固定的高度,你可以:

      <style>
            #centerMe{
                  line-height:4em
                 }
      </style>
      
      <p id="centerMe">
          This line is vertically centered!
      </p>
      

      您可以根据自己的喜好设置行高。

      我也从Nerds to Geeks看到了这个:

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

      我将p 元素更改为li;它应该仍然有效。

      【讨论】:

      • 显示:IE7 不支持表格单元格,如果您的文本有两行,则行高设置为 4em 会使标签溢出
      • 4em 是一个示例设置。您可以根据自己的喜好设置行高,这意味着在浏览器中显示并调整它直到它是正确的。在您最初的问题中没有提到 Internet Explorer 7。我假设如果有人正在为网络制作网站,他们正在为现代浏览器制作它,而不是 8 岁的浏览器,甚至微软也准备放弃支持。必须支持IE7吗?如果没有,解决方案在上面。如果是这样,请说服您的客户花 10 分钟进行升级。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 1970-01-01
      • 1970-01-01
      • 2013-01-28
      • 1970-01-01
      • 2012-08-16
      • 2018-03-12
      相关资源
      最近更新 更多