CSS3中盒子的box-sizing屬性

來源:互聯網
上載者:User

標籤:

box-sizing

屬性

box-sizing 屬性用來改變預設的 CSS盒模型 對元素寬高的計算方式。這個屬性可以用於類比那些非正確支援標準盒模型的瀏覽器的表現。

box-sizing:content-box(default) | border-box ;

 

content-box

    預設值,標準盒模型。 width與 height 只包括內容的寬和高, 不包括邊框(border),內邊距(padding),外邊距(margin)。

    注意: 內邊距, 邊框 & 外邊距 都在這個盒子的外部。 

    比如. 如果 .box {width: 350px}; 而且 {border: 10px solid black;} 那麼在瀏覽器中的渲染的實際寬度將是370px;

    
    尺寸計算公式:width = 內容的寬度,height = 內容的高度。寬度和高度都不包含內容的邊框(border)和內邊距(padding)。

border-box 

    width 與 height 包括內邊距(padding)與邊框(border),不包括外邊距(margin)。

    這是IE 怪異模式(Quirks mode)使用的 盒模型 。注意:這個時候內邊距和邊框將會包括在盒子中。

    比如  .box {width: 350px; border: 10px solid black;} 瀏覽器渲染出的寬度將是350px.

    如果計算後的最內部的內容寬度為負值,都會被計算成0,內容還在,所以不可能通過 border-box來隱藏元素。

      
    尺寸計算公式:width = border + padding + 內容的寬度,height = border + padding + 內容的高度。

 Demo

<!DOCTYPE html><html><head lang="en">    <meta charset="UTF-8">    <title></title>    <style>        div{            /*計算height,width時包含了padding border的值*/            -webkit-box-sizing: border-box;            -moz-box-sizing: border-box;            box-sizing: border-box;            /*計算height,width時沒有包含padding border*/            /*-webkit-box-sizing: content-box;*/            /*-moz-box-sizing: content-box;*/            /*box-sizing: content-box;*/            /*計算height,width時包含了padding 的值*!/*/            /*-webkit-box-sizing: padding-box;*/            /*-moz-box-sizing: padding-box;*/            /*box-sizing: padding-box;*/            background-color: gray;            border: 10px orange solid;            width: 500px;            height: 300px;        }    </style></head><script></script><body>    <div>Demo</div></body></html>

 

Link...

https://developer.mozilla.org/zh-CN/docs/Web/CSS/box-sizing

CSS3中盒子的box-sizing屬性

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.