效果
实现
给父盒子宽高和flex,子盒子margin:auto
代码
<!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>
* {
margin: 0;
padding: 0;
}
html,
body {
width: 100%; //
height: 100%; //
display: flex; //
}
.con {
width: 300px;
height: 300px;
background-color: skyblue;
margin: auto; //
}
</style>
</head>
<body>
<div class="con">
<h1>居中对齐</h1>
</div>
</body>
</html>