CSS-008-布局套路

布局套路

左右布局

float + clearfix

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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<style>
.parent{
border:1px solid red;
}
.child{
height: 30px;
float:left;
}

.child:nth-child(1){
width:30%;
background:blue;
}

.child:nth-child(2){
width:70%;
background:red
}

.clearfix::after{
content:'';
display:block;
clear:both;
}

</style>
</head>
<body>
<div class="parent clearfix">
<div class="child"></div>
<div class="child"></div>
</div>
</body>
</html>

实战PC页面的套路

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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<!DOCTYPE html>
<html>
<head>
<style>
*{margin:0;padding:0;box-sizing:border-box;}
.main-center{margin:0 auto;}
.parent{
border:1px solid red;
margin:0 auto;
background:#ddd;
min-width:600px;
}
.child{
float:left;
}

.child:nth-child(1){
width:100px;
background:#333;
color:white;
text-align:center;
height:36px;
line-height:36px;
}

.child:nth-child(2){
float:right;
}

.content{
border:1px solid black;
margin-right:10px;
}

.nav{
line-height:24px;
padding:6px 0;
}

.navItem{
float:left;
margin-left:20px;
}

.clearfix::after{
content:'';
display:block;
clear:both;
}
.clearfix{
zoom:1; /* 兼容IE6 */
}

.banner{
width: 800px;
height: 300px;
background:#888;
margin-top: 10px;
margin-left:auto;
margin-right:auto;
}

.pictures{
width: 800px;
margin:0 auto;
background:green;
}

.pictures .xxx{
margin-left:-4px;
margin-right:-4px;
}
.picture{
height: 194px;
width: 194px;
float:left;
margin:4px;
background:#ddd
}

.pictures2{
width: 800px;
margin:0 auto;
background:green;
display:flex;
flex-wrap:wrap;
justify-content:space-between;
}

.picture2{
height: 194px;
width: 194px;
margin-top:4px;
margin-bottom:4px;
background:#ddd
}

.pictures3{
width: 800px;
margin:0 auto;
background:green;
}

.pictures3 .xxx{
display:flex;
flex-wrap:wrap;
margin:0 -4px;
}

.picture3{
height: 194px;
width: 194px;
margin:4px;
background:#ddd;
border:1px solid red;
}

.pictures4{
width: 800px;
margin:0 auto;
background:green;
}

.pictures4 .xxx{
display:flex;
flex-wrap:wrap;
margin:0 -4px;
}

.picture4{
height: 194px;
width: calc(25% - 8px);
margin:4px;
background:#ddd;
border:1px solid red;
}

.art{
background:#ddd;
height: 300px;
width: 800px;
margin:0 auto;
}

.art .sider{
float:left;
width: calc(33.333333% - 20px);
margin-right:20px;
}

.art .sider .child-ad{
height: 300px;
border:1px solid black;
}

.art .main{
float:left;
height: 300px;
border:1px solid black;
width:66.66666%;
}
</style>
</head>
<body>
<div class="parent clearfix">
<div class="child">logo</div>
<div class="child">
<div class="nav clearfix">
<div class="navItem">导航1</div>
<div class="navItem">导航2</div>
<div class="navItem">导航3</div>
<div class="navItem">导航4</div>
<div class="navItem">导航5</div>
</div>
</div>
</div>

<div class="banner"></div>

<h3 style="margin:0 auto;width:800px;">float+margin负边距 样品展示区</h3>
<div class="pictures">
<div class="xxx clearfix">
<div class="picture"></div>
<div class="picture"></div>
<div class="picture"></div>
<div class="picture"></div>
<div class="picture"></div>
<div class="picture"></div>
<div class="picture"></div>
<div class="picture"></div>
</div>
</div>

<h3 style="margin:0 auto;width:800px;">flex 样品展示区</h3>
<p style="margin:0 auto;width:800px;">隐藏了bug 如果是7个 第二行就会很难看</p>
<div class="pictures2">
<div class="picture2"></div>
<div class="picture2"></div>
<div class="picture2"></div>
<div class="picture2"></div>
<div class="picture2"></div>
<div class="picture2"></div>
<div class="picture2"></div>
<div class="picture2"></div>
</div>

<h3 style="margin:0 auto;width:800px;">flex 样品展示区:修复bug</h3>
<div class="pictures3">
<div class="xxx">
<div class="picture3"></div>
<div class="picture3"></div>
<div class="picture3"></div>
<div class="picture3"></div>
<div class="picture3"></div>
<div class="picture3"></div>
<div class="picture3"></div>
</div>
</div>


<h3 style="margin:0 auto;width:800px;">flex 样品展示区:使用 calc</h3>
<div class="pictures4">
<div class="xxx">
<div class="picture4"></div>
<div class="picture4"></div>
<div class="picture4"></div>
<div class="picture4"></div>
<div class="picture4"></div>
<div class="picture4"></div>
<div class="picture4"></div>
<div class="picture4"></div>
</div>
</div>

<div class="art">
<div class="sider">
<div class="child-ad">广告1</div>
</div>
<div class="main">广告2</div>
</div>
</body>
</html>

手机端

  • 直接将上面的样式 媒体查询 + flex即可