標籤:
1、單行垂直置中
文字在層中垂直置中vertical-align 屬性是做不到的.我們這裡有個比較巧妙的方法就是:設定height的高度與line-height的高度相同!
Example Source Code [www.52css.com]<div style="line-height:500px;height:500;"></div>
2、層水平置中
設定div的寬度小於父div的寬度,設定 margin:0 auto;,即可讓div置中。
Example Source Code [www.52css.com]#parentdiv{width: 500px;}#childdiv {width: 200px;margin:0 auto;}
3、層中的文字水平置中
在childdiv的css加上text-align:center;
Example Source Code [www.52css.com]#parentdiv{width: 500px;}#childdiv {width: 200px;margin:0 auto;text-align:center;}
4、div層垂直置中
Example Source Code [www.52css.com]<div style="width:275px;height:375px;border: solid red;"> <div style=" background:green;height: 375px; width: 275px; position: relative; display: table-cell; vertical-align: middle;"> <div style=" background:red;position:static;position:absolute\9; top: 50%;"> <div style=" background:blue;position: relative; top: -50%;"> www.52css.com </div> </div> </div></div>
5、div層垂直水平置中,英文超長換行
Example Source Code [www.52css.com]<div style="float:left;width:275px;height:375px;border: solid red;"> <div style=" height: 375px; width: 275px; position: relative; display: table-cell; vertical-align: middle;"> <div style="position:static;position:absolute\9; top: 50%;"> <div style="position: relative; top: -50%; text-align: center;"> <div style="width: 85px;WORD-WRAP: break-word;TABLE-LAYOUT: fixed;word-break:break-all;margin:0 auto;"> www.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.com </div> </div> </div> </div></div>
6、div垂直滾動
Example Source Code [www.52css.com]<div style="width: 160px; height: 260px; overflow-y: scroll; border: 1px solid;"></div>
7、垂直置中和使用text-align水平置中
Example Source Code [www.52css.com]<div style="float:left;width:275px;height:375px;border: solid red;"> <div style=" height: 375px; width: 275px; position: relative; display: table-cell; vertical-align: middle;"> <div style="position:static;position:absolute\9;top: 50%;"> <div style="position: relative; top: -50%; text-align:center;"> <div style="width: 275px;"> <div style="width: 160px;WORD-WRAP: break-word;TABLE-LAYOUT: fixed;word-break:break-all;text-align:left;"> www.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.com </div> </div> </div> </div> </div></div>
8、垂直置中和使用margin水平置中
Example Source Code [www.52css.com]<div style="float:left;width:275px;height:375px;border: solid red;"> <div style=" height: 375px; width: 275px; position: relative; display: table-cell; vertical-align: middle;"> <div style="position:static;position:absolute\9; top: 50%;"> <div style="position: relative; top: -50%; "> <div style="margin:0 auto;width: 160px;WORD-WRAP: break-word;TABLE-LAYOUT: fixed;word-break:break-all;"> www.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.com </div> </div> </div> </div></div>
CSS執行個體:水平置中和垂直置中的多種解決方案