页面中元素居中效果分为三种情况,1.水平居中,2.垂直居中,3.水平垂直居中

下面代码视图层统一代码结构:

<div class="father">
	<div class="son"></div>
</div>

1.水平居中:

应用场景:整体网站居中,文字排版居中

css代码:

.father{
	width: 100%;
}
.son{
	width:800px;
	height: 60px;
	background: #ccc;
	margin: auto;
}

2.垂直居中:

css代码:

.father{
	width: 100%;
	height:300px;
	writing-mode: vertical-lr;
	background: orange;
}
.son{
	width:80%;
	height: 100px;
	background: #f2f2f2;
	margin: auto;
}

3.水平居中:

.father{
	position:absolute;
	top: 0;left: 0;bottom: 0;right: 0;
	background: rgba(0,0,0,.5);
}
.son{
	position: absolute;
	top: 0;left: 0;bottom: 0;right: 0;
	width:300px;
	height: 300px;
	background: #f2f2f2;
	margin: auto;
}

相关文章:

  • 2021-12-26
  • 2022-12-23
  • 2021-12-10
  • 2021-12-13
  • 2021-10-14
猜你喜欢
  • 2021-12-05
  • 2022-01-09
  • 2021-08-31
  • 2021-12-04
  • 2021-05-20
相关资源
相似解决方案