900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > (css)div中内容垂直居中

(css)div中内容垂直居中

时间:2022-08-19 17:25:03

相关推荐

(css)div中内容垂直居中

目录

一、常用单行文字居中(line-height/padding)

二、模拟表格方式 ***

三、css3 transform

四、flex布局

一、常用单行文字居中(line-height/padding)

p{ height:30px; line-height:30px; width:100px; overflow:hidden; }

p { padding:20px 0; }

二、模拟表格方式 ***

父组件设置 display: table; 子组件设置 display: table-cell; vertical-align: middle

<div style="display: table"><div style="display: table-cell; vertical-align: middle"><p>测试垂直居中效果测试垂直居中效果</p><p>测试垂直居中效果测试垂直居中效果</p></div></div>

三、css3 transform

.content{position: relative;top:50%;transform:translateY(-50%);}

四、flex布局

.flex{/*flex 布局*/display: flex;/*实现垂直居中*/align-items: center;/*实现水平居中*/justify-content: center;text-align: justify;width:200px;height:200px;background: #000;margin:0 auto;color:#fff;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。