我正在尝试使用带有 ul、li 元素的 CSS 创建一个水平菜单。目标是完成以下任务:每当菜单选项太长时,文本会转到第二行,垂直和水平对齐。
I managed to make that work, however when the option is two-lined, it automatically get the max-width parameter as its width, and doesn't set to option's actual width. 但是,当该选项是一行时,它确实设置为实际宽度。max-width 是我想要实现的错误参数吗?我确实希望 li 元素与其内容的宽度相同。
HTML:
<ul class="trials">
<li><span><a href="#">kitchen furniture</a></span></li>
<li><span><a href="#">wardrobes & cabinets</a></span></li>
<li><span><a href="#">hallway furniture</a></span></li>
<li><span><a href="#">materials</a></span></li>
</ul>
CSS:
.trials {
margin: 0;
padding: 0;
text-transform: uppercase;
float: right;
}
.trials li {
display: inline-block;
list-style: none;
margin-left: 10px;
}
.trials li {
max-width: 130px;
line-height: 40px;
height: 40px;
border: 1px blue solid;
}
.trials li span {
display: -moz-inline-box; /* FF2 or lower */
display: inline-block; /* FF3, Opera, Safari */
line-height: normal;
vertical-align: middle;
text-align: center;
}
.trials li span {
*display : inline; /* haslayout for IE6/7 */
}
这是小提琴:http: //jsfiddle.net/so_artistic/ecCM4/
帮助将不胜感激。