我想在另一个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
更多的例子和可能性:
比较一个页面上的所有方法
另一种方法是使用转换转换
外部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个不带前缀)
更多浏览器,我可以使用吗?
垂直对齐任何只需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;
}
你可以在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>
如果你在看完上面给出的精彩答案后仍然不明白。
这里有两个简单的例子,告诉你如何实现它。
使用显示:表格单元格
.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>
输出: