垂直对齐标签中的跨度

发布于 2024-10-27 04:10:17 字数 757 浏览 2 评论 0原文

这是我的标记。解决方案必须针对跨度中的单线或双线动态工作。所以它应该是可以添加到其中每一个并且工作正常的CSS。从internet上看,这似乎是不可能的。请证明我错了。

<ul style="text-align:center; list-style-type: none;">
    <li style="width: 200px">
        <a href="http://www.bing.com" style="display: block; height:35px; background: Black;">
            <span>Vertical Align This Text</span>
        </a>
    </li>
</ul>

<ul style="text-align:center; list-style-type: none;">
    <li style="width: 200px">
        <a href="http://www.bing.com" style="display: block; height:35px; background: Black;">
            <span>Vertical Align This Text With Double Line As Well</span>
        </a>
    </li>
</ul>

So here is my markup. Solution has to work dynamically for a single line or double line in the span. So it should be css that can be added to each of these and work fine. From looking on the internez this seems to be impossible. Please prove me wrong.

<ul style="text-align:center; list-style-type: none;">
    <li style="width: 200px">
        <a href="http://www.bing.com" style="display: block; height:35px; background: Black;">
            <span>Vertical Align This Text</span>
        </a>
    </li>
</ul>

<ul style="text-align:center; list-style-type: none;">
    <li style="width: 200px">
        <a href="http://www.bing.com" style="display: block; height:35px; background: Black;">
            <span>Vertical Align This Text With Double Line As Well</span>
        </a>
    </li>
</ul>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

棒棒糖 2024-11-03 04:10:17

这对我有用:

<ul style="text-align:center; list-style-type: none;">
    <li style="width: 200px; display: table">
        <a href="http://www.bing.com" style="display: table-cell; height:35px; background: Black; vertical-align: middle; text-align:center">
            <span>Vertical Align This Text</span>
        </a>
    </li>
</ul>

<ul style="text-align:center; list-style-type: none;">
    <li style="width: 200px; display: table">
        <a href="http://www.bing.com" style="display: table-cell; height:35px; background: Black; vertical-align: middle; text-align:center">
            <span>Vertical Align This Text With Double Line As Well</span>
        </a>
    </li>
</ul>

为了方便起见,CSS代码:

ul {
  list-style-type: none;
}

li {
  width: 200px;
  display: table;
}

a {
  display: table-cell;
  height: 35px;
  background: black;
  vertical-align: middle;
  text-align: center;
}

This works for me:

<ul style="text-align:center; list-style-type: none;">
    <li style="width: 200px; display: table">
        <a href="http://www.bing.com" style="display: table-cell; height:35px; background: Black; vertical-align: middle; text-align:center">
            <span>Vertical Align This Text</span>
        </a>
    </li>
</ul>

<ul style="text-align:center; list-style-type: none;">
    <li style="width: 200px; display: table">
        <a href="http://www.bing.com" style="display: table-cell; height:35px; background: Black; vertical-align: middle; text-align:center">
            <span>Vertical Align This Text With Double Line As Well</span>
        </a>
    </li>
</ul>

CSS code for convenience:

ul {
  list-style-type: none;
}

li {
  width: 200px;
  display: table;
}

a {
  display: table-cell;
  height: 35px;
  background: black;
  vertical-align: middle;
  text-align: center;
}
灯角 2024-11-03 04:10:17

Since your containing <a> have a set height value, you can use the ol' line-heighttrick of setting it to the same value, in this case:

a { 
    height:35px; 
    line-height:35px; 
}

This will only work, though, if your <span> isn't wider than your <li>. In the case with the double line, you'd need to set the <line-height> to half the <a>'s height (35px height divided by 2 lines), so around 17px. If you had three lines, it would work with 35px/3 ~ 11px. But as you can imagine, it will look smooshed at one point.
So depending on the content you might have to with the relative/absolute positioning, a smaller font, or just less content ;)

是你 2024-11-03 04:10:17

为您的

  • 提供以下 CSS 属性
  • li{
      position:relative;
    }
    
    a{
      position:absolute;
      top:50%;
      margin-top:-17px; /*negative half of your <a> tag height */
    }
    

    检查工作示例,网址为 http://jsfiddle.net/r4Wsx/

    Give your <li> and <a> the following CSS properties

    li{
      position:relative;
    }
    
    a{
      position:absolute;
      top:50%;
      margin-top:-17px; /*negative half of your <a> tag height */
    }
    

    Check working example at http://jsfiddle.net/r4Wsx/

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文