-
Notifications
You must be signed in to change notification settings - Fork 0
/
margin.html
237 lines (223 loc) · 7.08 KB
/
margin.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
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
<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
<meta charset="UTF-8">
<title>margin 的用法</title>
<link rel="stylesheet"
href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/atom-one-light.min.css">
<style>
h3 {
margin: 0;
}
pre {
width: 800px;
}
.container {
margin: 10px 0;
background-color: #efecf4;
border: 1px solid transparent;
width: 600px;
}
.container + .container {
margin-top: 20px;
}
.container2 {
margin: 10px 0;
background-color: #efecf4;
width: 600px;
}
.inner {
color: #fff;
background-color: #00b0e0;
}
</style>
</head>
<body>
<ol>
<li>
<h3>行内元素与块级元素(或行内块级元素) margin 行为表现的不一样</h3>
<div class="container">
<span class="inner" style="margin: 20px 50px;">行内元素 margin: 20px 50px;</span>
</div>
<div class="container">
<div class="inner" style="margin: 20px 50px;">
块级元素或行内块级元素 margin: 20px 50px;
</div>
</div>
<pre>
<code class="html">
<div class="container">
<span class="inner" style="margin: 20px 50px;">行内元素 margin: 20px 50px;</span>
</div>
<div class="container">
<div class="inner" style="margin: 20px 50px;">
块级元素或行内块级元素 margin: 20px 50px;
</div>
</div>
</code>
</pre>
</li>
<li>
<h3>margin 值为百分比时计算规则</h3>
<div class="container">
<div class="inner" style="margin: 2%;">
父元素 width 600px; 子元素 margin: 2%
</div>
</div>
<div class="container" style="height: 77px;">
<div class="inner" style="margin: 2%;position: absolute;">
父元素 width 600px; 子元素 margin: 2%;position: absolute;
</div>
</div>
<pre>
<code class="html">
<div class="container">
<div class="inner" style="margin: 2%;">
父元素 width 600px; 子元素 margin: 2%
</div>
</div>
<div class="container" style="height: 77px;">
<div class="inner" style="margin: 2%;position: absolute;">
父元素 width 600px; 子元素 margin: 2%;position: absolute;
</div>
</div>
</code>
</pre>
</li>
<li>
<h3>margin 重叠特性</h3>
<h4>兄弟元素重叠</h4>
<div class="container2">
<p class="inner" style="margin: 1em 0; line-height: 1em;">第一行</p>
<p class="inner" style="margin: 1em 0; line-height: 1em;">第二行</p>
</div>
<pre>
<code class="html">
<div class="container2">
<p class="inner" style="margin: 1em 0; line-height: 1em;">第一行</p>
<p class="inner" style="margin: 1em 0; line-height: 1em;">第二行</p>
</div>
</code>
</pre>
<h4>父子元素重叠</h4>
<div style="border-top: 1px solid #88C100;"></div>
<div class="container2">
<div class="inner" style="margin-top: 50px;">子元素</div>
</div>
<pre>
<code class="html">
<div class="container2">
<div class="inner" style="margin-top: 50px;">子元素</div>
</div>
</code>
</pre>
等同于给父元素设置 margin-top
<pre>
<code class="html">
<div class="container2" style="margin-top: 50px;">
<div class="inner">子元素</div>
</div>
</code>
</pre>
也等同于给父子元素同时设置 margin-top
<pre>
<code class="html">
<div class="container2" style="margin-top: 50px;">
<div class="inner" style="margin-top: 50px;">子元素</div>
</div>
</code>
</pre>
</li>
<li>
<h3>空block元素重叠</h3>
<div class="container">
<p class="inner" style="margin: 1em 0;"></p>
</div>
<pre>
<code class="html">
<div class="container">
<div class="inner" style="margin: 1em 0;"></div>
</div>
</code>
</pre>
</li>
<li>
<h3>margin auto 设置垂直居中</h3>
<div class="container" style="position: relative;height: 200px;">
<div class="inner"
style="position: absolute;left:0;right:0;top:0;bottom:0;height:100px;width:300px;margin: auto">
</div>
</div>
<pre>
<code class="html">
<div class="container" style="position: relative;height: 200px;">
<div class="inner"
style="position: absolute;left:0;right:0;top:0;bottom:0;
height:100px;width:300px;margin: auto">
</div>
</div>
</code>
</pre>
</li>
<li>
<h3>margin 重叠计算规则</h3>
<h4>参与折叠的 margin 都是正值</h4>
<div>
<div style="height:50px; margin-bottom:50px; width:50px; background-color: red;">A</div>
<div style="height:50px; margin-top:100px; width:50px; background-color: green;">B</div>
</div>
<pre>
<code class="html">
<div>
<div style="height:50px; margin-bottom:50px; width:50px; background-color: red;">A</div>
<div style="height:50px; margin-top:100px; width:50px; background-color: green;">B</div>
</div>
</code>
</pre>
<img src="http://linder0209.github.io/web-ui-guide/views/css/images/margin%20c2.png" alt="">
</li>
<li>
<h3>margin 重叠计算规则</h3>
<h4>参与折叠的 margin 都是负值</h4>
<div>
<div style="height:50px; margin-bottom:50px; width:50px; background-color: red;">A</div>
<div style="height:50px; margin-top:100px; width:50px; background-color: green;">B</div>
</div>
<pre>
<code class="html">
<div>
<div style="height:50px; margin-bottom:50px; width:50px; background-color: red;">A</div>
<div style="height:50px; margin-top:100px; width:50px; background-color: green;">B</div>
</div>
</code>
</pre>
<img src="http://linder0209.github.io/web-ui-guide/views/css/images/margin%20c2.png" alt="">
</li>
<li>
<h3>margin 重叠计算规则</h3>
<p id="marginCollapseRadio">
<input type="radio" name="marginCollapse" value="collapse" checked> collapse(重叠,默认值)
<input type="radio" name="marginCollapse" value="discard"> discard(取消margin 值)
<input type="radio" name="marginCollapse" value="separate"> separate(取消折叠)
</p>
<div id="marginCollapsePanel">
<div style="background: #009f3d;margin: 20px;">margin:20px;</div>
<div style="background: #0096be;margin: 50px;">margin:50px;</div>
</div>
</li>
</ol>
<script src="jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<script>
$(function() {
$('#marginCollapseRadio input').change(function(){
var value = $(this).val();
$('#marginCollapsePanel div').css({
marginCollapse: value
});
});
});
</script>
</body>
</html>