[英]CSS Menu: Fill Total Area Equally, Vertically-Centered text
I'm trying to create a simple CSS menu. 我正在尝试创建一个简单的CSS菜单。 Here are the constraints: 这里是约束:
Here is the html: 这是html:
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
</ul>
Here are my attempts, each has its own failing. 这是我的尝试,每一次都有自己的失败。
http://jsfiddle.net/QzYAr/266/ (widths aren't expressed as a %) http://jsfiddle.net/QzYAr/266/ (宽度未表示为%)
ul { width: 600px; display: table; table-layout: fixed; background: #EEE; } li { float: left; border: 1px dotted red; } a { display: table-cell; height: 50px; vertical-align: middle; text-align: center; width: 150px; border: 1px dotted green; }
http://jsfiddle.net/RzeK2/ (Anchor tag doesn't fill height) http://jsfiddle.net/RzeK2/ (锚标记未填充高度)
ul { width: 80%; display: table; table-layout: fixed; background: #EEE; } li { float: left; border: 1px dotted red; } a { display: table-cell; height: 50px; vertical-align: middle; text-align: center; width: 150px; /*this is the part that needs work?*/ border: 1px dotted green; }
http://jsfiddle.net/XsLHY/ (Anchor tag text isn't vertically centered) http://jsfiddle.net/XsLHY/ (锚标记文本未垂直居中)
ul { width: 80%; display: table; table-layout: fixed; background: #EEE; } li { display: table-cell; width: 33%; height: 50px; vertical-align: middle; text-align: center; border: 1px dotted red; } a { display: block; border: 1px dotted green; height: 50px; }
http://jsfiddle.net/w55Lg/ [suggested by two answers below] (Creates ugly button if text wraps, and also other buttons aren't full height now) http://jsfiddle.net/w55Lg/ [由以下两个答案建议](如果文本自动换行,则创建难看的按钮,并且其他按钮现在还没有全高)
same as three, plus: a { line-height: 50px; }
Try this: 尝试这个:
ul {
width: 80%;
background: #EEE;
}
li {
width:33.33333333%;
float:left;
}
a{
display:block;
text-align:center;
height:50px;
line-height:50px;
}
a:hover {
background:red;
}
DEMO: http://jsfiddle.net/QzYAr/267/ 演示: http : //jsfiddle.net/QzYAr/267/
EDIT: If you have longer text, you can wrap it inside a <span>
tag and add this CSS: 编辑:如果您有更长的文本,您可以将其包装在<span>
标记内并添加以下CSS:
ul span{
display:inline-block; vertical-align:middle;
line-height:14px;
}
Using fiddle #2, updated version: http://jsfiddle.net/XsLHY/1/ 使用小提琴#2,更新版本: http : //jsfiddle.net/XsLHY/1/
You just need to add a matching line-height to the li
as the height of the a
. 你只需要一个匹配的line-height添加到li
的高度a
。
CSS CSS
ul {
width: 80%;
display: table;
table-layout: fixed;
background: #EEE;
}
li {
display: table-cell;
width: 33%;
height: 50px;
vertical-align: middle;
text-align: center;
border: 1px dotted red;
line-height: 50px;
}
a {
display: block;
border: 1px dotted green;
height: 50px;
}
This could be done using: 可以使用以下方法完成:
display: flex;
http://jsfiddle.net/LD2w4/ http://jsfiddle.net/LD2w4/
To fix the anchor problem, you can switch the order, so the <a>
wraps the <li>
instead of the other way around: 要解决定位问题,可以切换顺序,因此<a>
包装<li>
而不是采用其他方式:
<ul>
<a href="#"><li>Item 1</li></a>
<a href="#"><li>Item 2</li></a>
<a href="#"><li>Item 3</li></a>
</ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.