如何使用CSS水平居中绝对div?

问题描述 投票:164回答:8

我有一个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;
}
css html center absolute
8个回答
387
投票

你需要设置left: 0right: 0

这指定了从窗口边缘偏移边缘边缘的距离。

与“顶部”类似,但指定框的右边距边缘偏移到框的包含块的[右/左]边缘的[左/右]。

资料来源:http://www.w3.org/TR/CSS2/visuren.html#position-props

注意:元素的宽度必须小于窗口,否则它将占据窗口的整个宽度。

如果您可以使用媒体查询指定最小边距,然后转换到auto以获得更大的屏幕尺寸。


.container {
  left:0;
  right:0;

  margin-left: auto;
  margin-right: auto;

  position: absolute;
  width: 40%;

  outline: 1px solid black;
  background: white;
}
<div class="container">
  Donec ullamcorper nulla non metus auctor fringilla.
  Maecenas faucibus mollis interdum.
  Sed posuere consectetur est at lobortis.
  Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.
  Sed posuere consectetur est at lobortis.
</div>

115
投票

这在IE8中不起作用,但可能是一个需要考虑的选项。如果您不想指定宽度,它主要是有用的。

.element
{
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}

13
投票

虽然上面的答案是正确的,但为了使新手简单,你需要做的就是设置左右边距。如果设置了width并且position是绝对的,则以下代码将执行此操作:

margin: 0 auto;
left: 0;
right: 0;

演示:

.centeredBox {
    margin: 0 auto;
    left: 0;
    right: 0;
   
   
   /** Position should be absolute */
    position: absolute;
    /** And box must have a width, any width */
    width: 40%;
    background: #faebd7;
   
   }
<div class="centeredBox">Centered Box</div>

5
投票

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>

3
投票
.centerDiv {
    position: absolute;
    left: 0;
    right: 0;
    margin: 0 auto;
    text-align:center;
}

2
投票

这么简单,只使用保证金和左右属性:

.elements {
 position: absolute;
 margin-left: auto;
 margin-right: auto;
 left: 0;
 right: 0;
}

您可以在此提示中查看更多内容=> How to set div element to center in html- Obinb blog


0
投票

这是一个链接,如果位置是绝对的,请使用它来使div位于中心。

HTML:

<div class="bar"></div>

CSS:

.bar{
  width:200px;
  border-top:4px solid red;
  position:absolute;
  margin-left:auto;
  margin-right:auto;
  left:0;
  right:0;
}

Example jsfiddle


-1
投票

你不能在margin:auto;元素上使用position:absolute;,如果你不需要它就删除它,但是,如果你这样做,你可以使用left:30%;((100%-40%)/ 2)和媒体查询最大和最小值:

.container {
    position: absolute;
    top: 15px;
    left: 30%;
    z-index: 2;
    width:40%;
    height: 60px;
    overflow: hidden;
    background: #fff;
}

@media all and (min-width:960px) {

    .container {
        left: 50%;
        margin-left:-480px;
        width: 960px;
    }

}

@media all and (max-width:600px) {

    .container {
        left: 50%;
        margin-left:-300px;
        width: 600px;
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.