三列布局-圣杯与双飞翼

圣杯布局

1618625303259.jpg

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    * {
      padding: 0;
      margin: 0;
    }

    .container {
      padding-left: 200px;
      padding-right: 200px;
    }

    .left {
      float: left;
      width: 200px;
      height: 400px;
      background: red;
      margin-left: -100%;
      position: relative;
      left: -200px;
    }

    .center {
      float: left;
      width: 100%;
      height: 400px;
      background: yellow;
    }

    .right {
      float: left;
      width: 200px;
      height: 400px;
      background: blue;
      margin-left: -200px;
      position: relative;
      right: -200px;
    }
  </style>
</head>
<body>
<section class="container">
  <div class="center"></div>
  <div class="left"></div>
  <div class="right"></div>
</section>
</body>
</html>
复制代码

步骤

  • 先写center部分,width 100%
  • center,left,right都是左浮动
  • 父容器container设置padding-leftpadding-right
  • 设置margin-left为负值让leftright部分回到与center部分同一行
  • 设置相对定位,让leftright部分移动到两边

缺点

  • center部分的最小宽度不能小于left部分的宽度
  • 其中一列内容高度拉长,其他两列的高度不会自动填充

双飞翼布局

image.png

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    *{
      padding: 0;
      margin: 0;
    }
    .container {
      min-width: 600px;
    }
    .left {
      float: left;
      width: 200px;
      height: 400px;
      background: red;
      margin-left: -100%;
    }
    .center {
      float: left;
      width: 100%;
      height: 400px;
      background: yellow;
    }
    .center .inner {
      margin: 0 200px;
    }
    .right {
      float: left;
      width: 200px;
      height: 400px;
      background: blue;
      margin-left: -200px;
    }
  </style>
</head>
<body>
<section class="container">
  <div class="center">
    <div class="inner">双飞翼布局</div>
  </div>
  <div class="left"></div>
  <div class="right"></div>
</section>
</body>
</html>
复制代码

步骤

  • 先写center部分,width 100%
  • center,left,right都是左浮动
  • center部分增加一个内层div,并设margin-left,margin-right
  • 设置margin-left为负值让leftright部分回到与center部分同一行

缺点

  • 多加一层 dom 树节点

总结

image.png

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享