Credits to:
- http://icewind-blog.com/2014/05/27/css-negative-margin/
 - https://blog.csdn.net/zhoulei1995/article/details/80161240
 - https://github.com/rccoder/blog/issues/6
 - https://www.jianshu.com/p/487d131537b4
 - https://alistapart.com/article/holygrail
 
Document flow
All block elements like boxes (block/inline) flowing on water; The water push them to left, and to top
- inline elements are only push to left; When there is no space in a row, it will automatically switch to next line without new line generated
 floatwill make element escape from normal document flow- inline element 
floatwill make it to be block element position: absolute&position: fixedwill also make escape
Margin Used in Domcument FLow
margindetermine the hidden outer border (notborder) for all elements.All elements have a hidden outer border used by browser to arrange elements.
Positive Margin
- block element: will make margins on 4 directions have a gap between its parents or its siblings
 - inline element:  will only respect 
left-marginandright-margin 
Negative Margin
normal document flow
- positive: margin will separate boxes by a gap
 - negative:
margin-left和margin-top:affect itself, move to its left/top by valuemargin-right和margin-bottom:affect adjacent elements, pull them to its right/bottom by value
 
escape document flow: absolute position
margin-left和margin-top:can affect itself, move to its left/top by valuemargin-right和margin-bottom:won’t affect adjacent, since it out of normal document flow
escape document flow: float
determined by
floatdirection (like normal doc flow)when
float: left:margin-left: -5pxwill move itself to left by 5pxmargin-right: -5pxwill move adjacent element to its right by 5pxmargin-top:can affect itself, move to its top by valuemargin-bottom:will move adjacent element to its bottom by value
when
float: right:margin-right: -5pxwill move adjacent element to its right by 5pxmargin-left: -5pxwill move itself to left by 5pxmargin-top:can affect itself, move to its top by valuemargin-bottom:will move adjacent element to its bottom by value
[NOTE]:
margin-left:-100%infloatdocument flowsince
floatflow is like inline elements, no new line between lines, even though they are in different linesFor example below: even though at first
leftis on bottom ofmain, does not mean it break off frommain, it still adjacent tomainmargin-left: -100%;moves itself to its left by 100% width of its parentExample
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<div class="main" > main </div>
<div class="left" > left </div>
<div class="right"> right </div>
<style>
.main{
float: left;
width: 100%; // occupy whole width
}
.left{
float: left;
width: 50px; // its own width
margin-left: -100%;
// even though at first it is on bottom of
// main, does not mean break off from main
// when '-100%' negative margin,
// it move itself to its left by
// 100% width of parent
}
.right{
float: left;
width: 50px;
margin-left: -50px;
}
</style>
Use case of negative margin
Layout Difference: Holy Grail vs Double wings
- Holy Grail: background won’t affect two sides, even it’s higher than two sides
 - Double wings: background affects parts below two sides, when it’s higher than two sides
 
Layout of Holy Grail (圣杯布局)
container set aside
left/right paddingpadding for left/right sectionmove left/right section onto main using
negative marginmove left/right section to padding place using
position:relative1
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<html>
<head>
<style>
.container {
padding: 0 300px 0 200px;
}
.content {
float: left;
width: 100%;
background: #f00;
}
.left {
position: relative;
left: -200px;
float: left;
width: 200px;
margin-left: -100%;
background: #0f0;
}
.right {
position: relative;
right: -300px;
float: left;
width: 300px;
margin-left: -300px;
background: #00f;
}
</style>
</head>
<body>
<div class="container">
<div class="content">
中间内容区域 中间内容区域 中间内容区域
中间内容区域 中间内容区域 中间内容区域
中间内容区域 中间内容区域 中间内容区域
</div>
<div class="left">左侧边栏区域</div>
<div class="right">右侧边栏区域</div>
</div>
</body>
</html>
Double Wings layout (双飞翼布局)
move left/right section onto wrapper using
negative marginset
margin left/rightto main, to make it won’t hidden by two sides1
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<html>
<head>
<style>
.wrapper {
float: left;
width: 100%;
background: #f00;
}
.content {
margin-left: 200px;
margin-right: 300px;
}
.left {
float: left;
width: 200px;
margin-left: -100%;
background: #0f0;
}
.right {
float: left;
width: 300px;
margin-left: -300px;
background: #00f;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="content">
中间内容区域 中间内容区域 中间内容区域
中间内容区域 中间内容区域 中间内容区域
中间内容区域 中间内容区域 中间内容区域 </div>
</div>
<div class="left">左侧边栏区域</div>
<div class="right">右侧边栏区域</div>
</body>
</html>
Horizontal & Vertical Center
For
position: absolute, even thought it out of the normal document flow, the negative left/top still affect itself, moving to its left/top by value1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<html>
<head>
<meta charset="UTF-8">
<title> Center </title>
<style>
body{ margin:0;padding:0;}
#test{
width:200px;
height:200px;
background:#F60;
position:absolute;
left:50%;
top:50%;
margin-left: -100px;
margin-top: -100px;
}
</style>
</head>
<body>
<div id="test"></div>
</body>
</html>