我有一个div,并希望它水平居中-尽管我给它margin:0 auto;
,但它没有居中……
.container {
position: absolute;
top: 15px;
z-index: 2;
width:40%;
max-width: 960px;
min-width: 600px;
height: 60px;
overflow: hidden;
background: #fff;
margin:0 auto;
}
发布于 2014-11-20 03:42:43
这在IE8中不起作用,但可能是一个需要考虑的选项。如果您不想指定宽度,它主要是有用的。
.element
{
position: absolute;
left: 50%;
transform: translateX(-50%);
}
发布于 2016-06-16 10:39:28
Flexbox?您可以使用flexbox。
.box {
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-justify-content: center;
justify-content: center;
}
.box div {
border:1px solid grey;
flex: 0 1 auto;
align-self: auto;
background: grey;
}
<div class="box">
<div class="A">I'm horizontally centered.</div>
</div>
发布于 2016-06-16 09:44:03
.centerDiv {
position: absolute;
left: 0;
right: 0;
margin: 0 auto;
text-align:center;
}
https://stackoverflow.com/questions/17976995
复制相似问题