<分区>
标签 html css width css-position
<分区>
我发现了一些涉及此问题的问题,并且我尝试了多种方法来使其正常工作,但一直删除,所以我想我只发布我的细节。我有一个 div,在其中我想将另一个 div 置于固定位置的中心。我可以用我附加的 CSS 来做到这一点。但是,我看到的唯一问题是,如果我更改主容器宽度,那么我也必须更新标题 div 宽度。我希望能够为标题 div 使用 100% 的宽度,以便它始终与父容器的宽度相匹配,同时具有固定的位置并在 div 中居中。
#main-container{
width: 800px;
position: relative;
margin: 0 auto;
background-color:black;
height: 100%;
}
#header{
color: white;
top: 20px;
position: fixed;
left: 50%;
width: 1000px;
margin-left:-500px;
text-align: center;
overflow:hidden;
}
最佳答案
对于#main-container 尝试
margin-left:auto;
margin-right:auto;
关于html - 固定位置 Div 以 100% 宽度居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23635303/