【实现三栏布局的七种方法】

本文介绍了网页布局的六种常见技术:float+margin、float+BFC、flex布局、table布局、position定位以及圣杯和双飞翼布局。通过实例代码详细展示了每种布局的实现原理和应用场景,帮助开发者更好地理解和运用各种布局策略。
摘要由CSDN通过智能技术生成

第一种:float+margin

        .left{
            float: left;
            width: 200px;
            height: 200px;
            background: red;
        }
        .right{
            width: 200px;
            height: 200px;
            float: right;
            background: blue;
        }
        .main{
            height: 200px;
            margin-left: 200px;
            margin-right: 200px;
            background: green;
        }
    <div class="container">
        <div class="left"></div>
        <div class="right"></div>
        <div class="main"></div>
    </div>


第二种:float+bfc

        .left {
            float: left;
            width: 200px;
            height: 200px;
            background: red;
        }
        .right{
            float: right;
            background: blue;
            width: 200px;
            height: 200px;
        }.main{
            height: 200px;
            overflow: hidden;
            background: green;
        }
    <div class="container">
        <div class="left"></div>
        <div class="right"></div>
        <div class="main"></div>
    </div>


第三种:flex布局

        .container{
            height: 200px;
            display: flex;
            justify-content: space-between;
        }
        .left{
            width: 200px;
            height: 200px;
            background: red;
        }
        .right{
            width: 200px;
            height: 200px;
            background: blue;
        }
        .main{
            flex: 1;
            background: green;
        }
    <div class="container">
        <div class="left"></div>
        <div class="main"></div>
        <div class="right"></div>
    </div>


第四种:table布局

        .container{
            display: table;
            width: 100%;;
        }
        .left
        {
            height: 200px;
            display:table-cell;
            background: red;
        }
        .main{
            height: 200px;
            display: table-cell;
            background: blue;
        }
        .right{
            height: 200px;
            display: table-cell;
            background: green;
        }
    <div class="container">
        <div class="left"></div>
        <div class="main"></div>
        <div class="right"></div>
    </div>


第五种:position

需要这注意 main 的位置。

.container{
            position: relative;
        }
        .left{
            position: absolute;
            width: 200px;
            height: 200px;
            left: 0;
            background: red;
        }
        .right{
            position: absolute;
            width: 200px;
            height: 200px;
            background: blue;
            /* 必须写这个 和 main书写位置有关系*/
            /* top: 0; */
            right: 0;
        }
        .main{
            height: 200px;
            margin: 0 200px;
            background: green;
        }
    <div class="container">
        <div class="left"></div>
        <div class="right"></div>
        <!-- main写在中间得加top -->
        <div class="main"></div>
    </div>


第六种:圣杯布局

.container{
            margin: 0 200px;
        }
        .main{
            float: left;
            width: 100%;
            height: 200px;
            background: red;
        }
        /* 被注释的地方还可以用相对定位去实现 
            position:relative;
            left:-200px;/right:-200px;
        */
        .left{
            float: left;
            width: 200px;
            height: 200px;
            margin-left: -100%;
            /* transform: translateX(-200px); */
            background: blue;
        }
        .right{
            float: left;
            width: 200px;
            height: 200px;
            /* transform: translateX(200px); */
            margin-left: -200px;
            background:green;
        }
    <div class="container">
        <div class="main"></div>
        <div class="left"></div>
        <div class="right"></div>
    </div>


第七种:双飞翼布局

<style>
        .container {
            float: left;
            width: 100%;
            height: 200px;
            background: red;
        }
    
        .main {
            margin: 0 200px;
        }
        .left {
            float: left;
            width: 200px;
            height: 200px;
            margin-left: -100%;
            background: blue;
        }
    
        .right {
            float: left;
            width: 200px;
            height: 200px;
            margin-left: -200px;
            background: green;
        }
    </style>
<body>
    <div class="container">
        <div class="main"></div>
    </div>
        <div class="left"></div>
        <div class="right"></div>
</body>

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
实现flex实现三栏布局,可以按照以下步骤进行操作: 1. 首先,创建一个外层容器,可以使用`<div>`元素,并为其设置`display: flex;`来启用flex布局。[2] 2. 在外层容器中创建三个子元素,代表三栏布局的左、中、右栏。 3. 对左、右栏设置一个固定的宽度,例如`width: 300px;`。对中间栏不设置特定的宽度。 4. 对三个子元素设置`flex: 1 1 auto;`,这将使它们平分剩余空间。这意味着中间栏的宽度将自适应。 5. 可以根据需要为三个子元素添加其他样式,比如背景色、边框等。 以下是一个使用flex实现三栏布局的示例代码: ``` <div class="container"> <div class="left"></div> <div class="middle"></div> <div class="right"></div> </div> ``` ``` .container { display: flex; } .left, .right { width: 300px; } .middle { flex: 1 1 auto; } ``` 通过设置外层容器的`display: flex;`和子元素的相应样式,就可以实现使用flex布局三栏等分布局。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [flex实现三栏等分布局](https://blog.csdn.net/az44yao/article/details/117676640)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [css3弹性盒子flex实现三栏布局实现](https://download.csdn.net/download/weixin_38726441/14919543)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Crush201

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值