在CSS中,实现上下居中对齐是一个常见的问题。本文将介绍几种方法来解决它。一、使用line-height属性一种简单的方法是为容器设置一个与容器高度相等的line-height值。这样可以使容器内的文
<div class="center">这里是文本</div>
.center { height: 100px; line-height: 100px; }
<div class="center"> <p>这里是文本</p> </div>
.center { display: flex; justify-content: center; align-items: center; height: 100px; }
<div class="center"> <p>这里是文本</p> </div>
.center { position: relative; height: 100px; } .center p { position: absolute; top: 50%; transform: translateY(-50%); }
粉丝
0
关注
0
收藏
0