css置中常見寫法

來源:互聯網
上載者:User

標籤:20px   css   ddl   code   splay   垂直置中   firefox   垂直   利用   

1、最常見的水平置中寫法

 1  .box-center{ 2         width: 100%; 
4
background-color: darkgrey; 5 height: 100px; 6 } 7 .box-child{ 8 width: 30%; 9 height: 50px;10 background-color: #00a5e0;11 margin: auto;12 }13 <div class="box-center">14 <div class="box-child"></div>15 </div>

2、利用css3的新特性,做水平置中

    .box-center1 {        width: 100%;        background-color: darkgrey;        height: 100px;        /* Firefox */        display: -moz-box;        -moz-box-pack: center;        /* Safari, Chrome, and Opera */        display: -webkit-box;        -webkit-box-pack: center;        /* w3c*/        display: box;        box-pack: center;    }    .box-child1 {        width: 30%;        height: 50px;        background-color: #00a5e0;    }<div class="box-center1">    <div class="box-child1">    </div></div>

3、垂直置中

<style>    .box-center2 {        line-height: 200px;        width: 100%;        height: 200px;        background-color: darkgrey    }    .box-child2 {        display: inline-block;        line-height: 20px;        padding: 20px;        background-color: #cd0000;        color: #fff;        vertical-align: middle;        width: -webkit-fill-available;        width: -moz-fill-available;        width: -moz-available; /* FireFox目前這個生效 */        width: fill-available;    }</style><div class="box-center2">    <div class="box-child2">    </div></div>

4、利用css3新特性,垂直置中

<style>    .box-center3 {        line-height: 200px;        width: 100%;        height: 200px;        background-color: darkgrey;        /* Firefox */        display: -moz-box;        -moz-box-align: center;        /* Safari, Chrome, and Opera */        display: -webkit-box;        -webkit-box-align: center;        /*w3c */        display: box;        box-align: center;    }    .box-clild3{        background-color: #cd0000;        color: #fff;        width: 100%;        height: 50px;    }</style><div class="box-center3">    <div class="box-clild3"></div></div>

5、水平垂直置中:可以利用2和4綜合得出絕對置中效果,1和3綜合需要在外層div加上text-align:center

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.