margin的传递和折叠

margin的传递和折叠

一、margin的上下传递(一般发生在父子元素之间)

  • margin-top传递

    • 如果块级元素的顶部线和父元素的顶部线重叠,那么这个块级元素的margin-top值会传递给父元素

    • 为什么会产生传递呢?

    • 原因很简单:w3c的人设计的时候可能考虑到既然父子是近挨着的,子元素没必要设置外边距,给父元素设置更适合

    • 在这里插入图片描述

    • 解决方案:

      • 1、给父元素设置overflow:auto,触发BFC
      • 2、给父元素设置padding-top、padding-bottom
      • 3、给父元素设置border
    • <!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>
          .box {
            width: 300px;
            height: 300px;
            background-color: red;
      
            overflow: auto;
            /* padding-top: 1px; */
            /* border: 1px solid rgba(0, 0, 0, 0); */
          }
      
          .content {
            width: 100px;
            height: 100px;
            background-color: blue;
      
            margin-top: 100px;
          }
        </style>
      
      </head>
      
      <body>
      
        <div class="box">
          <div class="content"></div>
        </div>
      
      </body>
      
      </html>
      
  • margin-bottom传递 (了解)

    • 如果块级元素的底部线和父元素的底部线重叠,那么这个块级元素的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>
          .box {
            width: 300px;
            height: 100px;
            background-color: red;
      
            /* overflow: auto; */
            /* padding-top: 1px; */
            /* border: 1px solid rgba(0, 0, 0, 0); */
          }
      
          .content {
            width: 100px;
            height: 100px;
            background-color: blue;
      
            margin-bottom: 300px;
          }
        </style>
      
      </head>
      
      <body>
      
        <div class="box">
          <div class="content"></div>
        </div>
        <p>哈哈哈哈哈</p>
      
      </body>
      
      </html>
      
    • 如果父元素的高度不是auto的话,不会发生传递现象

    • 如果父元素的不设置高度或者高度为auto,就会发生传递

    • 高度不设置默认是auto

    • 在这里插入图片描述

    • 在这里插入图片描述

    • <!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>
          .box {
            width: 300px;
            height: auto;
            background-color: red;
      
            /* overflow: auto; */
            /* padding-top: 1px; */
            /* border: 1px solid rgba(0, 0, 0, 0); */
          }
      
          .content {
            width: 100px;
            height: 100px;
            background-color: blue;
      
            margin-bottom: 300px;
          }
        </style>
      
      </head>
      
      <body>
      
        <div class="box">
          <div class="content"></div>
        </div>
        <p>哈哈哈哈哈</p>
      
      </body>
      
      </html>
      

二、margin上下折叠

  • 垂直方向上相邻的2个margin(margin-top、margin-bottom)有可能会合并为1个margin,这种现象叫做collapse(折叠)

  • 在这里插入图片描述

  • <!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: 300px;
          height: 300px;
          background-color: red;
    
          margin-bottom: 30px;
        }
    
        .box2 {
          width: 300px;
          height: 300px;
          background-color: #0f0;
    
          margin-top: 20px;
        }
      </style>
    </head>
    
    <body>
    
      <div class="box1"></div>
      <div class="box2"></div>
    
    </body>
    
    </html>
    
  • 水平方向上的margin(margin-left、margin-right)永远不会collapse

  • 折算后的规则:

    • 两个值进行比较,选取最大的值
  • 如何防止折叠呢

    • 只设置一个的margin

三、上下margin折叠情况

  • 两个兄弟块级元素之间上下margin的折叠

    • 在这里插入图片描述
  • 父子块级元素之间margin的折叠

    • 在这里插入图片描述
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值