我有一个高度为30px
的div
。
我想在这个div
中添加普通文本。如何使纯文本显示在div
的中心位置?
也就是说,文本将显示在div
顶部下方15px
的位置。
我尝试使用带margin-top: 15;
的标签,但效果不佳。
height: 30px; line-height: 30px; padding: 0;
以下 CSS 可以起作用
text-align:center;
vertical-align:middle;
display:table-cell;
你可以:
<div style="height:30px; line-height:30px; text-align:center;">
Label
</div>
设置文本的line-height
属性可以帮助你适应单行的高度。
虽然这是一个旧的主题,但我认为应该介绍一种flex
解决方案。
如果您使用以下样式设置您的
padding: 15px 0px;
没有指定高度。
#MyDiv
{
background-image: url(images/pagedescription_bar.png);
background-repeat: repeat-x;
border-color: #868686;
border-width: thin;
border-style: solid;
border-style: none;
width: 1008px;
height:70px;
color: #FB8022;
font-size: 16px;
font-weight: bold;
}
#MyDiv label
{
width: 1008px;
text-align: center;
height: 70px;
vertical-align: middle;
display:table-cell;
}
如果你想让任何元素在其父级居中,只需使用样式为父级指定特定宽度,并为子元素添加style="margin:0 auto"。 祝好运
<label></label>
元素不是块级元素,而是内联元素。
尝试这段代码
<div style="height:30px">
<label style="display:block; margin-top:15px;">
</label>
</div>