美文网首页
css实现三栏布局

css实现三栏布局

作者: 肉肉肉肉_包 | 来源:发表于2019-06-03 16:49 被阅读0次

假设高度一定,请写出三栏布局,左右宽度300px,中间自适应。
初始化页面

*{
      padding: 0;
      margin: 0;
    }
.layout {
      margin-top: 20px;
    }
    .layout article div {
      min-height: 100px;
    }

下面是五种解决方案

<!-- float浮动的解决方案 -->
  <section class="layout float">
    <style>
      .layout.float .left {
        float: left;
        width: 300px;
        background: #ffa907;
      }
      .layout.float .right {
        float: right;
        width: 300px;
        background: pink;
      }
      .layout.float .center {
        background: #00d4ae;
      }
    </style>
    <article class="left-center-right">
      <div class="left">我是左边</div>
      <div class="right">我是右边</div>
      <div class="center">
          <h1>浮动解决方案</h1>
          <p>1.这是布局的中间部分</p>
          <p>2.这是布局的中间部分</p>
      </div>
    </article>
  </section>
<!-- 绝对定位的解决方案 -->
  <section class="layout absolute">
    <style>
      .layout.absolute .left-center-right>div {
        position: absolute;
      }
      .layout.absolute .left {
        left: 0;
        width: 300px;
        background: #ffa907;
      }
      .layout.absolute .center {
        left: 300px;
        right: 300px;
        background: #00d4ae;
      }
      .layout.absolute .right {
        right: 0;
        width: 300px;
        background: pink;
      }
    </style>
    <article class="left-center-right">
      <div class="left">我是左边</div>
      <div class="right">我是右边</div>
      <div class="center">
          <h1>绝对定位解决方案</h1>
          <p>1.这是布局的中间部分</p>
          <p>2.这是布局的中间部分</p>
      </div>
    </article>
  </section>
<!-- flexbox的解决方案 -->
  <section class="layout flexbox">
    <style>
      .layout.flexbox {
        margin-top: 140px;
      }
      .layout.flexbox .left-center-right {
        display: flex;
      }
      .layout.flexbox .left {
        width: 300px;
        background: #ffa907;
      }
      .layout.flexbox .center {
        flex: 1;
        background: #00d4ae;
      }
      .layout.flexbox .right {
        width: 300px;
        background: pink;
      }
    </style>
    <article class="left-center-right">
      <div class="left">我是左边</div>
      <div class="center">
          <h1>flexbox解决方案</h1>
          <p>1.这是布局的中间部分</p>
          <p>2.这是布局的中间部分</p>
      </div>
      <div class="right">我是右边</div>
    </article>
  </section>
<!-- 表格布局的解决方案 -->
  <section class="layout table">
    <style>
      .layout.table .left-center-right {
        width: 100%;
        display: table;
        height: 100px;
      }
      .layout.table .left-center-right>div {
        display: table-cell;
      }
      .layout.table .left {
        width: 300px;
        background: #ffa907;
      }
      .layout.table .center {
        background: #00d4ae;
      }
      .layout.table .right {
        width: 300px;
        background: pink;
      }
    </style>
    <article class="left-center-right">
      <div class="left">我是左边</div>
      <div class="center">
          <h1>flexbox解决方案</h1>
          <p>1.这是布局的中间部分</p>
          <p>2.这是布局的中间部分</p>
      </div>
      <div class="right">我是右边</div>
    </article>
  </section>
<!-- 网格布局的解决方案 -->
  <section class="layout grid">
    <style>
      .layout.grid .left-center-right {
        width: 100%;
        display: grid;
        grid-template-rows: 100px;
        grid-template-columns: 300px auto 300px;
      }
      .layout.grid .left {
        width: 300px;
        background: #ffa907;
      }
      .layout.grid .center {
        background: #00d4ae;
      }
      .layout.grid .right {
        width: 300px;
        background: pink;
      }
    </style>
    <article class="left-center-right">
      <div class="left">我是左边</div>
      <div class="center">
          <h1>flexbox解决方案</h1>
          <p>1.这是布局的中间部分</p>
          <p>2.这是布局的中间部分</p>
      </div>
      <div class="right">我是右边</div>
    </article>
  </section>

几种方案的比较
1.浮动布局 缺点:浮动以后脱离文档流 优点:兼容性好
2.绝对定位 缺点:脱离文档流 ,可使用性差 优点:快捷
3.flexbox布局 移动端使用很多
4.表格布局 兼容性好
5.网格布局 代码量少

