css3盒模型

來源:互聯網
上載者:User

標籤:

css2.1盒模型:

     當你定義盒子的寬高後;如果添加padding和border值後盒子的寬高會被撐大

     盒子的高度=定義的高度+(padding-top + padding-bottom)+(border-top + border-bottom);

     盒子的寬度=定義的寬度+(padding-left+ padding-right)+(border-left+ border-right);

css3.0盒模型:

       當你定義盒子高度後;如果添加padding和border值後盒子大小不會改變,他會向內容區收縮。

       盒子的高度=你定義的高度;盒子的寬度度=你定義的寬度;

用法:

   box-sizing:用來控制元素的盒模型解析模式

    box-sizing:content-box | border-box | inherit;

    預設值是content-box:維持W3C的標準盒模型 也就是css3.0以前的版本布局

    border-box:重新定義盒模型組成的模式。

    inherit:使元素繼承父元素的盒模型模式。

    寫法(考慮相容): 

         -moz-box-sizing: border-box;
         --webkit-box-sizing: border-box;
         -moz-box-sizing: border-box;
         -ms-box-sizing:border-box;
         box-sizing: border-box;

執行個體:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>css3布局</title>
</head>
<style>
*{margin:0;padding:0;}
.wrapper{
width:960px;
margin:0 auto;
color:#fff;
background:#cccccc;
text-align:center;
-moz-box-sizing: border-box;
--webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-ms-box-sizing:border-box;
box-sizing: border-box;
}
.header{
background:#38382e;
margin-bottom: 10px;
border:10px solid red;
padding:10px;
width:100%;height:100px;
box-sizing:inherit;
}
.sidebar{
float:left;
width:220px;
margin:0px 20px 10px 0px;
height:300px;
background:#5d33cf;
border:10px solid red;
padding:10px;
box-sizing:inherit;
}
.content{
float:left;
width:720px;
margin-bottom: 10px;
height:300px;
background:#c8ca30;
border:10px solid red;
padding:10px;
box-sizing:inherit;
}
.footer{
clear:both;
width:100%;
height:100px;
background:#cc4ad5;
border:10px solid red;
padding:10px;
box-sizing:inherit;
}
</style>
<body>
<div class="wrapper">
<div class="header">頁首</div>
<div class="sidebar">左側邊欄</div>
<div class="content">主內容</div>
<div class="footer">頁尾</div>
</div>
</body>
</html>

效果:

    

css3盒模型

聯繫我們

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