2-3.CSS-盒模型

两种盒模型

1.示意图

  • content box:宽度包含内容区,叫内容盒模型
  • border boxcontent:宽度包含border,则叫border盒模型
  • 外边距、边框、内边距、内容区
  • 一般border box更好用
    在这里插入图片描述

2.实例

<body>
  <div class="content-box">content box</div>
  <div class="border-box">border box</div>
</body>
.content-box{
  margin: 25px;
  border: 5px solid red;
  padding: 15px;
  box-sizing: content-box;
  width: 100px;/*实际可见宽度为:100+15*2+5*2*/
}

.border-box{
  margin: 25px;
  border: 5px solid red;
  padding: 15px;
  box-sizing: border-box;
  width: 100px;/*实际可见宽度为:100*/
}

在这里插入图片描述

3.margin合并

  • 实例:两个孩子之间的距离依旧是30px,margin-bottom和margin-top会合并
<body>
  <div class="parent">
    <div class="child1">第一个孩子</div>
    <div class="child2">第二个孩子</div>
  </div>
</body>
.parent{
  border: 2px solid red;
}

.child1{
  border: 2px solid blue;
  margin-bottom:30px;/*下边距*/
}
.child2{
  border: 2px solid green;
  margin-top: 30px;/*上边距*/
}
  • 取消合并添加:
.child1{
  border: 2px solid blue;
  margin-bottom:30px;
  width: 100%;/*inline-block宽度为元素宽度*/
  display: inline-block;/*不合并*/
}
  • 哪些情况会合并
    父子margin合并:第一个孩子的上边距、最后一个孩子的下边距均和父级的内边距合并
    兄弟margin合并
    只有上下合并,左右不会合并
  • 如何取消合并:父子之间有padding/border挡住、或者overflow: hidden
    加个外边距:
.parent{
  border: 2px solid red;
  background: red;
  border-top: 10px solid green;/*加个帽子,则孩子的上边距和父级不合并*/
}

加个内边距:

.parent{
  border: 2px solid red;
  background: red;
  padding-top: 1px;/*内边距是透明的*/
}

4.基本单位
长度单位

  • px 像素
  • em 相对于自身 font-size的倍数(如:3em=3*font-size)
  • 百分数
  • 整数
  • rem
  • vw和vh

颜色

  • 十六进制:#FF6600 或者 #F60
  • RGB颜色:rgb(0,0,0) 或者 rgba(0,0,0,1)
    其中a表示透明度,0是全透明,1是不透明,0.5半透明
  • hsl颜色:hsl(色值,饱和度,明度)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值