另补充两种方式

<section class="layout margin">
    <style>
      .layout.margin .left-center-right .main {
        width: 100%;
        float: left;
      }
      .layout.margin .center {
        margin: 0 300px;
        background: #00d4ae;
      }
      .layout.margin .left {
        float: left;
        width: 300px;
        margin-left: -100%;
        background: #ffa907;
      }
      .layout.margin .right {
        float: right;
        width: 300px;
        margin-left: -300px;
        background: pink;
      }
    </style>
    <article class="left-center-right">
      <div class="main">
        <div class="center">
            <h1>margin解决方案</h1>
            <p>1.这是布局的中间部分</p>
            <p>2.这是布局的中间部分</p>
        </div>
      </div>
      <div class="left">我是左边</div>
      <div class="right">我是右边</div>
    </article>
  </section>

中间的主体使用双层标签,外层div宽度100%显示,并且浮动,内层div为真正的主体内容,含有左右300像素的margin值。左栏与右栏都是采用margin负值定位的,左栏左浮动,margin-left为-100%,由于前面的div宽度100%与浏览器,所以这里的-100%margin值正好使左栏div定位到了页面的左侧;右侧栏也是左浮动,其margin-left也是负值,大小为其本身的宽度即300像素。

<section class="layout margin">
    <style>
      .layout.margin .left-center-right .main {
        padding: 300px;
      }
      .layout.margin .center {
        width: 100%;
        float: left;
        background: #00d4ae;
      }
      .layout.margin .left {
        float: left;
        margin-left: -100%;
        position: relative;
        left: -300px;
        width: 300px;
        background: #ffa907;
      }
      .layout.margin .right {
        float: right;
        margin-left: -300px;
        position: relative;
        right: -300px;
        width: 300px;
        background: pink;
      }
    </style>
    <article class="left-center-right">
      <div class="main">
        <div class="center">
            <h1>margin解决方案</h1>
            <p>1.这是布局的中间部分</p>
            <p>2.这是布局的中间部分</p>
        </div>
        <div class="left">我是左边</div>
        <div class="right">我是右边</div>
      </div>
    </article>
  </section>

首先用一个外层div包裹主体和左右两栏,外层div设置左右padding:300px,主体和左右两栏左浮动,左栏与右栏都是采用margin负值定位的,左栏左浮动,margin-left为-100%,由于前面的div宽度100%与浏览器,所以这里的-100%margin值正好使左栏div定位到了主题的左侧,再相对定位position:relative;left:-300px;将左栏移动到外层的padding区域,以保证不遮挡主体区域;右侧栏也是左浮动,其margin-left也是负值,大小为其本身的宽度即300像素,同时也需要设置相对定位,position:relative;right:-300px;以保证不遮挡主体区域。

相关文章

  • CSS布局

    HTML CSS + DIV实现整体布局必备知识利用HTML和CSS实现常见的布局 单列布局 css 实现竖直居中...

  • CSS常用布局实现

    该系列用于整理记录常用的CSS布局实现。 CSS常用布局实现01-水平居中 CSS常用布局实现02-垂直居中 CS...

  • CSS布局(不完全)总结

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

  • 24.纯CSS实现瀑布流布局

    1.CSS实现瀑布流布局(display: flex) 本例使用 CSS flex 实现瀑布流布局关键点,横向fl...

  • css布局和居中

    本文主要介绍了css常用的布局,居中等其他小技巧。 css布局 左右布局 利用float实现左右布局 左右模块设置...

  • css+div 实现上中下左右布局

    css+div 实现上中下布局

  • Css float属性的一些特点

    Css float属性的一些特点 css布局中float布局是常用的布局方式,用于实现横向多列布局。这个时候我们就...

  • 使用CSS实现栅格系统布局

    使用CSS实现栅格布局 栅格系统暴露给开发者的概念只有行(Row)和列(Column),但其内部实现还是CSS布局...

  • 轮播-淡入淡出

    JS实现功能 CSS样式以及布局结构

  • 特殊布局方法

    关于圣杯布局和双飞翼布局:CSS布局之--淘宝双飞翼布局双飞翼布局介绍CSS控制自适应宽度两三栏布局圣杯布局的实现...

网友评论

      本文标题:css实现三栏布局

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