CSS 三栏布局方法总结

作者: sylvia_yue | 来源:发表于2019-04-08 19:18 被阅读5次

本文总结了三栏布局的几种常用方法,效果如下:

image

其中,左右分别为100px,中间自适应。

1. 浮动

html 代码:

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

css 代码:

      .container-float > div {
        height: 200px;
      }
      .container-float > .left {
        width: 100px;
        background: aliceblue;
        float: left;
      }
      .container-float > .center {
        background: antiquewhite;
        margin: 0 100px 0 100px;
      }
      .container-float > .right {
        width: 100px;
        background: aquamarine;
        float: right;
      }

缺点:

  • 中间栏必须放在最后;
  • 窗口缩小时,中间栏可能被覆盖,右边栏也可能被挤到下一行。

2. 绝对定位

html 代码:

        <div class="container-absolute">
          <div class="left">left</div>
          <div class="center">center</div>
          <div class="right">right</div>
        </div>

css 代码:

      .container-absolute {
        height: 200px;
        position: relative;
      }
      .container-absolute > div {
        height: 200px;
        position: absolute;
        top: 0;
      }
      .container-absolute .left {
        width: 100px;
        background: aliceblue;
        left: 0;
      }
      .container-absolute .center {
        background: antiquewhite;
        left: 100px;
        right: 100px;
      }
      .container-absolute .right {
        width: 100px;
        background: aquamarine;
        right: 0;
      }

缺点:

  • 窗口缩小时,中间栏、左边栏可能会被覆盖;
  • 使用绝对定位,要考虑与页面其他元素间的关系。

3. flex

html 代码:

        <div class="container-flex">
          <div class="left">left</div>
          <div class="center">center</div>
          <div class="right">right</div>
        </div>

css 代码:

      .container-flex {
        height: 200px;
        display: flex;
      }
      .container-flex > div {
        height: 200px;
      }
      .container-flex .left {
        width: 100px;
        background: aliceblue;
      }
      .container-flex .center {
        background: antiquewhite;
        flex: 1;
      }
      .container-flex .right {
        width: 100px;
        background: aquamarine;
      }

缺点:

  • 窗口缩小时,左右两栏宽度可能会改变。

4. table

html 代码:

        <div class="container-table">
          <div class="left">left</div>
          <div class="center">center</div>
          <div class="right">right</div>
        </div>

css 代码:

      .container-table {
        height: 200px;
        width: 100%;
        display: table;
      }
      .container-table > div {
        height: 200px;
        display: table-cell;
      }
      .container-table .left {
        width: 100px;
        background: aliceblue;
      }
      .container-table .center {
        background: antiquewhite;
      }
      .container-table .right {
        width: 100px;
        background: aquamarine;
      }

缺点:

  • 窗口缩小时,左右两栏宽度可能会改变。

5. grid

html 代码:

        <div class="container-grid">
          <div class="left">left</div>
          <div class="center">center</div>
          <div class="right">right</div>
        </div>

css 代码:

      .container-grid {
        height: 200px;
        width: 100%;
        display: grid;
        grid-template-rows: 200px;
        grid-template-columns: 100px auto 100px;
      }
      .container-grid .left {
        background: aliceblue;
      }
      .container-grid .center {
        background: antiquewhite;
      }
      .container-grid .right {
        background: aquamarine;
      }

6. 圣杯布局

html 代码:

        <div class="container-shengbei">
          <div class="left">left</div>
          <div class="center">center</div>
          <div class="right">right</div>
     
        </div>

css 代码:

      .container-shengbei {
        height: 200px;
        padding: 0 100px 0 100px;
      }
      .container-shengbei > div {
        float: left;
        position: relative;
        height: 200px;
      }
      .container-shengbei .left {
        width: 100px;
        background: aliceblue;
        margin-left: -100px;
      }
      .container-shengbei .center {
        width: 100%;
        background: antiquewhite;
      }
      .container-shengbei .right {
        width: 100px;
        background: aquamarine;
        right: -100px;
        margin-left: -100px;
      }

缺点:

  • 窗口缩小时,中间栏可能被覆盖,右边栏也可能被挤到下一行。

7. 双飞翼布局

html 代码:

        <div class="container-shuangfei">
          <div class="center">
              <div class="inner">center</div>
          </div>
          <div class="left">left</div>
          <div class="right">right</div>
        </div>

css 代码:

      .container-shuangfei {
        height: 200px;
      }
      .container-shuangfei > div {
        float: left;
        position: relative;
        height: 200px;
      }
      .container-shuangfei .left {
        width: 100px;
        background: aliceblue;
        margin-left: -100%;
      }
      .container-shuangfei .center {
        width: 100%;
      }
      .container-shuangfei .center .inner{
          margin: 0 100px 0 100px;
          height: 200px;
          background: antiquewhite;
      }
      .container-shuangfei .right {
        width: 100px;
        background: aquamarine;
        margin-left: -100px;
      }

缺点:

  • 窗口缩小时,中间栏、左边栏都可能被覆盖,整个布局可能会乱。

相关文章

  • CSS布局(不完全)总结

    CSS布局(不完全)总结 实现水平居中布局的几种方式 方法一: 通过以下CSS代码实现水平居中布局 方法二: 通过...

  • web前端教程:CSS 布局十八般武艺都在这里了

    CSS布局 布局是CSS中一个重要部分,本文总结了CSS布局中的常用技巧,包括常用的水平居中、垂直居中方法,以及单...

  • DIV+CSS对SEO网站优化好处有哪些?

    div+css布局是一种网页的布局方法,是目前应用最广泛的网页布局方法。div css布局是把网页用div+css...

  • 2019-01-26

    一、总结一下CSS的几种布局以及实现方法 左右布局 左中右 水平居中 垂直居中 1. 左右布局 左右布局很多种实现...

  • 小程序CSS知识点

    一、flex布局Flex 布局教程:语法篇Flex 布局教程:实例篇 二、CSS position 属性总结CSS...

  • css居中总结

      css居中是布局中常见的方法,现将常用方法总结如下: 1.水平居中 1.1.使用inline-block + ...

  • css布局之上下两行布局(上面固定高度,下面自适应)

    方法一: 利用position绝对定位+BFChtml: css: 方法二:flex布局 html: css:

  • CSS经典布局

    CSS经典布局 本文主要对一些常见的CSS布局问题进行总结,涉及三栏布局、负margin、清除浮动、居中布局、Fl...

  • 006_布局任务与答案第一期(四川师范大学JavaWeb)

    试验目的 了解页面常用布局结构; 掌握 DIV+CSS 布局的基本方法; 3) 掌握用 CSS 改变页面样式的方法...

  • css布局

    最近在学习css相关的知识,本文主要总结一些css布局相关的知识。 一、左右布局和左中右布局 左右布局和左中右布局...

网友评论

    本文标题:CSS 三栏布局方法总结

    本文链接:https://www.haomeiwen.com/subject/gxopiqtx.html