<body>
<div style="position:absolute; height:100%; width:100%;">
<h1 style="text-align:center;">Text</h1>
</div>
</body>
我如何在div标签内垂直居中h1标签,无论div元素的高度如何? 也就是说,如果用户更改浏览器高度,我希望h1根据新高度在中心垂直对齐。
谢谢。
<body>
<div style="position:absolute; height:100%; width:100%;">
<h1 style="text-align:center;">Text</h1>
</div>
</body>
我如何在div标签内垂直居中h1标签,无论div元素的高度如何? 也就是说,如果用户更改浏览器高度,我希望h1根据新高度在中心垂直对齐。
谢谢。
我遇到的最好的解决方案是利用 display
属性,将包装元素设置为 table
,以允许在要居中的元素上使用 vertical-align:middle
:
查看这个 工作示例!
HTML
<body>
<div>
<h1>Text</h1>
</div>
</body>
CSS
body {width: 100%; height: 100%;} /* this is just to "view" the div height...*/
div {
position:absolute; height:100%; width:100%;
display: table;
}
h1 {
display: table-cell;
vertical-align: middle;
text-align:center;
}
Windows XP Professional 版本 2002 Service Pack 3
Windows 7 Home Edition Service Pack 1
Linux Ubuntu 12.04
<h1>
元素之前插入一个 <span>
标签:http://jsfiddle.net/Wexcode/axRxE/
HTML:
<div>
<span></span><h1>Text</h1>
</div>
CSS:
div { text-align: center; /* horizontally center */ }
div span {
height: 100%;
vertical-align: middle;
display: inline-block; }
div h1 {
vertical-align: middle;
display: inline-block; }
将此技术扩展为垂直对齐到浏览器高度:http://jsfiddle.net/Wexcode/axRxE/1/
<span>
和<h1>
标签之间添加的空格会影响垂直对齐。 - Wex:before
伪元素。http://jsfiddle.net/6kk8yyxk/1/ - hakatashi我有三行最简单的CSS代码,将会让你大吃一惊,并且你会想到“为什么我一开始没想到这个”。在你想要垂直居中定位的元素上使用此代码,并在父容器上添加100%的高度。
position: relative;
top: 50%;
transform: translateY(-50%);
该位置可以是相对、绝对或固定的。
在2012年,被接受的答案是正确/最佳选项。
向前跳8年,弹性盒子(flex boxes)现在已被每个主流浏览器(IE10+)支持:
display: flex;
align-items: center;
flex-direction: column; /* To also center horizontally, change to `rows` or remove */
注意:如果您关心 IE10,需要包含前缀版本 -ms-
,请参考上面的链接。
top: 50%
和transform: translateY(50%)
或表格。 - Dani Amsalem<body>
<div style="position:absolute; height:100%; width:100%;">
<h1 style="text-align:center; height:20px; position:relative; top:50%; margin-top:-10px;">Text</h1>
</div>
</body>
<h1>
的高度,并给出一半高度的负边距。 - Zoltan Toth使用CSS2 + CSS3,无需任何Javascript,有一种跨(桌面)浏览器解决方案来实现此功能。
适用于以下浏览器:
文档:CSS中的垂直居中 具有未知高度的明确解决方案:
http://www.jakpsatweb.cz/css/css-vertical-center-solution.html
干净的jsFiddle示例: http://jsfiddle.net/WYgsP/
<div class="outerBox">
<div class="helper">
<div class="boxWithUnknownHeight">
any text<br>
any height<br>
any content, for example generated from DB<br>
everything is vertically centered
</div>
</div>
</div>
CSS
.outerBox {
display: table;
height: 400px;
#position: relative; /* ie hack */
overflow: hidden;
border: 1px solid red;
}
.helper {
#position: absolute; /* ie hack */
#top: 50%; /* ie hack */
display: table-cell;
vertical-align: middle;
}
.boxWithUnknownHeight {
#position: relative; /* ie hack */
#top: -50%;
border: 1px solid green
}
即使我通过Firebug等方式添加文本和换行符,它也可以正常工作。
为了保持你的CSS不受无效的CSS Hack影响,我建议您使用条件注释并创建一个包含特定浏览器代码的单独CSS文件。
关于未知高度的垂直居中是如何工作的以及原因:详细描述
其他解决方案使用display: table
和/或display: table-cell
以及绝对定位,在此查看。
display: table
或类似的东西。<table style="width: 100%; height: 100%;">
<tr>
<td>
<!-- Whatever you want vertically and horizontally centered -->
</td>
</tr>
</table>
使用同样的方法,您可以为自己的情况做类似的事情:
<div id="container-div" style="position: relative">
<div id="random-content-that-changes-container-height-and-width" style="height: 600px; width: 400px;">
<div style="position: absolute; top: 0; right: 0; left: 0; bottom: 0">
<table style="width: 100%; height: 100%;">
<tr>
<td>
<!-- Whatever you want vertically and horizontally centered -->
</td>
</tr>
</table>
</div>
</div>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
body {
background-color: #0c0c0c;
}
.object {
background-color: #666666;
height: 350px;
width: 600px;
}
.verticalCenter {
width:600px;
height:350px;
position:absolute;
left:50%;
top:50%;
margin:-175px 0 0 -300px;
}
-->
</style>
</head>
<body>
<div class="verticalCenter">
<div class="object">cetnered</div>
</div>
</body>
</html>
您必须在 .verticalCenter 类中设置高度和宽度,与要居中的对象(div、flash、图像、文本)相同。边距必须是这些高度和宽度的一半。
如果您动态更改该对象,则我认为没有解决方案。除非,也许可以使用 JavaScript。
display:table-cell
并选择垂直居中对齐方式。<div id="vertical">
<p>this text is vertically centered. It's long enough to wrap, and should work for any size chunk of content.</p>
<p>Heck, it even works with multiple items in the middle.</p>
</div>
CSS:
#vertical {
display:table-cell;
vertical-align:middle;
height: 500px;
width: 300px;
}
JSFiddle: http://jsfiddle.net/6Re3E/1/
JSFiddle:http://jsfiddle.net/6Re3E/1/div
在这种情况下不是一个合适的选择。我的建议是使用table
并在其td
上应用vertical-align
样式。