6种方式实现双栏布局和三栏布局

66 阅读3分钟

两栏布局

1.利用浮动,margin实现

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    .container {
      height: 200px;
      line-height: 200px;
    }
    .left {
      width: 200px;
      height: 100%;
      background: rebeccapurple;
      text-align: center;
      float: left;
    }
    .right {
      height: 100%;
      margin-left: 200px;
      background: pink;
      text-align: center;
    }
  </style>

  <body>
    <div class="container">
      <div class="left">left固定</div>
      <div class="right">right自适应</div>
    </div>
  </body>
</html>

2.浮动+BFC

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    .container {
      height: 200px;
      line-height: 200px;
    }
    .left {
      width: 200px;
      height: 100%;
      background: rgb(177, 141, 214);
      text-align: center;
      float: left;
    }
    .right {
      height: 100%;
      /* 触发BFC */
      overflow: hidden;
      background: rgb(205, 29, 58);
      text-align: center;
    }
  </style>

  <body>
    <div class="container">
      <div class="left">left固定</div>
      <div class="right">right自适应</div>
    </div>
  </body>
</html>

3. 定位

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    .container {
      height: 200px;
      line-height: 200px;
      position: relative;
    }
    .left {
      width: 200px;
      height: 100%;
      background: orange;
      text-align: center;
      position: absolute;
    }
    .right {
      height: 100%;
      background: blue;
      text-align: center;
      position: absolute;
      left: 200px;
      right: 0;
    }
  </style>

  <body>
    <div class="container">
      <div class="left">left固定</div>
      <div class="right">right自适应</div>
    </div>
  </body>
</html>

4.flex

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    .container {
      height: 200px;
      line-height: 200px;
      display: flex;
    }
    .left {
      width: 200px;
      height: 100%;
      background: orange;
      text-align: center;
    }
    .right {
      height: 100%;
      background: blue;
      text-align: center;
      flex: 1;
    }
  </style>

  <body>
    <div class="container">
      <div class="left">left固定</div>
      <div class="right">right自适应</div>
    </div>
  </body>
</html>

5.table布局

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    .container {
      width: 100%;
      height: 200px;
      line-height: 200px;
      display: table;
    }
    .left {
      width: 200px;
      height: 100%;
      background: orange;
      text-align: center;
      display: table-cell;
    }
    .right {
      height: 100%;
      background: blue;
      text-align: center;
      display: table-cell;
    }
  </style>

  <body>
    <div class="container">
      <div class="left">left固定</div>
      <div class="right">right自适应</div>
    </div>
  </body>
</html>

table布局 子容器会占满父容器空间 ,但是父容器必须显示设置宽度

6.grid

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    .container {
      display: grid;
      grid-template-columns: 200px 1fr;
      line-height: 100px;
    }
    .left {
      background-color: green;
    }
    .right {
      background-color: red;
    }
  </style>

  <body>
    <div class="container">
      <div class="left">left固定</div>
      <div class="right">right自适应</div>
    </div>
  </body>
</html>

网格布局grid非常灵活

三栏布局

1.浮动+margin

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    .container {
      height: 200px;
      line-height: 200px;
    }
    .left {
      width: 100px;
      height: 100%;
      float: left;
      background: orange;
      text-align: center;
    }
    .right {
      width: 100px;
      height: 100%;
      float: right;
      background: blue;
      text-align: center;
    }

    .center {
      height: 100%;
      margin-left: 100px;
      margin-right: 100px;
      background: oldlace;
    }
  </style>

  <body>
    <div class="container">
      <div class="left">left固定</div>
      <div class="right">right固定</div>
      <div class="center">中间自适应</div>
    </div>
  </body>
</html>

2.浮动+BFC

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    .container {
      height: 200px;
      line-height: 200px;
    }
    .left {
      width: 100px;
      height: 100%;
      float: left;
      background: orange;
      text-align: center;
    }
    .right {
      width: 100px;
      height: 100%;
      float: right;
      background: blue;
      text-align: center;
    }

    .center {
      height: 100%;
      /* 触发BFC */
      overflow: hidden;
      background: oldlace;
      text-align: center;
    }
  </style>

  <body>
    <div class="container">
      <div class="left">left固定</div>
      <div class="right">right固定</div>
      <div class="center">中间自适应</div>
    </div>
  </body>
</html>

3.flex

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    .container {
      height: 200px;
      line-height: 200px;
      display: flex;
    }
    .left {
      width: 100px;
      height: 100%;
      background: orange;
      text-align: center;
    }
    .right {
      width: 100px;
      height: 100%;
      background: blue;
      text-align: center;
    }

    .center {
      height: 100%;
      flex: 1;
      background: oldlace;
      text-align: center;
    }
  </style>

  <body>
    <div class="container">
      <div class="left">left固定</div>
      <div class="center">中间自适应</div>
      <div class="right">right固定</div>
    </div>
  </body>
</html>

4.table布局

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    .container {
      width: 100%;
      height: 200px;
      line-height: 200px;
      display: table;
    }
    .left {
      width: 100px;
      height: 100%;
      display: table-cell;
      background: orange;
      text-align: center;
    }
    .right {
      width: 100px;
      height: 100%;
      display: table-cell;
      background: blue;
      text-align: center;
    }

    .center {
      height: 100%;
      display: table-cell;
      background: oldlace;
      text-align: center;
    }
  </style>

  <body>
    <div class="container">
      <div class="left">left固定</div>
      <div class="center">中间自适应</div>
      <div class="right">right固定</div>
    </div>
  </body>
</html>

5.定位

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    .container {
      height: 200px;
      line-height: 200px;
      position: relative;
    }
    .left {
      width: 100px;
      height: 100%;
      background: orange;
      text-align: center;
      position: absolute;
      left: 0;
    }
    .right {
      width: 100px;
      height: 100%;
      background: blue;
      text-align: center;
      position: absolute;
      right: 0;
    }

    .center {
      height: 100%;
      background: oldlace;
      text-align: center;
      margin: 0 100px;
    }
  </style>

  <body>
    <div class="container">
      <div class="left">left固定</div>
      <div class="right">right固定</div>
      <div class="center">中间自适应</div>
    </div>
  </body>
</html>

6.grid

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    .container {
      /* width: 500px; */
      height: 200px;
      line-height: 200px;
      display: grid;
      grid-template-columns: 200px 1fr 200px;
    }
    .left {
      height: 100%;
      background: orange;
      text-align: center;
    }
    .right {
      height: 100%;
      background: blue;
      text-align: center;
    }

    .center {
      height: 100%;
      background: oldlace;
      /* text-align: center; */
    }
  </style>

  <body>
    <div class="container">
      <div class="left">left固定</div>
      <div class="center">中间自适应</div>
      <div class="right">right固定</div>
    </div>
  </body>
</html>

参考文献

CSS多栏布局-两栏布局和三栏布局_css两栏布局-CSDN博客