我认为#container
将以#main_content
为中心。然而,事实并非如此。为什么这不起作用,我该如何修复它?
#main_content {
top: 160px;
left: 160px;
width: 800px;
min-height: 500px;
height: auto;
background-color: #2185C5;
position: relative;
}
#container {
width: auto;
height: auto;
margin: 0 auto;
padding: 10px;
position: relative;
}
<div id="main_content">
<div id="container">
</div>
</div>
发布于 2015-06-18 04:42:45
另一种有趣的方式:fiddle
CSS
.container {
background: yellow;
width: 100%;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
.centered-div {
width: 80%;
height: 190px;
margin: 10px;
padding: 5px;
background: blue;
color: white;
}
HTML
<div class="container">
<div class="centered-div">
<b>Enjoy</b>
</div>
</div>
发布于 2013-03-22 20:36:19
你可以用下面的小代码将浮动div居中:
#div {
width: 200px;
height: 200px;
position: absolute;
left: 50%;
top: 50%;
margin-top: -100px;
margin-left: -100px;
}
发布于 2013-03-13 11:55:48
尝试添加
text-align: center;
添加到父容器CSS声明中。并将以下内容添加到子容器:
display: inline-block;
它必须做到这一点。
https://stackoverflow.com/questions/15376634
复制相似问题