我想在另一个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到中间。但是垂直排列呢?


博士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

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


对于innerdiv没有指定它的高度值,没有纯CSS解决方案使它垂直居中。javascript的解决方案可以得到innerdiv的offsetHeight,然后计算style.marginTop。


我知道这个问题是一年前提出的…… 无论如何,感谢CSS3,你可以很容易地垂直对齐div在div(例子有http://jsfiddle.net/mcSfe/98/)

<div style="width: 100px; height: 100px">
<div>
Go to Hell!
</div>
</div>

div
{
display:-moz-box;
-moz-box-align:center;
} 

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

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

(参考)


而不是让自己陷入难以编写和难以维护的CSS(这也需要仔细的跨浏览器验证!)我发现放弃CSS而改用非常简单的HTML 1.0会更好:

<table id="outerDiv" cellpadding="0" cellspacing="0" border="0">
    <tr>
        <td valign="middle" id="innerDiv">
        </td>
    </tr>
</table>

这实现了原始海报想要的一切,并且是健壮的和可维护的。


我已经使用以下解决方案一年多了,它也适用于IE 7和8。

<style>
.outer {
    font-size: 0;
    width: 400px;
    height: 400px;
    background: orange;
    text-align: center;
    display: inline-block;
}

.outer .emptyDiv {
    height: 100%;
    background: orange;
    visibility: collapse;
}

.outer .inner {
    padding: 10px;
    background: red;
    font: bold 12px Arial;
}

.verticalCenter {
    display: inline-block;
    *display: inline;
    zoom: 1;
    vertical-align: middle;
}
</style>

<div class="outer">
    <div class="emptyDiv verticalCenter"></div>
    <div class="inner verticalCenter">
        <p>Line 1</p>
        <p>Line 2</p>
    </div>
</div>

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

CSS:

#outerDiv{
    height:100%;
}

Javascript:

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

另一种方法是使用转换转换

外部Div必须将其位置设置为相对或固定,内部Div必须将其位置设置为绝对,顶部和左侧为50%,并应用transform: translate(-50%, -50%)。

div.cn { 位置:相对; 宽度:200 px; 身高:200 px; 背景:灰色; text-align:中心; } div.inner { 位置:绝对的; 上图:50%; 左:50%; 宽度:100 px; 身高:100 px; -webkit-transform (-50%, -50%); 转换:翻译(-50%,-50%); 背景:红色; } < div class = " cn”> < div class = "内部" > 测验 < / div > < / div >

测试:

Opera 24.0(最低12.1) Safari 5.1.7(最少4个带-webkit-前缀) Firefox 31.0(最低3.6带-moz前缀,最低16不带前缀) Chrome 36(最少11个带-webkit前缀,最少36个不带前缀) IE 11, 10(最小9个带-ms前缀,最小10个不带前缀) 更多浏览器,我可以使用吗?


在父元素上显示文本对齐中心,在子元素上显示内联块。这将居中所有的东西。我相信这叫做“块浮动”。

<div class="outer">
 <div class="inner"> some content </div>
</div><!-- end outer -->

<style>
div.outer{
 width: 100%;
 text-align: center;
}
div.inner{
 display: inline-block;
 text-align: left
}
</style>

这也是一个很好的选择浮动,祝你好运!


垂直对齐任何只需3行CSS

HTML

<div class="parent-of-element">

    <div class="element">
        <p>Hello</p>
    </div>

</div>

简单的

.element {
  position: relative;
  top: 50%;
  transform: translateY(-50%);
}

CSS

.parent-of-element {
   position: relative;
   height: 500px;
   /* or height: 73.61% */
   /* or height: 35vh */
   /* or height: ANY HEIGHT */
}

.element {
  position: absolute;
  top: 50%;

  -webkit-transform: translateY(-50%);
      -ms-transform: translateY(-50%);
          transform: translateY(-50%);
}

根据shouldiprefix,这是你唯一需要的前缀

您还可以使用%作为.parent-of-element的'height'属性的值,只要元素的父元素具有高度或一些扩展其垂直大小的内容。


我个人更喜欢使用隐藏的伪元素来跨越外部容器的整个高度,并将其与其他内容垂直对齐。 Chris coyer有一篇关于这项技术的好文章。http://css-tricks.com/centering-in-the-unknown/ 这样做的巨大优势是可伸缩性。你不必知道内容的高度,也不必担心它的增长/缩小。此解决方案可伸缩:)。

这里有一个你需要的所有CSS和一个工作示例。 http://jsfiddle.net/m5sLze0d/

.center:before {
    content: ""; /* Adding Extra Space Above Element */
    display: inline-block;
    height: 100%;
    margin-right: -0.3em;
    vertical-align: middle;
}
.center_element {
    display:inline-block;
    float:none;
    vertical-align:middle;
    white-space:normal;
    text-align:left;
}

