方法一:
.box{
display:flex;
justify-content:center;
align-items: center;
}
方法二:
父元素:display:flex;
子元素:margin:auto;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<div class="box">
</div>
<script>
</script>
<style>
html{
width:100%;
height:100%;
border:solid red;
}
body{
width:100%;
height:100%;
display:flex;
border:solid;
}
.box{
margin:auto;
width:100px;
height:100px;
background:red;
}
</style>
</body>
</html>
方法三:position
.box{
width:200px;
height:200px;
background:red;
position:absolute;
left:50%;
top:50%;
margin-left:-100px;
margin-top:-100px;
}
方法四:position
.box{
width:100px;
height:100px;
background:blue;
position:absolute;
left:0;
right:0;
top:0;
bottom:0;
margin:auto;
}