1. 程式人生 > 實用技巧 >CSS 聖盃/雙飛翼佈局

CSS 聖盃/雙飛翼佈局

常用佈局

聖盃佈局

html結構
 <body>
    <div class="container clearfix">
      <div class="center"></div>
      <div class="left"></div>
      <div class="right"></div>
    </div>
    <div>
      <div>123</div>
      <div>123</div>
      <div>123</div>
    </div>
  </body>
css:
container

把左右兩邊距離空出來

.container {
    padding: 0 200px;
}
center

設定widh為100%,即充滿整個容器

.center {
    width: 100%;
}
浮動

設定left,center,right左浮動。
清除container浮動,保證後面的div正常佈局

.left, .center, .right {
    float: left;
}
.clearfix::after {
    display: block;
    height: 0;
    content: "";
    visibility: hidden;
    clear: both
}
.clearfix {
    *zoom: 1;
}
left

使用margin-left: -100%;

.left {
    position: relative;
    margin-left: -100%;
    left: -200px;
}

使用margin-right: -200px;;

.right {
    margin-right: -200px;
}

為什麼不用相對定位來移動

.right {
position: relative;
left: -200px;
}

會在第二行向左移動200px,不能和center同行

程式碼
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style type="text/css">
      html,
      body {
        height: 100%;
        overflow: hidden;
        margin: 0;
        padding: 0;
      }

      .container {
        padding: 0 200px;
      }
      .left,
      .right {
        width: 200px;
        min-height: 200px;
      }
      .left {
        background: lightgreen;
      }
      .right {
        background: lightgrey;
      }
      .center {
        width: 100%;
        min-height: 400px;
        background: lightsalmon;
      }

      .left,
      .center,
      .right {
        float: left;
      }
      .left {
        position: relative;
        margin-left: -100%;
        left: -200px;
      }
      .right {
        margin-right: -200px;
        /* position: relative; */
        /* left: -200px; */
      }
      .clearfix::after {
        display: block;
        height: 0;
        content: "";
        visibility: hidden;
        clear: both;
      }
      .clearfix {
        *zoom: 1;
      }
    </style>
  </head>
  <body>
    <div class="container clearfix">
      <div class="center"></div>
      <div class="left"></div>
      <div class="right"></div>
    </div>
    <div>
      <div>123</div>
      <div>123</div>
      <div>123</div>
    </div>
  </body>
</html>

雙飛翼佈局

HTML結構

 <body>
    <div class="container">
      <div class="cemter"></div>
    </div>
    <div class="left"></div>
    <div class="right"></div>
  </body>

container

.container {
    width: 100%;
    background: lightseagreen;
    height: 700px;
}

center

.center {
    min-height: 400px;
    background: lightpink;
    margin: 0 200px;
}

浮動

.container, .left, .right {
    float: left;
}
.clearfix::after {
    display: block;
    height: 0;
    content: "";
    visibility: hidden;
    clear: both;
}
.clearfix {
    *zoom: 1;
}

left

.left {
        margin-left: -100%;
        width: 200px;
        min-height: 400px;
        background: lightskyblue;
      }

right

.right {
        margin-left: -200px;
        width: 200px;
        min-height: 400px;
        background: lightgoldenrodyellow;
      }
程式碼
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style type="text/css">
      html,
      body {
        width: 100%;
        margin: 0;
        padding: 0;
        overflow: hidden;
      }
      .container {
        width: 100%;
        background: lightseagreen;
        height: 700px;
      }
      .center {
        min-height: 400px;
        background: lightpink;
        margin: 0 200px;
      }
      .container,
      .left,
      .right {
        float: left;
      }
      .left {
        margin-left: -100%;
        width: 200px;
        min-height: 400px;
        background: lightskyblue;
      }
      .right {
        margin-left: -200px;
        width: 200px;
        min-height: 400px;
        background: lightgoldenrodyellow;
      }
      .clearfix::after {
        display: block;
        height: 0;
        content: "";
        visibility: hidden;
        clear: both;
      }
      .clearfix {
        *zoom: 1;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="center"></div>
    </div>
    <div class="left"></div>
    <div class="right"></div>
  </body>
</html>

圖解: