提供两种方法line-height和flex布局方法
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>文字固定容器居中</title>
<style type="text/css">
.box1 {
background-color: #ccc;
width: 300px;
height: 800px;
margin: 100px auto;
line-height: 800px;
}
.box1 span {
display: inline-block;
line-height: 20px;
vertical-align: middle;
}
.box {
display: flex;
width: 500px;
height: 800px;
margin: 50px auto;
border: 2px solid #000;
align-items: center;
/*垂直轴居中*/
}
.box span {
/*span是另一个flex布局容器,它本身将自适应填满除p元素外的宽度*/
flex: 1;
display: flex;
justify-content: center;
/*水平轴居中*/
}
</style>
</head>
<body>
<!--line-height-->
<div class="box1">
<span>多行居中测试多行居中测试多行居中
测试多行居中测试多行居中测试多行居中测试多行居中测
试多行居中测试多行居中测试多行居中测试多行居中测试多行居中
</span>
</div>
<!--flex布局-->
<div class="box">
<span>span多行居中测试span多行居中测试span多行居中测试span多行居中测试span多行居中测试span多行居中测试span多行居中测试span多行居中测试span多行居中测试span多行居中测试span多行居中测试span多行居中测试</span>
</div>
</body>
</html>