CSS三栏布局的5种方法详解

更新日期: 2019-05-24阅读: 1.7k标签: 布局

题目:假设高度已知,请写出三栏布局,其中左栏、右栏宽度各为300px,中间自适应.


三栏布局的5种方案

这是一道经典的面试题,下面记录了css布局的5种方法。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>三栏布局</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
  </style>
</head>
<body>
<!--5种解决方案-->
</body>


1. 浮动解决方案

  <!-- 1. 浮动解决方案 -->
  <scetion class="layout float">
    <!-- 样式 -->
    <style media="screen">
      .layout.float article div {
        min-height: 80px;
      }

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

      .layout.float .center {
        background-color: yellow;
      }

      .layout.float .right {
        width: 300px;
        background-color: blue;
        float: right;
      }
    </style>

    <!-- 结构 -->
    <!-- 注意:结构中 右浮动的div一定要写在 center 的前面,否则无效. -->
    <h2>三栏布局</h2>
    <article class="left-ritgh-center">
      <div class="left"></div>
      <div class="right"></div>
      <div class="center">
        <h2>浮动解决方案</h2>
        1.这是三栏布局的浮动解决方案;
        2.这是三栏布局的浮动解决方案;
        3.这是三栏布局的浮动解决方案;
        4.这是三栏布局的浮动解决方案;
        5.这是三栏布局的浮动解决方案;
        6.这是三栏布局的浮动解决方案;
      </div>
    </article>
  </scetion>


2. 绝对定位解决方案

  <!-- 2. 绝对定位解决方案 -->
  <section class="layout absolute">
    <!-- 样式 -->
    <style>
      .layout.absolute article div {
        min-height: 80px;
        position: absolute;
      }

      .layout.absolute .left {
        width: 300px;
        background-color: red;
        left: 0;
      }

      .layout.absolute .center {
        background-color: yellow;
        left: 300px;
        right: 300px;
      }

      .layout.absolute .right {
        width: 300px;
        background-color: blue;
        right: 0;
      }
    </style>

    <!-- 结构 -->
    <h1>三栏布局</h1>
    <article class="left-center-right">
      <div class="left"></div>
      <div class="center">
        <h2>绝对定位解决方案</h2>
        1.这是三栏布局的绝对定位解决方案;
        2.这是三栏布局的绝对定位解决方案;
        3.这是三栏布局的绝对定位解决方案;
        4.这是三栏布局的绝对定位解决方案;
        5.这是三栏布局的绝对定位解决方案;
        6.这是三栏布局的绝对定位解决方案;
      </div>
      <div class="right"></div>
    </article>
  </section>


3. flexbox 解决方案

  <!-- 3. flexbox解决方案 -->
  <section class="layout flexbox">
    <!-- 样式 -->
    <style>
      /* flexbox解决方案在浏览器中显示时被上面的绝对定位解决方案挡住了,设置一个margin-top */
      .layout.flexbox {
        margin-top: 110px;
      }

      /* 设置最低高度 */
      .layout.flexbox article div {
        min-height: 80px;
      }

      .layout.flexbox article {
        display: flex;
      }

      .layout.flexbox .left {
        width: 300px;
        background-color: red;
      }

      .layout.flexbox .center {
        /*center部分增长 使整行填充*/
        flex: 1;
        background-color: yellow;
      }

      .layout.flexbox .right {
        width: 300px;
        background-color: blue;
      }
    </style>

    <!-- 结构 -->
    <h1>三栏布局</h1>
    <article class="left-center-right">
      <div class="left"></div>
      <div class="center">
        <h2>flexbox解决方案</h2>
        1.这是三栏布局的flexbox解决方案;
        2.这是三栏布局的flexbox解决方案;
        3.这是三栏布局的flexbox解决方案;
        4.这是三栏布局的flexbox解决方案;
        5.这是三栏布局的flexbox解决方案;
        6.这是三栏布局的flexbox解决方案;
      </div>
      <div class="right"></div>
    </article>
  </section>


4. 表格布局解决方案

  <!-- 4. 表格布局解决方案 -->
  <section class="layout table">
    <!-- 样式 -->
    <style>
      .layout.table .left-center-right {
        width: 100%;
        min-height: 80px;
        display: table;
      }

      .layout.table .left-center-right>div {
        display: table-cell;
      }

      .layout.table .left {
        width: 300px;
        background-color: red;
      }

      .layout.table .center {
        background-color: yellow;
      }

      .layout.table .right {
        width: 300px;
        background-color: blue;
      }
    </style>

    <!-- 结构 -->
    <h1>三栏布局</h1>
    <article class="left-center-right">
      <div class="left"></div>
      <div class="center">
        <h2>表格布局解决方案</h2>
        1.这是三栏布局的表格布局解决方案;
        2.这是三栏布局的表格布局解决方案;
        3.这是三栏布局的表格布局解决方案;
        4.这是三栏布局的表格布局解决方案;
        5.这是三栏布局的表格布局解决方案;
        6.这是三栏布局的表格布局解决方案;
      </div>
      <div class="right"></div>
    </article>
  </section>


