圣杯布局、双飞翼布局

200 阅读1分钟

圣杯布局和双飞翼布局是两边定宽,中间自适应的三栏布局的两种实现方式。

圣杯布局

缺点:

  1. center宽度过小,样式会错乱
  2. 样式较复杂

iShot2021-10-07 15.39.38.png

<!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>圣杯布局</title>
  <style>
    .container{
      padding: 0 180px 0 200px;
      height: 500px;
      min-width: 680px; // center宽度过小,样式会错乱
      background-color: gray;
    }

    .center{
      float: left;
      width: 100%;
      height: 500px;
      background-color: yellow;
    }

    .left{
      float: left;
      margin-left: -100%;
      position: relative;
      left: -200px;
      width: 200px;
      height: 500px;
      background-color: green;
    }

    .right{
      float: left;
      margin-left: -180px;
      position: relative;
      right: -180px;
      width: 180px;
      height: 500px;
      background-color: green;
    }
  </style>
</head>
<body>
  <h1>圣杯布局</h1>
  <div class="container">
    <div class="center">center</div>
    <div class="left">left</div>
    <div class="right">right</div>
  </div>
</body>
</html>

双飞翼布局

iShot2021-10-07 15.39.24.png

<!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>双飞翼布局</title>
  <style>
    .center{
      float: left;
      width: 100%;
      height: 500px;
      background-color: gray;
    }

    .inner{
      margin-left: 200px;
      margin-right: 180px;
      height: 500px;
      background-color: yellow;
    }

    .left{
      float: left;
      width: 200px;
      height: 500px;
      margin-left: -100%;
      background-color: green;
    }

    .right{
      float: left;
      width: 180px;
      height: 500px;
      margin-left: -180px;
      background-color: green;
    }
  </style>
</head>
<body>
  <h1>双飞翼布局</h1>
  <div class="center">
    <div class="inner">center inner</div>
  </div>
  <div class="left">left</div>
  <div class="right">right</div>
</body>
</html>