怎么设置中间的文字能随着长度的变化而自动垂直居中

https://img4.mukewang.com/5c08950a00017e1b06280414.jpg

喵喔喔
浏览 787回答 1
1回答

慕码人8056858

直接设置上下相同的padding就可以了,&nbsp;可以在任何浏览器上运行,并且代码很简单,只不过这种方法应用的前提就是容器的高度必须是可伸缩的。。而高度变化正好是题主的要求。demo:<!DOCTYPE html><html><head>&nbsp; &nbsp; &nbsp;<meta charset="UTF-8">&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; &nbsp; <meta http-equiv="X-UA-Compatible" content="ie=edge">&nbsp; &nbsp; <title>Document</title>&nbsp; &nbsp; <title> 多行文字实现垂直居中 </title>&nbsp; &nbsp; <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />&nbsp; &nbsp; <style type="text/css">&nbsp; &nbsp; &nbsp; &nbsp; .tx-verti-center{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; padding: 25px;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; border: 1px solid #FF0099;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width: 760px;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </style></head><body>&nbsp; &nbsp; <div class="tx-verti-center">&nbsp; &nbsp; &nbsp; &nbsp; 现在我们要使这段文字垂直居中显示!阿萨德黄金卡撕掉会阿萨德卡时间段卡拉胶考虑到静安寺来看待&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 阿萨德黄金卡撕掉会阿萨德卡时间段卡拉胶考虑到静安寺来看待&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 阿萨德黄金卡撕掉会阿萨德卡时间段卡拉胶考虑到静安寺来看待&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; </div>&nbsp; &nbsp; <div&nbsp; class="tx-verti-center">&nbsp; &nbsp; &nbsp; &nbsp; 现在我们要使这段文字垂直居中显示!阿萨德黄金卡撕掉会阿萨德卡时间段卡拉胶考虑到静安寺来看待&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; </div></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript