简体   繁体   English

垂直对齐多行文本(菜单元素)?

[英]Vertically align more than one line of text (menu elements)?

I'm trying to vertically align the text inside an UL.我正在尝试垂直对齐 UL 内的文本。 Problem is that some list items have more than one line of text and therefore cannot use line-height.问题是某些列表项的文本不止一行,因此不能使用行高。

The fiddle: http://jsfiddle.net/jaAYT/小提琴: http://jsfiddle.net/jaAYT/

Here's the result I want to achieve: http://img402.imageshack.us/img402/7979/menuor.jpg这是我想要达到的结果: http://img402.imageshack.us/img402/7979/menuor.jpg

Here's my HTML:这是我的 HTML:

<ul class="menu">
    <li> <a href="#"><span>WHAT IS TEAM X?</span></a> </li>
    <li> <a href="#"><span>COMPANY BENEFITS</span></a> </li>
    <li> <a href="#"><span>COMPANY, COMPANY DATA</span></a> </li>
    <li> <a href="#"><span>TEAM X COVERAGE</span></a> </li>
    <li> <a href="#"><span>COMPANY2 </span></a> </li>
    <li> <a href="#"><span>COMPANY3 </span></a> </li>
    <li> <a href="#"><span>LATEST CAMPAIGNS</span></a> </li>
    <li> <a href="#"><span>CONTACT</span></a> </li>
</ul>

Here's the CSS:这是 CSS:

.menu {
    float: left;
    margin: 0px 0px 0px 0px;
    width: 720px;
}
.menu li {
    float: left;
    position: relative;
}
.menu li a {
    width: 86px;
    height: 52px;
    padding: 15px 0px 15px 4px;
    display: block;
    float: left;
    color: #fff;
    font-size: 14px;
    font-weight: bold;
    text-decoration: none;
    background-color: #ccc;
}
.menu li a:hover {
    background-color: #000;
}

Thank you!谢谢! Cris克里斯

Add these declarations to the .menu li a rule:将这些声明添加到.menu li a规则中:

display: -webkit-box;
display: -moz-box;
display: box;
-webkit-box-align: center;
-moz-box-align: center;
box-align: center;

See updated fiddle .请参阅更新的小提琴

08/14/13 See this answer for current syntax. 2013 年 8 月 14 日请参阅此答案以了解当前语法。

It's not the prettiest thing to do and it will not work if the links are dynamically generated, but you could add special classes to those spans with one or two lines only and then add some padding at the top to center the text.这不是最漂亮的事情,如果链接是动态生成的,它将无法工作,但是您可以只用一两行向那些跨度添加特殊类,然后在顶部添加一些padding以使文本居中。

.oneLine {display:block; padding-top:1.25em;}
.twoLines {display:block; padding-top:.75em;}

<ul class="menu">
    <li> <a href="#"><span class="twoLines">WHAT IS TEAM X?</span></a> </li>
    <li> <a href="#"><span class="twoLines">COMPANY BENEFITS</span></a> </li>
    <li> <a href="#"><span>COMPANY, COMPANY DATA</span></a> </li>
    <li> <a href="#"><span>TEAM CYCAD COVERAGE</span></a> </li>
    <li> <a href="#"><span class="oneLine">COMPANY2 </span></a> </li>
    <li> <a href="#"><span class="oneLine">COMPANY3 </span></a> </li>
    <li> <a href="#"><span class="twoLines">LATEST CAMPAIGNS</span></a> </li>
    <li> <a href="#"><span class="oneLine">CONTACT</span></a> </li>
</ul>

http://jsfiddle.net/jasongennaro/4MWTj/ http://jsfiddle.net/jasongennaro/4MWTj/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM