我有一个 CSS 元素,它应该是 1000 像素的固定宽度。这是我的 CSS 代码:
#content
{
margin:auto;
background-color:#DDDDDD;
position:absolute;
top:110px;
width:1000px;
}
它完美地设置了宽度、位置和颜色,但它总是左对齐。我希望整个文本 block 与中心对齐。我尝试使用 HTML 来做到这一点:
<div id="content" align="center">
Some test content
</div>
然而,这只会对齐元素内部的文本,而不是元素本身。这是它的样子:
http://gyazo.com/b44a28edfecb9cbfc3a5afe93fa08d8a
非常感谢任何将其与中心对齐的帮助。提前致谢!
最佳答案
您应该使用 css 将 div
居中。
像这样:
#content
{
background-color:#DDDDDD;
width:1000px;
margin: 110px auto; /*center the element and set top margin to 110px*/
}
<div id="content">
Some test content
</div>
关于html - 将 CSS 元素与设置的宽度对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16906330/