尝试将div容器
问题描述:
这里中居中一个div是我的CSS:尝试将div容器
#main {
background: #eae8dc;
margin: 0 auto;
width: 790px;
height: 900px;
position: relative;
display: block;
}
#header-container {
display: inline-block;
height: 118px;
width: 648px;
background: #9ed5de;
margin: 11px auto;
}
这里的HTML
<div id="main">
<header>
<h1 id="header-container">
</h1>
</header>
</div>
这个问题是固定我的保证金问题有帮助,但现在我似乎无法将标题容器居中。 Why does this CSS margin-top style not work?
答
的text-align应该可以帮助您居中:
#main {
background: #eae8dc;
margin: 0 auto;
width: 790px;
height: 900px;
position: relative;
display: block;
text-align: center;
}
#header-container {
display: inline-block;
height: 118px;
width: 648px;
background: #9ed5de;
margin: 11px auto;
}
这里的JS小提琴http://jsfiddle.net/RBkmv/
答
这是不好的做法。你应该设计你的元素header
而不是你的h1
元素。
#main {
background: #eae8dc;
margin: 0 auto;
width: 790px;
height: 900px;
position: relative;
display: block;
text-align: center;
}
header {
display: inline-block;
height: 118px;
width: 648px;
background: #9ed5de;
margin: 11px auto;
}
这样,你的CSS代码工作。
+0
你的JSFiddle仍然有它他H1 – Trent
+0
不,样式是针对标题元素的。 –
CSS是如此不直观:(。谢谢,这绝对有效,但它会集中#main中的每个元素?我想避免,如果可能 – natecraft1
是的,如果 - 如果这不是你想要的,你需要重新考虑你的方法,或者依赖50%的左边距,这是不好的建议 – Trent
什么是规范或典型的方法来组织html和css以达到这个目的? – natecraft1