html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
* {
padding: 0;
margin: 0;
}
body,
html {
width: 100%;
height: 100%;
}
.wrap1 {
position: relative;
width: 100%;
height: 100%;
background: gray;
}
.fixed-box {
position: absolute;
left: 50%;
top: 50%;
margin-top: -250px;
margin-left: -200px;
height: 500px;
background: red;
width: 400px;
}
/* 未知宽高上下居中 */
.auto-box {
background: pink;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.wrap2 {
display: table;
width: 600px;
height: 600px;
}
.wrap2 .box {
display: table-cell;
vertical-align: middle;
width: 20px;
text-align: center;
height: 20px;
background: red;
}
.wrap3 .box {
display: flex;
justify-content: center;
align-items: center;
height: 400px;
width: 400px;
background: yellow;
}
/* // 1 */
.wraper {
position: relative;
.box {
position: absolute;
top: 50%;
left: 50%;
width: 100px;
height: 100px;
margin: -50px 0 0 -50px;
}
}
/* // 2 */
.wraper {
position: relative;
.box {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
}
/* // 3 */
.wraper {
.box {
display: flex;
justify-content:center;
align-items: center;
height: 100px;
}
}
/* // 4 */
.wraper {
display: table;
.box {
display: table-cell;
vertical-align: middle;
}
}
/* <!-- 5 grid --> */
.wp {
display: grid;
}
.box {
align-self: center;
justify-self: center;
}
</style>
<body>
<div class="wrap1">
<div class="fixed-box">
123
</div>
<div class="auto-box">
transform
</div>
</div>
<div class="wrap2">
<div class="box">
table
</div>
</div>
<div class="wrap3">
<div class="box">
asdfasdf
</div>
</div>
</body>
</html>