css

flexbox改写的一个响应式网页布局实例

September 16, 2019
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js">
  <!--<![endif]-->
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>弹性盒子</title>
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="" />
    <style>
      body {
        margin: 0;
        background: #f1f1f1;
      }
      * {
        box-sizing: border-box;
      }
      .container {
        text-align: center;
        display: flex;
        flex-direction: column;
        margin: 15px;
      }
      .header {
        background: #ffffff;
      }
      .header h1 {
        font-size: 50px;
      }
      .header ul {
        list-style-type: none;
        display: flex;
        justify-content: space-between;
        background: #333;
        margin: 0;
        padding: 0;
      }
      .header ul li {
        flex: 1;
        display: flex;
      }
      .header ul li a {
        flex: 1;
        text-decoration: none;
        color: white;
        padding: 15px;
      }
      .header ul li a:hover {
        background: #ddd;
        color: black;
      }
      .body {
        display: flex;
        align-items: flex-start;
        text-align: center;
        margin-top: 20px;
        text-align: left;
      }
      .content {
        flex: 3.5;
      }
      .article {
        background: #ffffff;
        padding: 20px;
        margin-bottom: 20px;
      }
      .fakeImg {
        height: 200px;
        background: #aaa;
        padding: 20px;
      }
      .sider {
        flex: 1;
        margin-left: 20px;
      }
      .sider > * {
        background: #ffffff;
        margin-bottom: 15px;
        padding: 20px;
      }
      .fakeImg2 {
        height: 100px;
        background: #aaa;
        padding: 20px;
      }
      .footer {
        background: #ddd;
        padding: 20px;
      }
      @media all and (max-width: 800px) {
        .body {
          flex-direction: column;
        }
        .sider {
          width: 100%;
          margin-left: 0;
        }
      }
    </style>
  </head>
  <body>
    <!--[if lt IE 7]>
      <p class="browsehappy">
        You are using an <strong>outdated</strong> browser. Please
        <a href="#">upgrade your browser</a> to improve your experience.
      </p>
    <![endif]-->
    <div class="container">
      <div class="header">
        <div>
          <h1>我的网页</h1>
          <p>重置浏览器大小看效果</p>
        </div>
        <ul>
          <div style="display: flex">
            <li><a href="#">链接</a></li>
            <li><a href="#">链接</a></li>
            <li><a href="#">链接</a></li>
          </div>
          <div>
            <li><a href="#">链接</a></li>
          </div>
        </ul>
      </div>
 
      <div class="body">
        <div class="content">
          <div class="article">
            <h2>文章标题</h2>
            <h5>2019 年 4 月 17日</h5>
            <div class="fakeImg">图片</div>
            <p>一些文本. . .</p>
            <p>
              菜鸟教程 - 学的不仅是技术,更是梦想!菜鸟教程 -
              学的不仅是技术,更是梦想!菜鸟教程 -
              学的不仅是技术,更是梦想!菜鸟教程 - 学的不仅是技术,更是梦想!
            </p>
          </div>
          <div class="article">
            <h2>文章标题</h2>
            <h5>2019 年 4 月 17日</h5>
            <div class="fakeImg">图片</div>
            <p>一些文本. . .</p>
            <p>
              菜鸟教程 - 学的不仅是技术,更是梦想!菜鸟教程 -
              学的不仅是技术,更是梦想!菜鸟教程 -
              学的不仅是技术,更是梦想!菜鸟教程 - 学的不仅是技术,更是梦想!
            </p>
          </div>
        </div>
 
        <div class="sider">
          <div>
            <h3 style="margin-top: 0">关于我</h3>
            <div class="fakeImg2">图片</div>
            <p>关于我的一些信息..</p>
          </div>
          <div>
            <h3>热门文章</h3>
            <div class="fakeImg2">图片</div>
            <div class="fakeImg2">图片</div>
            <div class="fakeImg2">图片</div>
          </div>
          <div>
            <h3>关注我</h3>
            <p>一些文本..</p>
          </div>
        </div>
      </div>
 
      <div class="footer"><h2>底部区域</h2></div>
    </div>
 
    <script src="" async defer></script>
  </body>
</html>

整体效果如下,而且当屏幕宽度小于800px时右侧边栏变成垂直上下显示。