1. 将父元素设置为块级表格来显示display:table;子元素设置为表格单元格 来显示,设置vertical-align: middle。
<div style="margin: 20px auto;width: 400px;height: 400px;background-color: aqua;overflow: hidden;">
<div style="display: table;margin: 100px auto;width: 200px;height: 200px;text-align: center;background-color: antiquewhite;">
<span style="display: table-cell;vertical-align: middle;">你好你好你好你好你好你好你好你好你好你好你好</span>
</div>
</div>
2. 先对父元素设置高度和行高(相等)即height=line-height,子元素设置 为行内块元素display:inline-block,设置vertical-align: middle,自定义line-height 属性的值如18px;
<div style="margin: 20px auto;width: 400px;height: 400px;background-color: aqua;overflow: hidden;">
<div style="display: table;margin: 100px auto;width: 200px;height: 200px;text-align: center;background-color: antiquewhite;">
<span style="display: table-cell;vertical-align: middle;">你好你好你好你好你好你好你好你好你好你好你好</span>
</div>
</div>
3. 先将子元素设置为行内块元素display:inline-block,再对子元素设置相对 定位position:relative,之后利用top:50%和transform:translateY(-50%)设置 平移;
<div style="margin: 20px auto;width: 400px;height: 400px;background-color: rgb(254, 164, 181);overflow: hidden;">
<div style="margin: 100px auto;width: 200px;height: 200px;text-align: center;background-color: antiquewhite;">
<span style="display: inline-block;position: relative;top: 50%;transform: translateY(-50%);">你好你好你好你好你好你好你好你好你好你好你好</span>
</div>
</div>
4. 利用flex弹性布局,将父元素设置为弹性盒子,并设置父元素align-items: center,即定义flex子项在flex容器侧轴方向上的对齐方式为center。
<div style="margin: 20px auto;width: 400px;height: 400px;background-color: rgb(254, 229, 164);overflow: hidden;">
<div style="display: flex;align-items: center;margin: 100px auto;width: 200px;height: 200px;text-align: center;background-color: antiquewhite;">
<span>你好你好你好你好你好你好你好你好你好你好你好</span>
</div>
</div>
效果: