float高度塌陷和BFC

开启BFC方式:

1.设置浮动float(副作用比较大,不推荐)

2.将元素设置为行内块元素 display:inline-block;(不推荐)

3.将元素的overlfow设置为非visible的值

overflow:scoll;(带滚动条)

overflow:auto;

overflow:hidden;(常用)

查看代码

<!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>
        header,main,footer{
            width:600px;
            margin: 0px auto;
        }

        header{
            background-color: aqua;
            height: 50px;

        }

        main{
            overflow: hidden;
            background-color: yellow;
        }

        .nav,.content,.aside{
             float: left;
            height: 400px;
          
        }

        .nav{
           
            width:100px;
            background-color: red;
        }

        .content{
            width:380px;
            background-color: violet;
            margin: 0px 10px;
        }

        .aside{
            width: 100px;
            background-color: red;
        }

        footer{
            height: 50px;
            background-color: aqua;
        }
    </style>
</head>
<body>
    <!-- 网页的头部 -->
    <header>
        
    </header>
    
    <!-- 网页的主体 -->
    <main>
        <div class="nav"></div>
        <div class="content"></div>
        <div class="aside"></div>
    </main>

    <!-- 网页的底部 -->
    <footer></footer>
</body>
</html>

黄色背景随着main中的三部分的高度变化而变化

posted @ 2022-04-23 09:13  Eveeee  阅读(21)  评论(0编辑  收藏  举报