CSS盒模型

目录

一. 标准盒模型

1. 标准盒模型组成

2.块级盒子和内联级盒子

3. 盒模型存在的问题

二. 怪异盒模型(IE盒模型)

三. 总结


一. 标准盒模型

1. 标准盒模型组成

CSS盒子由content、padding、border、margin三个部分组成,我们可以将其理解为快递

content: 我们购买的商品

padding:商品和盒子之间的空隙

border:快递盒子📦

margin:快递和快递之间的距离

**给盒模型设置的背景色会平铺到非margin区域**

<!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{
      width: 100px;
      height: 100px;
      padding: 10px;
      margin: 10px;
      background-color: aqua;
      border: 2px solid black;
    }
  </style>
</head>
<body>
  <div class="box1"></div>
  
</body>
</html>

2.块级盒子和内联级盒子

常见的块级盒子比如<div>,内联级盒子如<strong>、<span>等

块级盒子特性:

  • 对各种样式都会生效,比如设置的宽度和高度
  • 独占一行
  • 在不写宽度时,块级盒子宽度和父元素相同
<!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: 100px;
      padding: 10px;
      margin: 10px;
      background-color: aqua;
      border: 2px solid black;
    }
  </style>
</head>
<body>
  <div class="box1"></div>
  
</body>
</html>
  • 所占有的形状为矩形

内联级盒子特性:

  • 在排列中不会独占一行
  • 所占有的形状不一定是矩形
  • 并不是所有样式都会生效,比如对<span>设置宽度和高度
  • 内联级盒子的宽度由内容决定
  • 设置内联标签之间由一定的距离

3. 盒模型存在的问题

  • margin-top传递问题,盒子一是盒子二的父元素,当我们给盒子二设置margin-top时,可以观察到盒子一的margin-top值也发生了变化。

<!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>
    .div1{
      width: 200px;
      height: 150px;
      background-color: red;
      /* margin-top: 10px; */
    }
    .div2{
      width: 100px;
      height: 150px;
      background-color: purple;
      margin-top: 10px;
    }
  </style>
</head>
<body>
  <div class="div1">
    <div class="div2">盒子二</div>
  </div>

我们可以通过padding或者是border来解决这个问题 ,padding设置给父元素或者是BFC,后面会专门发文章详细解析BFC

  • 设置margin数值时,会出现重叠,取二者像素的最大值
<!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>
    .div1{
      width: 200px;
      height: 150px;
      background-color: red;
      margin-bottom: 30px;
    }
    .div2{
      width: 100px;
      height: 150px;
      background-color: purple;
      margin-top: 80px;    
    }
  </style>
</head>
<body>
  <div class="div1">
    盒子一
  </div>
  <div class="div2">盒子二</div>
  
</body>
</html>

二. 怪异盒模型(IE盒模型)

IE盒模型设置的宽度是所有可见宽度,content的宽度等于设置宽度减去padding和border;对比标准盒模型设置宽度就是content区域。

  • box-sizing属性

border-box:width、height----->content 、border、padding

content-box:width、height---->content

优点:在布局时减少计算

三. 总结

在布局时尽量选择块级盒,比较便于控制,内联盒子仅仅用来做修饰

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值