【问题标题】:Vertically centering an anchor within spans and li with css使用 css 在 span 和 li 中垂直居中锚点
【发布时间】:2011-03-24 19:09:34
【问题描述】:

我正在使用 jQuery 选项卡,并在选项卡标题中添加了一些跨度,以便我可以使用背景图像。这是标记:

<ul>
    <li><span class='tab_outer'><span class='tab_inner'><span class='tab'><a href="#orderInfo">
        Order Info</a></span></span></span></li>
    <li><span class='tab_outer'><span class='tab_inner'><span class='tab'><a href="#notes">
        Notes</a></span></span></span></li>
    <li><span class='tab_outer'><span class='tab_inner'><span class='tab'><a href="#eventLog">
        Event Log</a></span></span></span></li>  
</ul>

问题:锚内的文本显示在锚块的最底部。如果我可以将它向上移动三个像素,那就完美了。以下是我认为相关的所有 CSS:

.tab_outer, .tab_inner, .tab
{
    display: inline-block;
    font-size: 11px;
    list-style: none;
}

.tab_outer
{
    margin-bottom: -3px;
    padding-right: 3px;
    margin-top: 4px;
}

.tab_inner
{
    margin-bottom: -1px;
    padding-left: 3px;
}

.tab
{
    margin-top: 0px;
    padding: 0px 4px 0px 4px;
    margin-bottom: -1px;
}

【问题讨论】:

  • 这段代码并没有真正显示问题。你能用jsFiddle.net复制它吗?

标签: css vertical-alignment text-alignment


【解决方案1】:
margin: 0px;
padding: 0px;
vertical-align: super;

【讨论】:

    【解决方案2】:

    试试 line-height 和 vertical-align 属性:

    .tab_outer  {
      line-height: 32px; /* Put the corresponding size here */
      vertical-align: middle;
    }
    

    或者您可以调整填充:

    .tab_outer {
      padding-bottom: 3px;
    }
    

    【讨论】:

    • 你打败了我! - 设置 line-height 解决了很多很多垂直对齐相关的问题
    猜你喜欢
    • 2011-11-28
    • 1970-01-01
    • 2018-05-17
    • 2012-06-21
    • 2016-11-06
    • 2013-05-21
    • 2019-09-23
    • 2014-11-15
    • 1970-01-01
    相关资源
    最近更新 更多