CSS盒子模型box-sizing属性详解

一、box-sizing属性

box-sizing属性的三个值:
content-boxborder-boxinherit

  1. content-box
    chrome浏览器默认的盒子属性值。
    content-box的width和height从content算起,不包含border和padding。
  2. border-box
    IE浏览器默认的盒子属性值。
    border-box的width和height从border算起,包含border和padding
  3. inherit
    inherit 关键字指定一个属性应从父元素继承它的值。
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta name="author" content="lanye" />
  <meta name="description" content="HTML CSS JavaScript" />
  <meta name="generator" content="" />
  <meta name="keywords" content="HTML, CSS, JavaScript" />
  <title>Document</title>
  <!--[if lt IE 9]>
    <script src="https://cdn.bootcss.com/html5shiv/r29/html5.js"></script>
  <![endif]-->
  <style>
    /*content-box的width和height从content算起,不包含border和padding*/
    .chrome {
      width: 100px;
      height: 100px;
      background-color: green;
      border: 5px solid black;
      padding: 5px;
      box-sizing: content-box;
    }

    /*border-box的width和height从border算起,包含border和padding*/
    .ie {
      border: 5px solid black;
      box-sizing: border-box;
      width: 100px;
      height: 100px;
      background-color: red;
      padding: 5px;
      position: relative;
      bottom: 110px;
      left: 10px;
    }
  </style>
</head>

<body>
  <div class="chrome"></div>
  <div class="ie"></div>
</body>

</html>

chrome显示如下:
在这里插入图片描述
IE显示如下:
在这里插入图片描述

二、 content-box和border-box的区别

  1. 当width和height属性设置为同一个值时,content-box的内容区域大小等于border-box整个盒子的大小。content-box额外多出来border和padding区域的大小。
  2. content-box的width,height与padding,border的大小无关。border-box的width,height与padding,border的大小有关。
  • 21
    点赞
  • 138
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值