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

 

 解决方案:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>test1</title>
    <style media="screen">
      .left {
        width: 300px;
        background-color: blue;
        height: 200px;
        text-align: center;
      }
      .center {
        background-color: #ccc;
        height: 200px;
        text-align: center;
      }
      .right {
        text-align: center;
        width: 300px;
        background-color: red;
        height: 200px;
      }
    </style>
  </head>
  <body>
    <!-- 浮动 -->
    <section class="sec1">
      <style media="screen">
        .sec1 .left {
          float: left;
        }
        .sec1 .right {
          float: right;
        }
      </style>
      <div class="left">left</div>
      <div class="right">right</div>
      <div class="center">浮动 : center--center--center--</div>
    </section>

    <!-- 绝对定位 -->
    <section class="sec2">
      <style media="screen">
      .sec2 {
        margin-top: 20px;
        position: relative;
      }
      .sec2 > div {
        position: absolute;
      }
      .sec2 .left {
        left: 0;
      }
      .sec2 .right {
        right: 0;
      }
      .sec2 .center {
        left: 300px;
        right: 300px;
      }
      </style>
      <div class="left">left</div>
      <div class="center">绝对定位 : center--center--center--</div>
      <div class="right">right</div>
    </section>

    <!-- flex布局 -->
    <section class="sec3">
      <style media="screen">
        .sec3 {
          margin-top: 240px;
          display: flex;
        }
        .sec3 .center {
          flex: 1;
        }
      </style>
      <div class="left">left</div>
      <div class="center">flex : center--center--center--</div>
      <div class="right">right</div>
    </section>

    <!-- 表格布局 -->
    <section class="sec4">
      <style media="screen">
        .sec4 {
          margin-top: 20px;
          display: table;
          width: 100%
        }
        .sec4 > div {
          display: table-cell;
        }
      </style>
      <div class="left">left</div>
      <div class="center">表格布局 : center--center--center--</div>
      <div class="right">right</div>
    </section>

    <!-- 网格布局 -->
    <!-- CSS3下一代标准 -->
    <section class="sec5">
      <style media="screen">
        .sec5 {
          margin-top: 20px;
          display: grid;
          width: 100%;
          /* grid-template-rows: 100px; */
          grid-template-columns: 300px auto 300px;
        }
      </style>
      <div class="left">left</div>
      <div class="center">网格布局 : center--center--center--</div>
      <div class="right">right</div>
    </section>
  </body>
</html>
<!--
1、以上各种布局的优缺点,以及比较推荐哪一种方案
  (1)浮动 :
    - 缺点 :浮动是脱离文档流的,有些时候需要清除浮动,需要很好的处理浮动周边元素的关系
    - 优点 :兼容性比较好
  (2)绝对定位 :
    - 缺点 :该布局脱离文档流,所以子元素也必须脱离文档流,因此可使用性比较差
    - 优点 :快捷,比较不容易出问题
  (3)flex :
    - 缺点 :兼容性比较差(css3的属性),不兼容IE8及以下
    - 优点 :非常有效的解决了浮动和绝对定位的问题
  (4)表格布局 :
    - 缺点 :操作繁琐,当三栏中其中某一栏高度超出时,其他两栏的高度也会自动跟着调整(不符合某些场景)
    - 优点 :兼容性非常好,补缺了flex布局兼容的问题
  (5)网格布局 :
    - 新技术,CSS3下一代局部方案
2、如果去掉"高度已知", 以上哪种方案同样适用?
  只有 flex布局 和 表格布局 同样适用 
 -->

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值