试着像这样对齐内部元素:

top: 0;
bottom: 0;
margin: auto;
display: table;

当然还有:

position: absolute;

这对我很有用。可以定义外部div的宽度和高度。

代码如下:

.outer { 位置:相对; text-align:中心; 宽度:100%; 身高:150 px;//允许任何高度,也在%中。 背景:灰色; } .outer > div:first-child { 位置:绝对的; 上图:50%; 左:50%; 宽度:100%; -webkit-transform (-50%, -50%); -ms-transform: -50%, -50%; 转换:翻译(-50%,-50%); 背景:红色; } < div class = "外" > < div class = "内部" > 把你的文本或div内容放在这里! < / div > < / div >


你可以在CSS中使用flex将div垂直和水平居中;

#outerDiv{
width: 500px;
    height: 500px;
    position:relative;
    border:1px solid #000;
    margin:0 auto;
    display: flex;
    -webkit-flex-direction: row;
    flex-direction: row;
    -webkit-align-items: center;
    align-items: center;
    -webkit-justify-content: center;
    justify-content: center;

    }

#innerDiv{
    width: 284px;
    height: 290px;
    border:1px solid #eee;

}

第二个是这样的;

    #outerDiv{
        width: 500px;
        height: 500px;
        position:relative;
        border:1px solid #000;
        }

        #innerDiv{
        max-width: 300px;
        height: 200px;
        background-color: blue;
        position:absolute; 
        left:0;
        right:0;
        top:0;
        bottom:0;
        margin:auto;
        border:1px solid #000;
        border-radius:4px;
    }

以及生成的HTML:

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

小提琴链接< http://jsfiddle.net/dGHFV/2515/>

试试这个

   #outerDiv{
        width: 500px;
        height: 500px;
        position:relative;
        border:1px solid red;
    }

    #innerDiv{
        width: 284px;
        height: 290px;
        position:absolute;
        top: 0px;
        left:0px;
        right:0px;
        bottom:0px;
        margin:auto;
        border:1px solid green;
    }

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

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

使用显示:表格单元格

.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的浏览器兼容性。


在这里输入图像描述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/


当你的高度没有设置(自动);你可以给内部div一些填充(顶部和底部),使它垂直居中:

<div>
    <div style="padding-top:20px;padding-bottom:20px">
    <!--content-->
    </div>
</div>

这将工作的方式回到IE6!

<!DOCTYPE html>在IE6上也是必需的! [将强制IE6默认严格模式以及]。

(当然,方框着色仅供演示使用)

#outer{ width: 180px; height: 180px; margin: auto; text-align: center; } #inner{ text-align: center; vertical-align: middle; width: 100px; height: 100px; display: inline-block; padding: .3em; } #center{ height: 100%; width:0px; vertical-align: middle; display: inline-block; } div {background: rgba(0,110,255,.7)} <DIV id=outer> <div id=center> </div><!--Don't break this line!--><div id=inner> The inner DIV </div> </DIV>


你可以通过添加下面提到的css样式来做到这一点。大多数浏览器都支持这一点。您可以在这里查看浏览器支持。愿一切都好!如有任何疑问,请评论

# outerDiv { 宽度:500 px; 身高:500 px; 位置:相对; 背景:灰色; 显示:flex; justify-content:中心; 对齐项目:中心; } # innerDiv { 背景:青色; 宽度:284 px; 身高:290 px; } < div id = " outerDiv " > < div id = " innerDiv " > 内心的Div < / div > < / div >


在另一个div中垂直居中一个div

# outerDiv { 宽度:500 px; 身高:500 px; 位置:相对; background - color: lightgrey; } # innerDiv { 宽度:284 px; 身高:290 px; 位置:绝对的; 上图:50%; 左:50%; 转换:翻译(-50%,-50%); -ms-transform: -50%, -50%;/*即9 */ -webkit-transform (-50%, -50%);/* Chrome, Safari, Opera */ 背景颜色:灰色; } < div id = " outerDiv " > < div id = " innerDiv " > < / div > < / div >


使居中:使垂直和水平对齐:

#parentDiv{
    display:table;
    text-align:center;
}

#child {
     display:table-cell;
     vertical-align:middle;
}

垂直居中的div项目在另一个div

只需将容器设置为display:table,然后将内部项设置为display:table-cell。在容器上设置高度,然后在内部项目上设置垂直对齐:中间。早在IE9时代,它就具有广泛的兼容性。

请注意,垂直对齐将取决于父容器的高度。

. cn { 显示:表; 身高:80 px; background - color: # 555; } 在 { 显示:表格单元; vertical-align:中间; 颜色:# FFF; padding-left: 10 px; padding-right: 10 px; } < div class = " cn”> <div class="inner">Item 1</div> <div class="inner">Item 2</div> < / div >


我想展示另一种跨浏览器的方法,可以使用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>

输出: