BFC解决的几种问题

外边距塌陷

原因:父子嵌套标签垂直方向的margin,margin会其中最大值

解决方法:

  1. 给父级设置border或者padding
  2. 改变父级元素bfc渲染模式

position:absolution/fixed

float:right/left

overflow:hidden

display:inline-bloack

 

浮动的问题

原因:标签浮动后脱离正常文档流,导致父标签无法被撑开,且会影响后续的布局

解决方法:

  1. 给父级设置高度
  2. 改变父级的bfc渲染模式:
  • 清除浮动:clear:both
  • overflow:hidden;

伪元素:

父级标签::after{
    content:'内容';

    display:block;

    clear:both;

}

 

 

图片间隙问题

  1. 将图片显示为块元素(解决下方间隙)display:block
  2. 改变图片的vertical-align:middle/top/bottom
  3. 设置父级标签:(水平间隙)

font-size:0;

line-height:0;

 

代码如下

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="bfc.css">
</head>

<body>
    <!-- 外边距塌陷问题 -->
    <!-- 
        1.给父级添加padding或者border
        2.改变父级的bfc渲染模式
          -position:absolute/fixed;
          -添加浮动float
          -overflow:hidden
          -display:inline-bloack
     -->
    <div class="bigbox">
        <div class="smbox"></div>
        <!-- <div style="clear:both;"></div> -->
       
    </div>
    <div class="box"></div>



    <!-- 浮动问题 -->
    <!-- 
给标签设置浮动后,脱离正常文档流,导致父标签不能被撑起

1.给父级设置高度
2.改变父级的bfc渲染模式
  -清除浮动clear:both
  -overflow:hidden
  -伪元素
.bigbox:after{
    content: '';
    display: block;
    clear: both;
}

     -->

</body>

</html>
.bigbox{
    background-color: skyblue;
    width: 500px;

    /* 外边距塌陷问题 */
    /* margin-top: 100px; */
   /* border: 1px solid red; */
   /* padding: 100px; */
   /* position: absolute; */
   /* position: fixed; */
   /* float: left; */
   /* overflow:hidden */
   /* display:inline-block; */



   /* 浮动问题 */
   /* height: 300px; */
   /* overflow:hidden */
}
/* .bigbox:after{
    content: '';
    display: block;
    clear: both;
} */
.smbox{
    width: 200px;
    height: 200px;
    background-color: pink;
    float: left;
    /* margin-top: 50px; */
}
.box{
    width: 200px;
    height: 200px;
    background-color: orange;
    /* float: left; */
    /* margin-top: 50px; */
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值