PC端盒子居中方式一
<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>盒子完全居中方式一</title>
<style>
* {
margin: 0;
padding: 0;
}
div {
width: 100px;
height: 200px;
background: rgb(189, 243, 209);
position: fixed;
top: 50%;
left: 50%;
margin-top: -100px;
margin-left: -50px;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
PC端盒子居中方式二
<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>盒子完全居中方式二</title>
<style>
* {
margin: 0;
padding: 0;
}
div {
width: 100px;
height: 200px;
background: rgb(189, 243, 209);
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
PC端盒子居中方式三
<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>盒子完全居中方式三</title>
<style>
* {
margin: 0;
padding: 0;
}
div {
width: 100px;
height: 200px;
background: rgb(189, 243, 209);
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<div></div>
</body>
</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>移动端盒子居中</title>
<style>
* {
margin: 0;
padding: 0;
}
.wrap {
width: 300px;
height: 300px;
background: rgb(237, 255, 155);
display: flex;
justify-content: center;
align-items: center;
}
.wrap div {
width: 100px;
height: 100px;
background: rgb(255, 203, 227);
}
</style>
</head>
<body>
<div class="wrap">
<div></div>
</div>
</body>
</html>
效果图