【发布时间】:2014-02-14 17:41:51
【问题描述】:
我的要求:
我想创建一个垂直的左侧菜单,其中包含 SQUARE 菜单选项和居中文本,如下所示
到目前为止我的收获:
文本未垂直对齐!
HTML:
<div id="LeftMenu">
<div class="LeftMenuItem">Invoices</div>
<div class="LeftMenuItem">Expenses</div>
</div>
CSS:
html, body {
height: 100% !important;
width: 100% !important;
margin: 0 !important;
font-size:12px;
}
#LeftMenu {
background-color: #0583c0;
height: 100%;
width: 8%;
}
.LeftMenuItem {
border-bottom:1px;
border-bottom-color:#00619e;
border-bottom-style:solid;
width: 100%;
height: 0;
color:White;
text-align:center;
vertical-align:middle;/* of course this doesnt work */
padding-bottom:100%;
}
.LeftMenuItem:hover {
background-color:#0095de;
}
JSFIDDLE:
问题:
问题在于它没有垂直对齐文本:(
我尝试过的其他解决方案:
我尝试使用表格、表格行、表格单元格的显示。这确实使文本居中对齐,但现在使单元格与页面一样长。
约束:
我不想使用硬编码像素。我想使用百分比。
【问题讨论】:
-
你绝对反对用像素设置内容的高度吗?
-
@DanielLisik 当有动态选项时,以像素为单位对高度进行硬编码有什么好处?
-
更新了我的答案,现在适用于垂直居中的各种内容。