-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2017-flex-layout.html
More file actions
187 lines (171 loc) · 4.42 KB
/
Copy path2017-flex-layout.html
File metadata and controls
187 lines (171 loc) · 4.42 KB
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
182
183
184
185
186
187
<!--
============================================================
Flex 弹性布局 - flex 实现各种布局,现代主流方案
布局方式:CSS3 Flexbox 弹性盒子
布局思想:
1. 父容器设为 display: flex,子元素自动成为弹性项目
2. 通过 justify-content 控制主轴对齐,align-items 控制交叉轴对齐
3. 子元素通过 flex 属性灵活分配空间,实现自适应布局
适用场景:
- 导航菜单水平/垂直排列
- 居中布局(水平垂直居中轻而易举)
- 两栏/三栏自适应布局
- 卡片列表等分布局
- 响应式布局的基础
技术要点:
- display: flex / inline-flex 声明弹性容器
- flex-direction 控制主轴方向(row/column)
- justify-content 主轴对齐方式
- align-items 交叉轴对齐方式
- flex-grow / flex-shrink / flex-basis 控制弹性
- flex-wrap 控制是否换行
可迁移性:
- 现代前端开发的主流布局方案
- 可迁移到任何现代网页和应用
- 移动端优先推荐使用
wjs created by 2017-03-15
============================================================
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flex 弹性布局</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: "Microsoft YaHei", sans-serif; color: #333; padding: 20px; }
h2 { margin: 20px 0 10px; color: #2c3e50; }
.demo-box {
margin-bottom: 30px;
padding: 15px;
border: 1px solid #ddd;
border-radius: 4px;
}
.flex-row {
display: flex;
gap: 10px;
}
.flex-row .item {
flex: 1;
height: 80px;
background: #3498db;
color: #fff;
display: flex;
align-items: center;
justify-content: center;
}
.flex-center {
display: flex;
justify-content: center;
align-items: center;
height: 120px;
background: #ecf0f1;
}
.flex-center .box {
width: 100px;
height: 60px;
background: #e74c3c;
color: #fff;
display: flex;
align-items: center;
justify-content: center;
}
.flex-column {
display: flex;
flex-direction: column;
gap: 10px;
width: 200px;
}
.flex-column .item {
height: 50px;
background: #2ecc71;
color: #fff;
display: flex;
align-items: center;
justify-content: center;
}
.flex-holy {
display: flex;
gap: 10px;
}
.flex-holy .left {
width: 150px;
height: 200px;
background: #9b59b6;
color: #fff;
display: flex;
align-items: center;
justify-content: center;
}
.flex-holy .main {
flex: 1;
height: 200px;
background: #f39c12;
color: #fff;
display: flex;
align-items: center;
justify-content: center;
}
.flex-holy .right {
width: 120px;
height: 200px;
background: #1abc9c;
color: #fff;
display: flex;
align-items: center;
justify-content: center;
}
.flex-nav {
display: flex;
justify-content: space-between;
align-items: center;
height: 60px;
padding: 0 20px;
background: #2c3e50;
color: #fff;
}
.flex-nav ul {
display: flex;
list-style: none;
gap: 20px;
}
</style>
</head>
<body>
<h1>Flex 弹性布局 Demo</h1>
<h2>1. 水平等分布局</h2>
<div class="demo-box flex-row">
<div class="item">flex: 1</div>
<div class="item">flex: 1</div>
<div class="item">flex: 1</div>
<div class="item">flex: 1</div>
</div>
<h2>2. 水平垂直居中</h2>
<div class="demo-box flex-center">
<div class="box">完美居中</div>
</div>
<h2>3. 垂直排列布局</h2>
<div class="demo-box flex-column">
<div class="item">item 1</div>
<div class="item">item 2</div>
<div class="item">item 3</div>
</div>
<h2>4. 三栏自适应布局</h2>
<div class="demo-box flex-holy">
<div class="left">左定宽</div>
<div class="main">中间自适应</div>
<div class="right">右定宽</div>
</div>
<h2>5. 导航栏布局</h2>
<div class="demo-box flex-nav">
<div class="logo">Logo</div>
<ul>
<li>首页</li>
<li>产品</li>
<li>服务</li>
<li>关于</li>
</ul>
</div>
</body>
</html>