关于盒模型的几个相关知识点的总结,以及三列布局的几种方法

盒模型

盒子模型:就是把HTML页面中的布局元素看作是一个矩形的盒子,也就是一个盛装内容的容器
CSS盒子模型本质上是一个盒子,封装周围的HTML元素,它包括:边框,外边距,内边距和实际内容

三栏布局(左右固定,中间自适应)

  1. BFC
<!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>
</head>
<style>
    /* 
        BFC(Block formatting context)直译为"块级格式化上下文"。
        它是一个独立的渲染区域,只有Block-level box参与, 
        它规定了内部的Block-level Box如何布局,并且与这个区域外部毫不相干。
        并且在一个BFC中,块盒与行盒,都会垂直的沿着其父元素的边框排列

        布局规则:
            内部的Box会在垂直方向,一个接一个地放置。
            Box垂直方向的距离由margin决定。属于同一个BFC的两个相邻Box的margin会发生重叠。
            每个盒子(块盒与行盒)的margin box的左边,与包含块border box的左边相接触(对于从左往右的格式化,否则相反)。即使存在浮动也是如此。
            BFC的区域不会与float box重叠。
            BFC就是页面上的一个隔离的独立容器,容器里面的子元素不会影响到外面的元素。反之也如此。
            计算BFC的高度时,浮动元素也参与计算
        如何创建:
            1、float的值不是none。
            2、position的值不是static或者relative。
            3、display的值是inline-block、table-cell、flex、table-caption或者inline-flex
            4、overflow的值不是visible
     */
    * {
        margin: 0;
        padding: 0;
    }

    #left ,#right{
        width: 30%;
        height: 150px;
        background: rgb(139, 214, 78);
        text-align: center;
        line-height: 150px;
        font-size: 20px;
    }

    #left {
        float: left;
    }

    #right {
        float: right;
    }

    #center {
        overflow: hidden;
        height: 200px;
        background: rgb(170, 54, 236);
        text-align: center;
        line-height: 200px;
        font-size: 40px;
    }
</style>

<body>
    <div id="div1">
        <div id="left">我是left</div>
        <div id="right">我是right</div>
        <div id="center">我是center</div>
        <!-- [注] 这里center必须放到最后 -->
    </div>
</body>

</html>

在这里插入图片描述

  1. 定位
<!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>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            min-height: 100px;
        }

        .div1 {
            position: relative;

        }

        div {
            position: absolute;
            top: 0;
            min-height: 100px;
        }

        .left {
            left: 0;
            width: 100px;
            /* height: 100px; */
            background-color: pink;
        }

        .right {
            right: 0;
            width: 150px;
            /* height: 100px; */
            background-color: purple;
        }

        .center {
            left: 100px;
            right: 150px;
            height: 150px;
            background-color: orange;
        }
    </style>
</head>

<body>
    <div class="div1">
        <div class="center">center</div>
        <div class="left">left</div>
        <div class="right">right</div>
    </div>
</body>

</html>

在这里插入图片描述

  • 扩展:四个盒子,上一行三列布局,左右固定,中间自适应,下一行铺满
<!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>
    <style>
        /* 
            上边实现三列布局,左右固定,中间自适应
            底部铺满
         */
        * {
            margin: 0;
            padding: 0;
        }

        .div1 {
            position: relative;
            /* display: flex; */
            height: 100px;
            /* 
                清除浮动防止父元素塌陷,脱离标准流,
                添加overflow时创建了BFC
                虽然设置的三个盒子高不同,但是触发bfc
                计算bfc高度时,浮动元素高度也参与计算
             */
            overflow: hidden;
        }

        .left {
            position: absolute;
            left: 0;
            top: 0;
            width: 100px;
            height: 120px;
            background-color: pink;
        }

        .right {
            position: absolute;
            top: 0;
            right: 0;
            width: 150px;
            height: 120px;
            background-color: purple;
        }

        .center {
            position: absolute;
            top: 0;
            left: 100px;
            right: 150px;
            height: 150px;
            background-color: orange;
        }

        /* 添加底部固定一栏 */
        .footer {
            width: 100%;
            height: 50px;
            background-color: skyblue;
        }
    </style>
</head>

<body>
    <div class="div1">
        <div class="center">center</div>
        <div class="left">left</div>
        <div class="right">right</div>
    </div>
    <div class="footer">footer</div>
</body>

</html>

在这里插入图片描述

  1. 浮动(双飞翼布局、圣杯布局)
  • 双飞翼布局
<!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>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        body {
            min-width: 550px;
        }
        
        /* 双飞翼布局 */
        .container {
            overflow: hidden;
        }
        .column {
            float: left;
            min-height: 100px;
        }
        .left {
            margin-left: -100%;
            width: 200px;
            background-color: pink;
        }.center {
            width: 100%;
        }
        .center-inner {
            margin: 0 150px 0 200px;
            min-height: 100px;
            background-color: skyblue;
        }
        .right {
            margin-left: -150px;
            width: 150px;
            background-color: orange;
        }

    </style>
</head>

<body>
    <div class="container">
        <div class="column center">
            <!-- 双飞翼布局多了一层包裹center内容的代码 -->
            <div class="center-inner">center</div>
        </div>
        <div class="column left">left</div>
        <div class="column right">right</div>
    </div>
</body>

</html>

在这里插入图片描述

  • 圣杯布局
<!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>
    <style>
        /* 圣杯布局 */
        * {
            margin: 0;
            padding: 0;
        }
        
        body {
            min-width: 550px;
        }

        .container {
            padding: 0 150px 0 200px;
            overflow: hidden;
        }

        .column {
            position: relative;
            float: left;
            min-height: 100px;
        }

        .left {
            right: 200px;
            margin-left: -100%;
            width: 200px;
            background-color: pink;
        }


        .center {
            width: 100%;
            background-color: orange;
        }
        
        .right {
            left: 150px;
            margin-left: -150px;
            width: 150px;
            background-color: purple;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="column center">center</div>
        <div class="column left">left</div>
        <div class="column right">right</div>
    </div>
</body>

</html>

在这里插入图片描述

  1. flex弹性布局
<!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>
</head>
<style>
    *{
        margin: 0;
        padding: 0;
    }
    #div1 {
        display: flex;
    }
    #left {
        width: 200px;
        height: 200px;
        background-color: pink;
    }
    #center {
        flex: 1;
        /* 
        flex: 1; === flex: 1 1 0;
        第一个参数表示: flex-grow 定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大
        第二个参数表示: flex-shrink 定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小
        第三个参数表示: flex-basis给上面两个属性分配多余空间之前, 计算项目是否有多余空间, 默认值为 auto, 即项目本身的大小
        【注】auto 为表示项目本身的大小, 如果设置为 auto, 那么这三个盒子就会按照自己内容的多少来等比例的放大和缩小
          */
        height: 300px;
        background-color: purple;
    }
    #right {
        width: 150px;
        height: 200px;
        background-color: orange;
    }
    /* 等宽布局 */
    /* div {
        flex: 1;height: 200px;border: 1px solid red;
    } */
</style>
<body>
    <div id="div1">
        <div id="left"></div>
        <div id="center"></div>
        <div id="right"></div>
    </div>
</body>
</html>

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值