【CSS布局】三栏布局,左右两栏固定宽度中间自适应

179 阅读1分钟

中间栏要放到文档流前面,保证先行渲染。

解法一:margin负值法

左右两栏均左浮动并采用负值margin,中间栏被一个宽度为100%且浮动的元素包含,中间栏设置margin值,类似于双飞翼布局,中间栏不变,将内容部分为两边腾开位置

<!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>margin负值法</title>
    <style>
      #container {
        float: left;
        width: 100%;

        height: 100px;
        background-color: aquamarine;
      }

      #center {
        margin: 0 200px;
      }

      #left {
        float: left;
        margin-left: -100%;
        width: 200px;
        height: 100px;
        background-color: cornflowerblue;
      }

      #right {
        float: left;
        margin-left: -200px;
        width: 200px;
        height: 100px;
        background-color: violet;
      }
    </style>
  </head>
  <body>
    <div id="container">
      <div id="center"></div>
    </div>

    <div id="left"></div>
    <div id="right"></div>
  </body>
</html>

解法二: 绝对定位法

左右两栏采用绝对定位,中间栏通过margin撑开左右间距

<!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;
      }
      
      #center {
        margin: 0 200px;
        width: 100%;
        height: 100px;
        background-color: green;
      }

      #left {
        position: absolute;
        top: 0;
        left: 0;
        width: 200px;
        height: 100px;
        background-color: cornflowerblue;
      }

      #right {
        position: absolute;
        top: 0;
        right: 0;
        width: 200px;
        height: 100px;
        background-color: violet;
      }
    </style>
  </head>
  <body>
    <div style="position: relative;">
      <div id="center"></div>

      <div id="left"></div>
      <div id="right"></div>
    </div>
  </body>
</html>

解法三: flex布局

通过flex布局,中间栏设flex:1

<!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>flex法</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }

      #container {
        display: flex;
      }

      #center {
        flex: 1;
        width: 100%;
        height: 100px;
        background-color: #00c9a7;
      }

      #left {
        width: 200px;
        height: 100px;
        background-color: #e9f3ec;
      }

      #right {
        width: 200px;
        height: 100px;
        background-color: #608e6f;
      }
    </style>
  </head>
  <body>
    <div id="container">
      <div id="left"></div>
      <div id="center"></div>
      <div id="right"></div>
    </div>
  </body>
</html>

解法四:左右浮动法

将左右两栏分别左右浮动,中间栏用margin撑开间距,注意中间栏需要放在最下面。

<!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>
      #center {
        margin: 0 200px;
        height: 100px;
        background-color: #00c9a7;
      }

      #left {
        float: left;
        width: 200px;
        height: 100px;
        background-color: #e9f3ec;
      }

      #right {
        float: right;
        width: 200px;
        height: 100px;
        background-color: #608e6f;
      }
    </style>
  </head>
  <body>
    <div id="left"></div>
    <div id="right"></div>
    <div id="center"></div>
  </body>
</html>

解法五: grid布局

<!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>grid布局法</title>
    <style>
      #container {
        display: grid;
        width: 100%;
        grid-template-columns: 200px auto 200px;
      }

      #center {
        width: 100%;
        height: 100px;
        background-color: #ffaec1;
      }

      #left {
        width: 200px;
        height: 100px;
        background-color: #ffb7a6;
      }

      #right {
        width: 200px;
        height: 100px;
        background-color: #ffacd6;
      }
    </style>
  </head>
  <body>
    <div id="container">
      <div id="left"></div>
      <div id="center"></div>
      <div id="right"></div>
    </div>
  </body>
</html>