CSS執行個體:水平置中和垂直置中的多種解決方案

來源:互聯網
上載者:User

標籤:

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執行個體:水平置中和垂直置中的多種解決方案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.