# 双飞翼布局

# flex实现

<!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>
      html,
      body {
        height: 100%;
      }
      body {
        padding: 0;
        margin: 0;
        display: flex;
        flex-direction: column;
      }
      .header,
      .footer {
        background: red;
        flex: 0 0 auto;
      }
      #section {
        display: flex;
        flex: 1 1 auto;
        /* 
        flex-grow: 1;
        flex-shrink: 1;
        flex-basis: auto; */
      }
      .left {
        order: -1;
      }
      .left,
      .right {
        flex: 0 0 20%;
        background: blue;
      }
      .main {
        flex: auto; /* 1 1 auto, 自动放大,自动缩小*/
        background: yellow;
      }
    </style>
  </head>
  <body>
    <div class="header">this is header</div>
    <div id="section">
      <div class="main">
        <h1>this is main</h1>
        <h1>this is main</h1>
        <h1>this is main</h1>
        <h1>this is main</h1>
        <h1>this is main</h1>
        <h1>this is main</h1>
        <h1>this is main</h1>
        <h1>this is main</h1>
        <h1>this is main</h1>
        <h1>this is main</h1>
      </div>
      <div class="left">this is left</div>
      <div class="right">this is right</div>
    </div>
    <div class="footer">this is footer</div>
  </body>
</html>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
this is header

this is main

this is main

this is main

this is left
this is right

# margin负边距实现

缺点: 宽度过窄布局会崩塌,可以缩小屏幕试一下下面的demo

<!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>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      .clearfix:after {
        content: " ";
        display: block;
        visibility: hidden;
        clear: both;
        height: 0;
        font-size: 0;
      }
      .header,
      .footer {
        height: 50px;
      }
      .header {
        background: fuchsia;
      }
      .footer {
        background: blueviolet;
      }
      .container {
        overflow: hidden;
      }
      .main-wrap,
      .left,
      .right {
        float: left;
      }
      .main-wrap {
        width: 100%;// 撑满,让left,right换行,再通过负边距拉回适当的位置
      }
      .main {
        margin-left: 20%; // 腾开位置给left
        margin-right: 25%; // 腾开位置给right
        background: purple;
      }
      .left {
        width: 20%;
        background: red;
        margin-left: -100%; // 拉回最左边
      }
      .right {
        width: 25%;
        background: green;
        margin-left: -25%; // 拉回右边
      }
    </style>
  </head>
  <body>
    <div class="header">这是header</div>
    <div class="container">
      <div class="main-wrap">
        <div class="main">
          这是主区域
        </div>
      </div>
      <div class="left">这是left</div>
      <div class="right">这是right</div>
    </div>
    <div class="footer">这是footer</div>
  </body>
</html>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
这是header 这是header

这是主区域

这是主区域

这是left
这是right
上次更新: 2020-01-09 12:19:37