-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path四分.html
187 lines (162 loc) · 4.49 KB
/
四分.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
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
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
/*<!--假设此盒子宽高为400px-->*/
.box {
margin: 100px auto;
width: 400px;
height: 400px;
position: relative;
background-color: #ccc;
}
/*方案一,利用伪类,此方法只给演示,不是用于布局*/
.demo:after, .demo:before {
content: "";
position: absolute;
background-color: #fff;
}
.demo:after {
width: 10px;
top: 0;
left: 50%;
height: 100%;
transform: translateX(-50%);
}
.demo:before {
height: 10px;
width: 100%;
top: 50%;
left: 0;
transform: translateY(-50%);
}
/*方案一 table 此方法在高度存在不能平分高度的缺陷。如果高度不固定的情况。下面的每个div,由于table的特性。高度只能跟随内容高度来高*/
.table {
display: table;
table-layout: fixed;
height: auto;
text-align: center;
vertical-align: middle;
}
.table-row {
display: table-row;
}
.table-row > div {
display: table-cell;
width: 50%;
background-color: blue;
background-clip: content-box;
padding-right: 5px;
padding-top: 5px;
}
.table .table-row:first-child > div {
padding-top: 0px;
padding-bottom: 5px;
}
.table-row div:nth-child(2n) {
padding-left: 5px;
padding-right: 0;
}
/*方案二 flex 此方法在高度上完全可以平分高度,但在移动端还是flex的兼容性问题存在*/
.flex {
display: flex;
flex-wrap: wrap;
}
.flex > div {
background-color: blue;
width: 50%;
box-sizing: border-box;
padding-right: 5px;
padding-bottom: 5px;
background-clip: content-box;
}
.flex div:nth-child(even) {
padding-left: 5px;
padding-right: 0;
}
.flex div:last-child, .flex div:nth-child(3) {
padding-top: 5px;
padding-bottom: 0;
}
/*方案三 float 此方法一样,在高度上也能平分*/
.float div {
float: left;
width: 50%;
background-color: blue;
padding-left: 5px;
padding-bottom: 5px;
box-sizing: border-box;
height: 50%;
background-clip: content-box;
}
.float div:nth-child(odd) {
padding-right: 5px;
padding-left: 0;
}
.float div:last-child, .float div:nth-child(3) {
padding-top: 5px;
padding-bottom: 0;
}
/*absolute 纯属娱乐*/
.absolute div {
position: absolute;
width: 50%;
height: 50%;
background-color: blue;
padding-right: 5px;
padding-bottom: 5px;
background-clip: content-box;
box-sizing: border-box;
}
.absolute div:nth-child(2n) {
left: 50%;
padding-left: 5px;
padding-right: 0;
}
.absolute div:last-child, .absolute div:nth-child(3) {
top: 50%;
padding-top: 5px;
padding-bottom: 0;
}
</style>
</head>
<body>
<h3>演示,非布局</h3>
<div class="box demo"></div>
<h3>table布局,问题:不能高度自适应</h3>
<div class="box table">
<div class="table-row">
<div>1</div>
<div>2</div>
</div>
<div class="table-row">
<div>3</div>
<div>4</div>
</div>
</div>
<h3>flex布局宽度和高度都能自适应。使用了伪类选择器</h3>
<div class="box flex">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
<h3>float布局宽度和高度都能自适应。使用了伪类选择器</h3>
<div class="box float">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
<h3>absolute布局。纯属娱乐</h3>
<div class="box absolute">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
<h3>还可以有tranform:translate方法,position:relative方法</h3>
</body>
</html>