四中居中方法

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"/>
<title>Demo</title>
<style>

.box {
position:relative;
500px;
height:300px;
background:gray;
margin:20px;
}

.two > div{
position:absolute;
top:calc(50% - 50px);
left:calc(50% - 50px);
100px;
height:100px;
background:blue;
}

.three > div{
position:absolute;
top:50%;
left:50%;
100px;
height:100px;
transform:translate(-50%,-50%);
background:green;
}

.four > div{
position:absolute;
top:50%;
left:50%;
100px;
height:100px;
margin-left:-50px;
margin-top:-50px;
background:red;
}

.five{
display:flex;

}

.five > div{
100px;
height:100px;
margin:auto;
background:black;
color:white;
}

</style>
</head>
<body>
<div class="box two">
<div></div>
</div>

<div class="box three">
<div></div>
</div>

<div class="box four">
<div></div>
</div>

<div class="box five">
<div></div>
</div>
</body>
</html>
原文地址:https://www.cnblogs.com/landofpromise/p/7403115.html