三栏布局的几种实现方式

本文介绍了实现三栏布局的八种方法:浮动布局、定位布局、圣杯布局、双飞翼布局、弹性布局、表格布局、网格布局和calc函数布局。每种布局方式都有其优缺点,如浮动布局兼容性好但可能导致父容器高度塌陷,弹性布局是未来趋势但要考虑浏览器兼容性,网格布局方便但不支持center部分前置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

三栏布局是一种经典的页面布局:在网页上以平铺方式展现的左中右三列布局,其特点在于,左右两列可固定在网页两侧,中间一列永远居中,且当网页宽度大于左右两列宽度之和时,中间一列可随网页整体宽度的变化而变化(简单来说就是两端固定,中间自适应)

提示:以下是本篇文章中所使用的公共样式,可供参考:

*{
    padding: 0;
    margin: 0;
    height: 50px;
    text-align: center;
}
div{
    height: 200px;
}
.left{
    width: 200px;
    background-color: aqua;
}
.right{
    width: 200px;
    background-color: aquamarine;
}
.center{
    background-color: greenyellow;
}
应用布局前
应用布局后

一、浮动布局 Float

        左右模块各自向左右浮动,并设置中间模块的 margin 值使中间模块宽度自适应。这种布局方式,dom结构必须是先写浮动部分,然后再写中间块,否则右浮动块会掉到下一行。

<style>
        .left{
            float: left;
        }
        .right{
            float: right;
        }
        .center{
            margin: 0 200px;
        }
    </style>
</head>
<body>
    <header>header</header>
    <div class="left">Left</div>
    <div class="right">Right</div>
    <!-- 中心布局必须在最后加载 -->
    <div class="center">Center</div>
    <footer>footer</footer>
</body>

         浮动布局方式比较简单,兼容性好,但是存在局限性:浮动元素脱离文档流,要做清除浮动操作, 这个处理不好的话,会带来很多问题,比如父容器高度塌陷等问题;而且由于主要内容无法最先加载,当页面内容较多时会影响用户体验。

二、定位布局 Position

        让两边的盒子定位固定在让中间的盒子流出空位:

<style>
        .left,
        .right{
            position: absolute;
            top: 50px;
        }
        .left{
            left: 0;
        }
        .right{
            right: 0;
        }
        .center{
            margin: 0 200px;
        }
    </style>
</head>
<body>
    <header>header</header>
    <!-- 因为不再使用浮动布局,所以可以将中间布局内容提前,以便于优先加载主要内容 -->
    <div class="center">Center</div>
    <div class="left">Left</div>
    <div class="right">Right</div>
    <footer>footer</footer>
</body>

        绝对定位布局优点就是简单易懂,并且主要内容可以优先加载,设置很方便,而且也不容易出问题。缺点就是,容器脱离了文档流,后代元素也脱离了文档流,高度未知的时候,会有问题,这就导致了这种方法的有效性和可使用性是比较差的。

三、圣杯布局 Holy Grail Layout

        将父级元素设置左右的 padding,三列均设置向左浮动,中间一列放在最前面,宽度设置为父级元素的宽度,因此后面两列都被挤到了下一行,再通过对浮动元素 margin 赋负值,使其移动到上一行之后,利用相对定位定位到父元素两边。

<style>
        body{
            margin: 0 200px;
        }
        div{
            float: left;
        }
        .left{
            margin-left: -100%;
            position: relative;
            left: -200px;
        }
        .right{
            margin-right: -200px;
        }
        .center{
            width: 100%;
        }
    </style>
<body>
    <header>header</header>
    <!-- 中间布局需要优先设置 -->
    <div class="center">Main</div>
    <div class="left">Left</div>
    <div class="right">Right</div>
    <footer>footer</footer>
</body>

        圣杯布局支持 center 部分前置,可以实现中间部分优先加载;left、right 的高度可以伴随 center 高度的变化,结构比较简单,没有多余的 dom 层。但是当父元素有内外边距时,会导致中间栏的位置出现偏差;当 center 部分小于 left 时,圣杯的结构容易出现混乱,完全看不出来需要实现的效果。

