水平垂直居中方式
方式一
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 300px;
height: 200px;
background: skyblue;
display: flex;
justify-content: center;
align-items: center;
}
.box div{
width:100px;
height:100px;
background:pink;
}
</style>
</head>
<body>
<div class="box">
<div></div>
</div>
</body>
</html>
方式二
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 300px;
height: 200px;
background: skyblue;
position: relative;
}
.box div{
width:200px;
height:50px;
background:pink;
position: absolute;
left:50%;
top:50%;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="box">
<div></div>
</div>
</body>
</html>
方式三
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 300px;
height: 200px;
background: skyblue;
position: relative;
display: flex;
}
.box div{
width:200px;
height:50px;
background:pink;
margin:auto;
}
</style>
</head>
<body>
<div class="box">
<div></div>
</div>
</body>
</html>
方式四
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 300px;
height: 200px;
background: skyblue;
position: relative;
}
.box div{
width:200px;
height:50px;
background:pink;
position: absolute;
left:0;
top:0;
right:0;
bottom:0;
margin:auto;
}
</style>
</head>
<body>
<div class="box">
<div></div>
</div>
</body>
</html>