元素水平垂直居中

  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>
posted @ 2019-11-20 22:51  甜珊贝奇  阅读(162)  评论(0编辑  收藏  举报