5. 网格布局解决方案

  <!-- 5. 网格布局解决方案 -->
  <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 {
        background-color: red;
      }

      .layout.grid .center {
        background-color: yellow;
      }

      .layout.grid .right {
        background-color: blue;
      }
    </style>

    <!-- 结构 -->
    <h1>三栏布局</h1>
    <article class="left-center-right">
      <div class="left"></div>
      <div class="center">
        <h2>网格布局解决方案</h2>
        1.这是三栏布局的网格布局解决方案;
        2.这是三栏布局的网格布局解决方案;
        3.这是三栏布局的网格布局解决方案;
        4.这是三栏布局的网格布局解决方案;
        5.这是三栏布局的网格布局解决方案;
        6.这是三栏布局的网格布局解决方案;
      </div>
      <div class="right"></div>
    </article>
  </section>

将浏览器窗口压窄,可以看到变化。由于上面的代码中设置的高度是min-width,而不是设置的固定高度width,所以现在看到的也就是高度不固定的情况。


5种布局方案在高度不固定的情况下呈现出不同的效果的分析

  • 浮动解决方案中:center部分会被内容撑高并向两边扩充,两边盒子的大小不受影响。
  • 绝对定位解决方案中:center部分会被内容撑高,不向两边扩充,两边盒子大小不受影响。
  • flexbox解决方案中:center部分会被内容撑高,并且两边的盒子与center高度始终保持一致。

这是因为在flex布局中,align-items属性默认为stretch,如果设置为:align-items: center;或align-items: start;或align-items: end;或其他值,那么就不会自动保持一样高。参见:MDN: flex 布局的基本概念

  • 表格布局解决方案中:center部分会被内容撑高,并且两边的盒子与center高度始终保持一致。
  • 网格布局解决方案中:盒子大小都不变化,文字直接超出center部分。


链接: https://www.fly63.com/article/detial/3421

css两端对齐——div+css布局实现2端对齐的4种方法总结

css两端对齐,通过margin负值、justify、space-between、column-count等多种方式来实现css的两端对齐。

flex布局,学习flex弹性布局总结

flex布局(Flexible Box)是对界面css盒模型的一个优化,适应不同尺寸的屏幕,被定义flex的容器可以控制子元素的排列方向和大小,能够用更简单清晰的代码来完成复杂的布局。

html通过css,js实现div悬浮效果总汇,如原生JS实现滚动到一定位置实现div悬浮

在我们的实际开发中,经常会遇到页面中需要悬浮效果,本文将介绍通过fixed的实现,以及通过原生Js实现滚动到一定位置,实现div的悬浮

理解css中Grid布局,在项目中如何实现grid页面布局

CSS中Grid是一种二维网格式布局方式,Grid是为了解决二维布局问题而创建的CSS模块。使用Gird的好处,Gird在浏览器的兼容性,Grid布局的示例源码...

使用flex实现5种常用布局

flex常用布局:经典的上-中-下布局。在上-中-下布局的基础上,加了左侧定宽 sidebar。左边是定宽 sidebar,右边是上-中-下布局。还是上-中-下布局,区别是 header 固定在顶部,不会随着页面滚动。左侧 sidebar 固定在左侧且与视窗同高,当内容超出视窗高度时,在 sidebar 内部出现滚动条。

你以为 CSS 只是个简单的布局?

本文中,所有的图形都是在单个标签内实现的,大量使用了 CSS3 中的 ::before 、::after 伪元素,transparent 、border,多重线性与径向渐变,多重内外阴影,如果你的效果不尽人意,请尝试在 Chrome 浏览器下预览。

html实现时间轴_纯css实现响应式竖着/垂直时间抽布局效果

html实现用时间点来展示事件发生点来代替用table展示一条条数据,能够给人清晰、一目了然能够看清事情发生的过程,UI页面也显示的那么清晰。如何用css+html做出时间轴展示事件点的?

基于jquery 横向/纵向 时间轴插件推荐

这篇文章主要整理一些关于基于jquery,实现横向/纵向时间轴的插件推荐:jquery.jqtimeline插件、timeline.js插件、Timeglider.js插件、Melon HTML5 、jQuery Timelinr、Timeline Porfolio

CSS position 属性_css中常用position定位属性介绍

css可以通过为元素设置一个position属性值,position定位是指定位置的定位,以下为常用的几种:static(静态定位)、relative定位(相对定位)、absolute定位(绝对定位)、fixed(固定定位)、inherit定位

css圣杯布局的实现方式

css圣杯布局思路:外面一个大div,里面三个小div(都是浮动)。实现左右两栏宽度固定,中间宽度自适应。中间栏优先渲染。

点击更多...

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!