块级盒子内的块级盒子如何实现水平居中(pink老师p371)
利用定位:子绝父相
内部盒子往下,右移动50%
然后内部盒子再往上,左移动自身高度的一半
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.out{
position: relative;
width: 300px;
height: 300px;
background-color: pink;
}
.inner{
position: absolute;
width: 100px;
height: 100px;
background-color: purple;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="out">
<div class="inner">
</div>
</div>
</body>
</html>
效果: