指定了 width 属性的容器,当 left-margin 和 right-margin 设置为 auto 的时候,容器会居中显示。
<body>
<p>Analog synthsizers are often said to have a "warmer" sound than their digital counterparts.</p>
<p class="example">
p.example {
margin: 10px auto 10px auto;
text-align: left;
}
</p>
</body>
<style type="text/css">
body {
text-align: center; // 兼容 IE6
}
p {
width: 300px;
height: 50px;
border: 20px solid #0088dd;
}
p.example {
margin: 10px auto 10px auto;
text-align: left; // 继承来的 text-align:center 会让文字内容居中,这里设置回 left 默认值.
}
</style>