css3 - div 水平垂直置中
問題描述
想請問一下如何讓p水平垂直置中? 就是不管中間的p大小 他能隨著瀏覽器大小自動置中對齊???
問題解答
回答1:每種寫法都會根據(jù)你的布局進(jìn)行一些小小變化。常用margin水平方法:
p { width:200px; margin:0 auto;}
1/2寬高的margin,50%的left、top方法:
p { Width:500px ; height:300px; Margin: -150px 0 0 -250px; position:relative; background-color:pink; left:50%; top:50%; }
LTRB值為0的方法:
p { width: 400px; height: 300px; margin:auto; position: absolute; left: 0; top: 0; right: 0; bottom: 0;}
transform方法
p { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);}
帶文本元素的話,讓line-height = height:
p { height:30px; line-height:30px; text-align:center;}
flex彈性盒子布局居中,給父元素添加:
p { display: flex; flex-flow: row wrap; width: 408px; align-items: center; justify-content: center;} 回答2:
css3方法可以用flex,給父級添加
.father { display: flex; justify-content: center; align-items: center;}回答3:
p { position:absolute; top:50%; left:50%; transform:translate(-50%,-50%);}回答4:
父元素設(shè)置為relative定位,本元素設(shè)置為絕對定位,然后通過top和translateY使其垂直居中
position: absolute; top: 50%; transform: translateY(-50%);回答5:
margin:auto
回答6:http://web.jobbole.com/83384/
回答7:外面的p{
position: relative;
}中間的p{
position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);
}
相關(guān)文章:
1. docker 下面創(chuàng)建的IMAGE 他們的 ID 一樣?這個是怎么回事????2. 在應(yīng)用配置文件 app.php 中找不到’route_check_cache’配置項(xiàng)3. html按鍵開關(guān)如何提交我想需要的值到數(shù)據(jù)庫4. mysql取模分表與分表5. gvim - 誰有vim里CSS的Indent文件, 能縮進(jìn)@media里面的6. HTML 5輸入框只能輸入漢字、字母、數(shù)字、標(biāo)點(diǎn)符號?正則如何寫?7. 跟著課件一模一樣的操作使用tp6,出現(xiàn)了錯誤8. PHP類屬性聲明?9. objective-c - ios 怎么實(shí)現(xiàn)微信聯(lián)系列表 最好是swift10. java - 安卓接入微信登錄,onCreate不會執(zhí)行
