如何理解盒子模型 CSS3的box-sizing的使用

​
1. 盒子模型 Box Model 是指在Web页面中 每个元素都是一个矩形的盒子
  1. box-width-height = 内容区域content + 内边距区域padding + 边框区域border + 外边距区域margin 

2. box-sizing 定义盒子模型的类型
  1. content-box(default): width = content
  2. border-box(global): width = content + padding + border
  3. inherit: box-sizing属性的值应该从父元素继承

* {
  // 全局设置为border-box 这样设置一个div的width就很接近盒子的实际大小box-width-height
  box-sizing: border-box;
  // 去除某些标签自带的padding和margin
  padding: 0;
  margin: 0;
}
.box {
  box-sizing: border-box;
  width: 300px;
  height: 200px;
  padding: 20px;
  border: 5px solid black;
  margin: 10px;
}
content-width = 240px = 300px - 2 * 20px + 5px
content-height = 160px = 200px - 2 * 20px + 5px
box-width = 320px = 300px + 2 * 10px
box-height = 220px = 200px + 2 * 10px 

​

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值