css三栏布局(中间先渲染)实现

74 阅读1分钟

1、父元素padding+子元素左浮动+margin+相对定位实现(圣杯布局)

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      .container,
      .left,
      .right {
        height: 500px;
      }
      .container {
        padding-left: 100px;
        padding-right: 50px;
      }
      .main {
        width: 100%;
        height: 500px;
        background-color: rebeccapurple;
        float: left;
      }
      .left {
        width: 100px;
        height: 500px;
        background-color: black;
        float: left;
        margin-left: -100%;
        position: relative;
        left: -100px;
      }
      .right {
        width: 50px;
        height: 500px;
        background-color: #abc;
        float: left;
        margin-left: -50px;
        position: relative;
        right: -50px;
      }
    </style>
    <div class="container">
      <div class="main"></div>
      <div class="left"></div>
      <div class="right"></div>
    </div>
  </body>
</html>
缺点:中间盒子同时小于左右两边会破裂

1、中间元素padding+左浮动+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>
    <body>
        <style>
            * {
                padding: 0;
                margin: 0;
            }
            .left {
                width: 300px;
                height: 300px;
                background-color: pink;
                float: left;
                margin-left: -100%;
            }
            .right {
                width: 200px;
                height: 300px;
                background-color: skyblue;
                float: left;
                margin-left: -200px;
            }
            .container {
                width: 100%;
                padding-left: 300px;
                padding-right: 200px;
                box-sizing: border-box;
                height: 300px;
                background-color: blue;
                float: left;
            }
        </style>
        <div class="father">
            <div class="container">container</div>
            <div class="left">left</div>
            <div class="right">right</div>
        </div>
    </body>
</html>

2、flex+order实现

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      .container,
      .left,
      .right {
        height: 500px;
      }
      .container {
        display: flex;
      }
      .main {
        width: 100%;
        height: 500px;
        background-color: rebeccapurple;
        flex: 1;
        order: 2;
      }
      .left {
        width: 100px;
        height: 500px;
        background-color: black;
        order: 1;
      }
      .right {
        width: 50px;
        height: 500px;
        background-color: #abc;
        order: 3;
      }
    </style>
    <div class="container">
      <div class="main"></div>
      <div class="left"></div>
      <div class="right"></div>
    </div>
  </body>
</html>

3、多加一个盒子(使用padding)+浮动+margin(双飞翼布局)

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      .container,
      .left,
      .right {
        height: 500px;
      }
      .main {
        width: 100%;
        height: 500px;
        background-color: rebeccapurple;
        float: left;
      }
      .left {
        width: 100px;
        height: 500px;
        background-color: black;
        float: left;
        margin-left: -100%;
      }
      .right {
        width: 50px;
        height: 500px;
        background-color: #abc;
        float: left;
        margin-left: -50px;
      }
      .insert {
        padding: 0 50px 0 100px;
      }
    </style>
    <div class="container">
      <div class="main">
        <div class="insert">
          <div>33</div>
        </div>
      </div>
      <div class="left"></div>
      <div class="right"></div>
    </div>
  </body>
</html>
优点:解决了圣杯破碎问题
缺点:需要多一个盒子包裹,可读性略低