CSS三栏布局的几种自适应的排版方式

   CSS三栏布局的几种方式,主要有 float、position、flex实现。


   对红色、蓝色 box 设置 float,绿色不设。



<style>
        * {
    
    
            margin: 0;
            padding: 0;
        }

        .left {
    
    
            width: 200px;
            height: 300px;
            background-color: red;
            float:left;
        }

        .right {
    
    
            width: 200px;
            height: 300px;
            background-color: blue;
            float:right
        }

        .center {
    
    
            width: 200px;
            height: 300px;
            background-color: green;
        }

</style>


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


   可见,结果绿色box好像消失了,其实是由于没有设置 float的情况下,它依然保持块级元素的属性。因此蓝色box在绿色box的元素下排列。



在这里插入图片描述



在这里插入图片描述



   把绿色块元素移到最后,给它配置 margin 0 200px



在这里插入图片描述



   可见绿色box刚好自动填充在红、蓝box之间。浏览器缩放时,也是3个box互相紧挨的效果。



在这里插入图片描述



   position实现



<style>
        * {
    
    
            margin: 0;
            padding: 0;
        }

        .left {
    
    
            width: 200px;
            height: 300px;
            background-color: red;
            /* float:left; */
            position:absolute;
            left:0;
        }

        .right {
    
    
            width: 200px;
            height: 300px;
            background-color: blue;
            /* float:right */
            position:absolute;
            right:0;
        }

        .center {
    
    
            /* width: 200px; */
            height: 300px;
            background-color: green;
            margin: 0 200px;
        }

</style>

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



   对红、蓝box设置绝对定位的方式固定位置



在这里插入图片描述



在这里插入图片描述



   flex实现 (最方便的一种)



 <style>
        * {
    
    
            margin: 0;
            padding: 0;
        }

        .left {
    
    
            width: 200px;
            height: 300px;
            background-color: red;
            /* float:left; */
            /* position:absolute;
            /* left:0; */
        } 

        .right {
    
    
            width: 200px;
            height: 300px;
            background-color: blue;
            /* float:right */
            /* position:absolute;
            right:0;
           */
        }

        .center {
    
    
            /* width: 200px; */
            height: 300px;
            background-color: green;
            /* margin: 0 200px; */
        }

        .father{
    
    
            display:flex;
        }

    
</style>


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


   设置 father 父元素的样式 display: flex;



在这里插入图片描述



   可见绿色box又好像消失了,是因为绿色box没有设置宽度,当 father 元素设置 flex 后, 它里面的块级元素就不能继承 father的宽度。



在这里插入图片描述



   给绿色 box 加设 flex-grow : 1 去自动填补剩余的空间。



在这里插入图片描述



在这里插入图片描述



   最后补充一点,flex-grow: 1 其实也可以写为 flex: 1 1 auto; 或者更简写的 flex:1; 这个知识点,大家可以再参考关于flex布局这方面。



在这里插入图片描述



在这里插入图片描述



在这里插入图片描述



在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_48591974/article/details/131767265
今日推荐