我想在另一个div里面居中一个div。

<div id="outerDiv">
    <div id="innerDiv">
    </div>
</div>

这是我目前使用的CSS。

    #outerDiv {
        width: 500px;
        height: 500px;
        position: relative;
    }
    
    #innerDiv {
        width: 284px;
        height: 290px;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-top: -147px;
        margin-left: -144px;
    }

如您所见,我现在使用的方法取决于#innerDiv的宽度和高度。如果宽度/高度改变,我将不得不修改margin-top和margin-left值。是否有任何通用的解决方案,我可以使用中心的#innerDiv独立于它的大小?

我发现使用margin: auto可以水平对齐#innerDiv到中间。但是垂直排列呢?


当前回答

另一种实现水平和垂直居中的方法是:

.Absolute-Center {
  margin: auto;
  position: absolute;
  top: 0; left: 0; bottom: 0; right: 0;
}

(参考)

其他回答

如果你在看完上面给出的精彩答案后仍然不明白。

这里有两个简单的例子,告诉你如何实现它。

使用显示:表格单元格

.wrapper { 显示:表格单元; vertical-align:中间; text-align:中心; 宽度:400 px; 身高:300 px; 边框:1px实体#555; } .container { 显示:inline-block; text-align:左; 填充:20 px; 边框:1px solid #cd0000; } < div class = "包装" > < div class = "容器" > 使用"<strong>display: table-cell</strong>"将div居中对齐 < / div > < / div >

使用flex-box(显示:flex)

.wrapper { 显示:flex; justify-content:中心; 宽度:400 px; 身高:300 px; 边框:1px实体#555; } .container { align-self:中心; 填充:20 px; 边框:1px solid #cd0000; } < div class = "包装" > < div class = "容器" > 使用"<strong>display: flex</strong>"将div居中 < / div > < / div >

注意:在使用上述实现之前,请检查display: table-cell和flex的浏览器兼容性。

我想展示另一种跨浏览器的方法,可以使用CSS3 calc()解决这个问题。

我们可以使用calc()函数来控制子div的margin-top属性,当它的位置绝对相对于父div时。

使用calc()的主要优点是父元素的高度可以随时改变,并且子div将始终对齐到中间。

边缘顶部计算是动态的(通过css而不是通过脚本,这是一个非常大的优势)。

看看这个现场演示

<!DOCTYPE html>
<html>
  <head>
    <style>
      #parent{
        background-color:blue;
        width: 500px;
        height: 500px;
        position:relative;
      }
      #child{
        background-color:red;
        width: 284px;
        height: 250px;
        position:absolute;
        /* the middle of the parent(50%) minus half of the child (125px) will always             
           center vertically the child inside the parent */
        margin-top: -moz-calc(50% - 125px);
        /* WebKit */
        margin-top: -webkit-calc(50% - 125px);
        /* Opera */
        margin-top: -o-calc(50% - 125px);
        /* Standard */
        margin-top: calc(50% - 125px);
      }
    </style>
  </head>
  <body>
    <div id="parent">
      <div id="child">
      </div>
    </div>
  </body>
</html>

输出:

博士tl;

垂直对齐middle可以,但是你必须在父元素上使用table-cell,在子元素上使用inline-block。

这个解决方案在IE6和ie7中不起作用。你的方法比较安全。但既然你用CSS3和HTML5标记了你的问题,我想你不介意使用现代解决方案。

经典的解决方案(表格布局)

这是我最初的答案。它仍然可以正常工作,并且是得到最广泛支持的解决方案。表格布局会影响你的渲染性能,所以我建议你使用一种更现代的解决方案。

这里有一个例子


测试:

FF3 + 5。 FF4 +。 Safari 5 + Chrome 11 +。 IE9 +。


HTML

<div class="cn"><div class="inner">your content</div></div>

CSS

.cn {
  display: table-cell;
  width: 500px;
  height: 500px;
  vertical-align: middle;
  text-align: center;
}

.inner {
  display: inline-block;
  width: 200px; height: 200px;
}

现代解决方案(转换)

由于转换得到了很好的支持,现在有一种更简单的方法来实现它。

CSS

.cn {
  position: relative;
  width: 500px;
  height: 500px;
}

.inner {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%,-50%);
  width: 200px;
  height: 200px;
}

演示


♥我最喜欢的现代解决方案(flexbox)

我开始越来越多地使用flexbox,它现在也得到了很好的支持,这是迄今为止最简单的方法。

CSS

.cn {
  display: flex;
  justify-content: center;
  align-items: center; 
}

Demo

更多的例子和可能性: 比较一个页面上的所有方法

你可以用一个简单的javascript (jQuery)块来做到这一点。

CSS:

#outerDiv{
    height:100%;
}

Javascript:

<script type="text/javascript">
    $(document).ready(function () {
        $("#innerDiv").css('top', ($(window).height() - $("#content").height()) / 2);
    });
</script>

在这里输入图像描述100%它工作

.div1{
  height: 300px;
  background: red;
  width: 100%;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -ms-flex-align: center;
  -webkit-align-items: center;
  -webkit-box-align: center;
  align-items: center;
}
.div2{
  background: green;
  height: 100px;
  width: 100%;
}

    <div class="div1">
      <div class="div2">
      sdfd
      </div>
    </div>

https://jsfiddle.net/Mangesh1556/btn1mozd/4/