请问该操作为什么能实现垂直居中?
.father{
position:relative;
}
.son{
position:absolute;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
}以上操作可以实现垂直居中,但是我不明白为什么会这样.
.father{
position:relative;
}
.son{
position:absolute;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
}以上操作可以实现垂直居中,但是我不明白为什么会这样.
2019-06-20
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>绝对定位居中测试</title>
<style>
.father{
background: red;
width: 100%;
height: 600px;
/*关键点*/
position: relative;
}
.father > .son{
background: blue;
width: 50%;
height: 45%;
/*关键点,为什么以下这些操作可以使盒子居中?更改或者删除其中任意一个参数都会导致异常?这里的margin:auto;起到了什么作用??*/
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>代码如上!!
举报