四、双飞翼布局

        双飞翼布局类似于圣杯布局,不过圣杯布局利用 wrapper 的 padding 来保留左右位置的,而双飞翼布局利用 center 的 margin 来实现的。只要center 外边距的宽度只要恰好等于 left 和 right,这样在视觉上看起来 center 和 left、right 就是三个独立的板块。

<style>
        body>div{
            float: left;
        }
        .left{
            margin-left: -100%;
        }
        .right{
            margin-left: -200px;
        }
        .center{
            width: 100%;
        }
        .inner {
            margin: 0 200px;
        }
    </style>
<body>
    <header>header</header>
    <!-- 中间布局需要优先设置 -->
    <div class="center">
    <!-- 多设置一层div嵌套 -->
        <div class="inner">
            Center
        </div>
    </div>
    <div class="left">Left</div>
    <div class="right">Right</div>
    <footer>footer</footer>
</body>

        双飞翼布局由于对 center 部分添加了一个中间父元素,多了一个 dom 结构层,就会增加渲染树的生成计算量。

五、弹性布局 Flex

        给父级设置display:flex,可以通过Flex布局来实现三栏布局的效果:

<style>
        .container {
            display: flex;
        }
        .center{
            flex: 1;
        }
    </style>
<body>
    <header>header</header>
    <div class="container">
        <div class="left">Left</div>
        <div class="center">Center</div>
        <div class="right">Right</div>
    </div>
    <footer>footer</footer>
</body>

        Flex布局简单实用,支持 center 部分前置,可以实现中间部分优先加载,是未来发展的趋势,不过使用时需要考虑浏览器的兼容问题。

六、表格布局 Table 

                给父级设置display:table,然后让子元素都是 display: table-cell,就可以实现三栏布局:

<style>
        .container {
            display: table;
            width: 100%;
        }

        .container>div {
            display: table-cell;
        }
    </style>
<body>
    <header>header</header>
    <div class="container">
        <div class="left">Left</div>
        <div class="center">Center</div>
        <div class="right">Right</div>
    </div>
    <footer>footer</footer>
</body>

        Table布局具有非常好的兼容性,当内容溢出时会自动撑开父元素;但是Table布局不支持center部分前置,无法设置栏间距,而且对SEO(Search Engine Optimization,搜索引擎优化)不友好。

七、网格布局 Grid

        网格布局(Grid)是最新的 CSS 布局方案。它将网页划分成一个个网格,可以任意组合不同的网格,从而就能做出各种各样的布局。新增的CSS网格布局模块(CSS Grid Layout Module)提供了带有行和列的基于网格的布局系统,它使网页设计变得更加容易,而无需使用浮动和定位。

<style>
        .container {
            display: grid;
            width: 100%;
            grid-template-columns: 200px auto 200px;
        }
    </style>
<body>
    <header>header</header>
    <div class="container">
        <div class="left">Left</div>
        <div class="center">Center</div>
        <div class="right">Right</div>
    </div>
    <footer>footer</footer>
</body>

         Grid布局依旧存在缺陷:Grid布局不支持center部分前置,无法实现中间部分优先加载;而且left、right 的高度无法伴随 center 高度的变化而变化。

八、calc函数布局

        calc函数可以通过计算像素动态生成宽度,给中间盒子的宽度设置calc就可以实现三栏布局。但是每个元素都需要设置浮动:

<style>
        .container{
            width: 100%;
        }
        .container>div {
            float: left;
        }
        .center{
            width: calc(100% - 400px);
        }
    </style>
</head>
<body>
    <header>header</header>
    <div class="container">
        <div class="left">Left</div>
        <div class="center">Center</div>
        <div class="right">Right</div>
    </div>
    <footer>footer</footer>
</body>

        使用calc函数时需要注意,运算符前后都需要保留一个空格,比如上面代码如果写成width: calc(100%-400px)('-'号前后没有空格)是错误的。


总结

        以上就是三栏布局的几种实现方式,在合适的地方使用恰当的方法实现三栏布局,可以便于我们快速便捷地处理布局相关的问题。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值