我想用CSS垂直居中放置一个div。我不需要表或JavaScript,只需要纯CSS。我找到了一些解决方案,但都缺少Internet Explorer 6支持。
<body>
<div>Div to be aligned vertically</div>
</body>
如何在所有主要浏览器(包括Internet Explorer 6)中垂直居中放置div?
我想用CSS垂直居中放置一个div。我不需要表或JavaScript,只需要纯CSS。我找到了一些解决方案,但都缺少Internet Explorer 6支持。
<body>
<div>Div to be aligned vertically</div>
</body>
如何在所有主要浏览器(包括Internet Explorer 6)中垂直居中放置div?
当前回答
特别是对于具有相对(未知)高度的父div,未知解决方案中的居中对我来说非常有用。
它在Chrome、Firefox、Opera和Internet Explorer中进行了测试。
/*此父项可以是任何宽度和高度*/.块{文本对齐:居中;}/*鬼魂被推着保持完美的居中*/.block:之前{内容:“”;显示:内联块;高度:100%;垂直对齐:中间;右边距:-0.25em;/*调整间距*/}/*要居中的元素可以也可以是任何宽度和高度*/.居中{显示:内联块;垂直对齐:中间;宽度:300px;}<div style=“width:400px;height:200px;”><div class=“block”style=“高度:90%;宽度:100%”><div class=“center”><h1>一些文本</h1><p>任何其他文本。。。“”</p></div></div></div>
其他回答
要垂直对齐网页(包括Internet Explorer 6)中的框,可以使用:
有条件的评论haslayout属性显示:其他表值(现在为flex)
不停摆弄
/*Internet Explorer 8和其他*/.主要{宽度:500px;边距:自动;边框:实心;}html格式{高度:100%;宽度:100%;显示:表格;}正文{显示:表格单元格;垂直对齐:中间;}<!-- [如果lte IE 7]><style>/*应该在<head>中*/html,正文,.ie{高度:100%;文本对齐:居中;空白:nowrap;}例如,.main{显示:内联;/*在使用块元素而不是内联元素时与缩放一起使用*/缩放:1;垂直对齐:中间;文本对齐:左侧;空白:正常;}</style><b class=“ie”></b><!--[endif]--><div class=“main”><p>用你的内容填满它</p><p><a href=“https://jsfiddle.net/h8z24s5v/embedded/result/“>JsFiddle版本</a></p></div>
实际上,InternetExplorer7在这里会带来一些麻烦,因为它是唯一严格应用高度的浏览器:100%应用于HTML/body元素。
但是,这是过去和现在的事了,谁还记得旧版本的Internet Explorer,表/表单元格很好,display:flex很有前途,display:grid总有一天会出现。
现在通过flex的另一个示例
html格式{显示:柔性;最小高度:100vh;/*或高度*/}正文{边距:自动;}<div>div垂直对齐</div>
使用转换的三行代码实际上在现代浏览器和Internet Explorer上运行:
.element{
position: relative;
top: 50%;
transform: translateY(-50%);
-moz-transform: translateY(-50%);
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
}
我添加这个答案是因为我发现这个答案的前一个版本有些不完整(堆栈溢出不允许我简单地评论)。
如果当前div位于主体中且没有容器div,则“position”relative会破坏样式。然而,“fixed”似乎有效,但它显然会修复视口中心的内容此外,我使用这种样式对一些覆盖div进行了居中设置,并发现在Mozilla中,这个转换后的div中的所有元素都失去了底部边框。可能是渲染问题。但是,只在其中一些页面上添加最小的填充就可以正确地渲染。Chrome和Internet Explorer(令人惊讶)渲染了这些框,而不需要填充
因为每次我需要垂直居中div时,我都会一遍又一遍地搜索它,这个答案总是首先出现的,所以我会把这个留给未来的我(因为提供的解决方案都不能很好地满足我的需求):
因此,如果已经在使用引导程序,可以按如下方式进行:
<div style="min-height: 100vh;" class="align-items-center row">
<div class="col" style="margin: auto; max-width: 750px;"> //optional style to center horizontally as well
//content goes here
</div>
</div>
只需这样做:在div中添加类:
.modal {
margin: auto;
position: absolute;
top: 0;
right: 0;
left: 0;
bottom: 0;
height: 240px;
}
请阅读本文以获得解释。注:高度是必需的。
居中是CSS最困难的方面之一。
这些方法本身通常不难理解。相反,这更多是因为有很多方法可以将事情集中起来。
您使用的方法可能会有所不同,这取决于您试图居中的HTML元素,或者是水平居中还是垂直居中。
水平居中
水平对中元件通常比垂直对中元件更容易。下面是一些常见的元素,您可能希望水平居中,以及不同的方法。
使用CSS文本居中属性居中文本
要使文本或链接水平居中,只需使用文本对齐属性和值中心:
HTML
<div class="container">
<p>Hello, (centered) World!</p>
</div>
CSS
.container {
font-family: arial;
font-size: 24px;
margin: 25px;
width: 350px;
height: 200px;
outline: dashed 1px black;
}
p {
/* Center horizontally*/
text-align: center;
}
使用CSS边距自动居中分隔
使用值为0的速记边距属性,自动将块级元素(如div)水平居中:
HTML
<div class="container">
<div class="child"></div>
</div>
CSS
.container {
font-family: arial;
font-size: 24px;
margin: 25px;
width: 350px;
height: 200px;
outline: dashed 1px black;
}
.child {
width: 50px;
height: 50px;
background-color: red;
/* Center horizontally*/
margin: 0 auto;
}
使用Flexbox水平居中跳水
Flexbox是在页面上居中的最现代的方式,它使设计响应式布局比以前容易得多。然而,它在一些传统浏览器(如Internet Explorer)中并不完全受支持。
要使用Flexbox水平居中元素,只需将display:flex和justify content:center应用于父元素:
HTML
<div class="container">
<div class="child"></div>
</div>
CSS
.container {
font-family: arial;
font-size: 24px;
margin: 25px;
width: 350px;
height: 200px;
outline: dashed 1px black;
/* Center child horizontally*/
display: flex;
justify-content: center;
}
.child {
width: 50px;
height: 50px;
background-color: red;
}
垂直居中
如果没有像Flexbox这样的现代方法,将元素垂直居中可能是一件很麻烦的事。在这里,我们将介绍一些较旧的方法,首先垂直居中,然后向您展示如何使用Flexbox实现这一点。
如何使用CSS绝对定位和负边距使Div垂直居中
在很长一段时间里,这是垂直居中的常用方法。对于此方法,必须知道要居中的元素的高度。
首先,将父元素的position属性设置为relative。
然后,对于子元素,将position属性设置为absolute,并将top设置为50%:
HTML
<div class="container">
<div class="child"></div>
</div>
CSS
.container {
font-family: arial;
font-size: 24px;
margin: 25px;
width: 350px;
height: 200px;
outline: dashed 1px black;
/* Setup */
position: relative;
}
.child {
width: 50px;
height: 50px;
background-color: red;
/* Center vertically */
position: absolute;
top: 50%;
}
但这实际上只是将子元素的顶部边缘垂直居中。
要使子元素真正居中,请将margin-top属性设置为-(子元素高度的一半):
.container {
font-family: arial;
font-size: 24px;
margin: 25px;
width: 350px;
height: 200px;
outline: dashed 1px black;
/* Setup */
position: relative;
}
.child {
width: 50px;
height: 50px;
background-color: red;
/* Center vertically */
position: absolute;
top: 50%;
margin-top: -25px; /* Half this element's height */
}
使用变换和平移使Div垂直居中
如果您不知道要居中的元素的高度(或者即使您知道),这个方法是一个很好的技巧。
该方法与上述负边距方法非常相似。将父元素的位置属性设置为相对。
对于子元素,将position属性设置为absolute,并将top设置为50%。现在,不要使用负边距来真正居中子元素,只需使用transform:translate(0,-50%):
HTML
<div class="container">
<div class="child"></div>
</div>
CSS
.container {
font-family: arial;
font-size: 24px;
margin: 25px;
width: 350px;
height: 200px;
outline: dashed 1px black;
/* Setup */
position: relative;
}
.child {
width: 50px;
height: 50px;
background-color: red;
/* Center vertically */
position: absolute;
top: 50%;
transform: translate(0, -50%);
}
请注意,translate(0,-50%)是translateX(0)和translateY(-50%)的简写。您还可以编写transform:translateY(-50%)以垂直居中子元素。
垂直和水平居中
使用CSS绝对定位和负边距使Div垂直和水平居中
这与上述垂直居中元素的方法非常相似。和上次一样,您必须知道要居中的元素的宽度和高度。
将父元素的位置属性设置为相对。
然后将孩子的位置属性设置为绝对,顶部设置为50%,左侧设置为50%。这只会使子元素的左上角垂直和水平居中。
要使子元素真正居中,请将负上边距设置为子元素高度的一半,将负左边距设置为子元素宽度的一半:
HTML
<div class="container">
<div class="child"></div>
</div>
CSS
.container {
font-family: arial;
font-size: 24px;
margin: 25px;
width: 350px;
height: 200px;
outline: dashed 1px black;
/* Setup */
position: relative;
}
.child {
width: 50px;
height: 50px;
background-color: red;
/* Center vertically and horizontally */
position: absolute;
top: 50%;
left: 50%;
margin: -25px 0 0 -25px; /* Apply negative top and left margins to truly center the element */
}