如何在不设置.headline宽度的情况下使div标题居中并保持其长度与内部文本(我的标题)一样宽。
<div class="sidebar">
<div class="headline"><p>My title</p></div>
</div>
现在,我正在使用此CSS,但是“我的标题”是由php生成的,其长度有所不同:
.headline {
width: 5em;
background: #900;
color: #FFF;
text-align: center;
text-transform: uppercase;
line-height: 2em;
border: solid 1px #555;
border-top: none;
padding: 0;
margin: 0 auto
}
谢谢
最佳答案
.sidebar {
text-align: center;
}
.headline {
display: inline-block;
}