<分区>
这是源代码:
<div id = "outer">
<div id="top">
....
</div>
<div id="inner">
....
</div>
<div id="bottom">
....
</div>
</div>
如何让 div(id inner)水平和垂直居中?
水平居中可以,但是垂直居中不行...
<分区>
这是源代码:
<div id = "outer">
<div id="top">
....
</div>
<div id="inner">
....
</div>
<div id="bottom">
....
</div>
</div>
如何让 div(id inner)水平和垂直居中?
水平居中可以,但是垂直居中不行...
最佳答案
如果您知道内部 div 的尺寸,则可以使用 CSS。
#outer {
position: relative;
}
#inner {
position: absolute;
width: 200px;
height: 200px;
left: 50%;
top: 50%;
margin-top: -100px;
margin-left: -100px;
}
还有其他选项使用 display: table-cell
和 vertical-align: middle
等。
其他选项包括 JavaScript 来动态确定内部 div 的尺寸并像以前的答案一样设置负边距。
关于html - 如何水平和垂直居中一个div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4325643/