web前端入门到实战:CSS三栏布局的5种方法详解

本文详细介绍了CSS实现三栏布局的五种方法,包括浮动解决方案、绝对定位、flexbox、表格布局和网格布局。在高度不固定情况下,各种布局展现出不同效果:浮动布局中心区域会被内容撑高;绝对定位保持中心区域独立;flexbox和表格布局保持三栏高度一致;网格布局下盒子大小不变,内容超出中心区域。
摘要由CSDN通过智能技术生成

题目:假设高度已知,请写出三栏布局,其中左栏、右栏宽度各为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. 浮动解决方案

web前端开发学习Q-q-u-n: 767273102 ,分享学习的方法和需要注意的小细节,不停更新最新的教程和学习方法(详细的前端项目实战教学视频)
  <!-- 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 解决方案

web前端开发学习Q-q-u-n: 767273102 ,分享学习的方法和需要注意的小细节,不停更新最新的教程和学习方法(详细的前端项目实战教学视频)
  <!-- 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,所以现在看到的也就是高度不固定的情况。

对web前端这门技术感兴趣的小伙伴可以加入到我们的学习圈来,工作第六个年头了,与大家分享一些学习方法,实战开发需要注意的细节。767-273-102 秋裙。从零基础开始怎么样学好前端。都是一群有梦想的人,我们可能在不同的城市,但我们会一起结伴同行前端前端前端

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值