三栏布局的实现

一、要求

实现左右固定宽度,中间自适应的布局


image.png

二、实现

1. float布局

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>三栏布局</title>
  <style>
    .left {
      float: left;
      width: 400px;
      height: 200px;
      background-color: red;
    }
    .right {
      float: right;
      width: 400px;
      height: 200px;
      background-color: yellow;
    }
    .main {
      background-color: green;
      height: 200px;
      margin-left: 400px;
      margin-right: 400px;
    }
  </style>
</head>
<body>      
  <div class="container">
    <div class="left">left</div>
    <div class="right">right</div>
    <div class="main">main</div>
  </div>
</body>
</html>

2.BFC规则

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>三栏布局</title>
  <style>
    .left {
      float: left;
      width: 400px;
      height: 200px;
      background-color: red;
    }
    .right {
      float: right;
      width: 400px;
      height: 200px;
      background-color: yellow;
    }
    .main {
      background-color: green;
      height: 200px;
      overflow: hidden;
    }
  </style>
</head>
<body>      
  <div class="container">
    <div class="left">left</div>
    <div class="right">right</div>
    <div class="main">main</div>
  </div>
</body>
</html>

3.圣杯布局

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>三栏布局</title>
  <style>
    .left {
      float: left;
      width: 400px;
      height: 200px;
      margin-left: -100%;
      position: relative;
      left: -400px;
      background-color: red;
    }
    .right {
      float: left;
      width: 400px;
      height: 200px;
      margin-left: -400px;
      position: relative;
      right: -400px;
      background-color: yellow;
    }
    .main {
      float: left;
      width: 100%;
      height: 200px;
      background-color: blue;
    }
    .container {
      padding-left: 400px;
      padding-right: 400px;
    }
  </style>
</head>
<body>      
  <div class="container">
    <div class="main">main</div>
    <div class="left">left</div>
    <div class="right">right</div>
  </div>
</body>
</html>

4.双飞翼布局

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>三栏布局</title>
  <style>
    .main {
      float: left;
      width: 100%;
    }
    .content {
      height: 200px;
      margin-left: 400px;
      margin-right: 400px;
      background-color: green;
    }
    .main::after {
      display: block;
      content: '';
      font-size: 0;
      height: 0;
      clear: both;
      zoom: 1;
    }
    .left {
      float: left;
      width: 400px;
      height: 200px;
      margin-left: -100%;
      background-color: red;
    }
    .right {
      float: left;
      width: 400px;
      height: 200px;
      margin-left: -400px;
      background-color: blue;
    }   
  </style>
</head>
<body>      
  <div class="container">
    <div class="main">
      <div class="content">main</div>
    </div>
    <div class="left">left</div>
    <div class="right">right</div>
  </div>
</body>
</html>

5.flex布局

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>三栏布局</title>
  <style>
    .container {
      display: flex;
      flex-direction: row;
    }
    .main {
      height: 200px;
      background-color: red;
      flex-grow: 1;
    }
    .left {
      height: 200px;
      order: -1;
      background-color: yellow;
      flex: 0 1 400px;
    }
    .right {
      height: 200px;
      background-color: green;
      flex: 0 1 400px;
    }
  </style>
</head>
<body>      
  <div class="container">
    <div class="main">main</div>
    <div class="left">left</div>
    <div class="right">right</div>
  </div>
</body>
</html>

6. 绝对定位布局

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>三栏布局</title>
  <style>
    .main {
      position: absolute;
      left: 400px;
      right: 400px;
      height: 200px;
      background-color: yellow;
    }
    .left {
      position: absolute;
      left: 0px;
      width: 400px;
      height: 200px;
      background-color: red;
    }
    .right {
      position: absolute;
      right: 0px;
      width: 400px;
      background-color: green;
      height: 200px;
    }
  </style>
</head>
<body>      
  <div class="container">
    <div class="main">main</div>
    <div class="left">left</div>
    <div class="right">right</div>
  </div>
</body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,815评论 1 92
  • 对于三栏布局,大家应该很熟悉了,像淘宝等电商网站,亦或是某网站的后台管理系统,都有三栏布局的身影。如果你还不知道三...
    ALOLONGHUN阅读 459评论 0 1
  • # 前言   布局方式是每个项目都必须使用到的,大部分新手看到设计稿的第一反应是: 从左到右,从上到下依次制作。对...
    果汁凉茶丶阅读 586评论 0 4
  • 三栏是CSS布局中常见的一种布局模式,顾名思义,就是将网页内容以三列的形式呈现。通常,三栏布局中的左栏和右栏是固定...
    bmwz110阅读 920评论 0 6
  • 今天阅读NO.2《人生效率手册》的第90页到142页:修练硬本领。 所谓硬本领,就是一项你很历害的本领,你一出手,...
    恋解向阳而生阅读 192评论 2 1