1. flex布局,新版本
//css
body {
	display: flex;
	justify-content: center;
	align-items: center;
}
.box {
	background: red;
	width: 200px;
	height: 200px;
}
//html
<body>
    <div class="box"></div>
</body>
  1. flex布局,老版本
//css
body {
    display: -webkit-box;
    -webkit-box-pack: center;
    -webkit-box-align: center;	
}
.box {
    background: red;
    width: 200px;
    height: 200px;
}
//html
<body>
    <div class="box"></div>
</body>
  1. 容器position为absolute
//css
.box {
  	background: red;
  	width: 200px;
  	height: 200px;
  	position: absolute;
  	left: 0;
  	right: 0;
  	top: 0;
  	bottom: 0;
  	margin: auto;
}
//或者
.box {
  	background: red;
  	width: 200px;
  	height: 200px;
  	position: absolute;
  	left: 50%;
  	right: 50%;
  	margin-top: -100px;
  	margin-left: -100px;
}
//或者
.box {
  	background: red;
  	width: 200px;
  	height: 200px;
  	position: absolute;
  	left: 50%;
  	right: 50%;
  	transform: translate(-50%,-50%);
}
//html
<body>
    <div class="box"></div>
</body>

相关文章:

  • 2021-12-09
  • 2021-11-21
  • 2021-09-05
  • 2022-12-23
  • 2021-08-05
  • 2022-02-17
  • 2021-11-10
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-06
相关资源
相似解决方案