什么是盒子模型?IE盒模型与Chrome盒模型的区别?box-sizing的用法

所有的HTML元素可以看作盒子,它包括四部分组成。每个元素在页面中占位大小 = content + padding + margin + border。

 

1)Margin(外边距)清除边框外的区域,外边距是透明的。

2)Border(边框)围绕在内边距和内容外的边框。

3)Padding(内边距)清除内容周围的区域,内边距是透明的。

4)Content(内容)盒子的内容,显示文本和图像。

 W3C盒模型和怪异盒模型的区别?

标准盒模型(W3C)内容大小等于content大小padding和border不被包含在定义的width和height之内。盒子的实际宽度=设置的width+padding+border

IE盒模型(怪异盒模型)内容大小等于content + padding + border的总和。padding和border被包含在定义的width和height之内。盒子的实际宽度=设置的width(padding和border不会影响实际宽度)

 

box-sizing可以设置盒子的类型

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

box-sizing:border-box,为IE盒模型(怪异盒模型)。

box-sizing:content-box,为标准盒模型(W3C盒模型)例如:Firefox/Chrome。

box-sizing:inherit,规定应从父元素继承 box-sizing 属性的值

一般默认浏览器使用标准盒模型。

例子:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
      .box1{
        height:150px;
        width:150px;
        padding: 50px;
        margin: 100px;
        border: solid 5px green;
        background-color: brown;
        box-sizing: content-box; 
      }
      .box2{
        height:150px;
        width:150px;
        padding: 50px;
        margin: 100px;
        border: solid 5px green;
        background-color: black;
        box-sizing: border-box;
      }
      .box3{
        height:50px;
        width:50px;    
        padding: 10px;
        border:1px solid red;
        background-color: green;
        box-sizing: inherit;
      }

      .box4{
        height:50px;
        width:50px;    
        padding: 10px;
        border:1px solid red;
        background-color: green;
        box-sizing: inherit;
      }
    </style>
</head>
<body>
    <div class="box1">
        <div class="box4"></div>
    </div>
    <div class="box2">
        <div class="box3"></div>
    </div>
</body>
</html>

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值