CSS⽂字居中显⽰的⼏种⽅式1、利⽤line-height和vertical-align
html
<div class="box">
<span>测试⽂字</span>
</div>
css
.box{
width: 200px;
height: 200px;
overflow: hidden;
background: #ccc;
text-align: center;
}
.box span{
vertical-align: middle;
line-height: 200px;
}
2、利⽤display:table-cell实现⽔平垂直居中显⽰
html
<div class="table">
<span class="cell">测试⽂字测试⽂字测试⽂字测试⽂字</span>
</div>
css
.table{
display: table;
}
.cell{
css设置文字垂直居中display: table-cell;
vertical-align: middle;
text-align: center;
}
3、利⽤定位⽅式position+transform实现⽔平垂直居中显⽰
html
<div class="box">
<span>测试⽂字测试⽂字测试⽂字测试⽂字</span>
</div>
css
.box{
position: relative;
}
.box span{
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
4、利⽤display:flex
html
<div class="flex">
<span>测试⽂字测试⽂字测试⽂字测试⽂字</span> </div>
css
display:flex;
flex-direction: row;
justify-content: space-around;
align-items: center;
5、利⽤display:box
html
<div class="box">
<span>测试⽂字测试⽂字测试⽂字测试⽂字</span> </div>
css
display: -webkit-box;
-webkit-box-orient: horizontal;
-webkit-box-pack: center;
-webkit-box-align: center;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。