css布局

左右布局

If two elements are displayed as a block:
html structure

1
2
3
4
<div class="wrapper clearFix">
<div class="red"></div>
<div class="blue"></div>
</div>

float方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
css:
.clearFix:after{
display:block;
content:'';
clear:both;
}

.red{
width: 100px;
height: 50px;
background: red;
float:left;
}

.blue{
width: 100px;
height: 50px;
background: blue;
float:left;
}

flex布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
.wrapper{
display:flex;
flex-direction:row;

}
.red{
width: 100px;
height: 50px;
background: red;
}

.blue{
width: 100px;
height: 50px;
background: blue;
}

If two elements are displayed as an inline element:

1
2
3
4
5
6
7
8
9
10
11
12
13
.red{
width: 100px;
height: 50px;
display:inline-block;
background: red;
}

.blue{
width: 100px;
height: 50px;
display:inline-block;
background: blue;
}

左中右布局

html structure

1
2
3
4
5
<div class="wrapper clearFix">
<div class="red"></div>
<div class="blue"></div>
<div class="yellow"></div>
</div>

If two elements are displayed as a block:

float方法

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
.clearFix:after{
display:block;
content:'';
clear:both;
}

.red{
width: 100px;
height: 50px;
background: red;
float:left;
}

.blue{
width: 100px;
height: 50px;
background: blue;
float:left;
}

.yellow{
width: 100px;
height: 50px;
background: yellow;
float:left;
}

flex布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
.wrapper{
display:flex;
flex-direction:row;

}
.red{
width: 100px;
height: 50px;
background: red;
}

.blue{
width: 100px;
height: 50px;
background: blue;
}

.yellow{
width: 100px;
height: 50px;
background: yellow;
}

If two elements are displayed as a inline-element:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
.red{
width: 100px;
height: 50px;
display:inline-block;
background: red;
}

.blue{
width: 100px;
height: 50px;
display:inline-block;
background: blue;
}

.yellow{
width: 100px;
height: 50px;
display:inline-block;
background: yellow;
}

水平居中

inline元素的父元素上设置text-align:center;

1
2
3
4
5
6
7
8
9
.wrapper{
text-align:center;
}
.red{
width: 100px;
height: 50px;
display:inline-block;
background: red;
}

div+css左右margin为auto(前提:已设置div的宽度)

1
2
3
4
5
6
7
8
9
.wrapper{
max-width:960px;
}
.red{
width: 100px;
height: 50px;
background: red;
margin:0 auto;
}

绝对定位和margin-left:-(width/2);left:50%;

1
2
3
4
5
6
7
8
9
10
11
.wrapper{
position: relative;
}
.red{
width: 100px;
height: 50px;
background: red;
position: absolute;
left:50%;
margin-left:-50px;
}

flex布局

1
2
3
4
5
6
7
8
9
10
.wrapper{
display:flex;
flex-direction:column;
}
.red{
width: 100px;
height: 50px;
background: red;
align-self:center;
}

或者

1
2
3
4
5
6
7
8
9
10
.wrapper{
display:flex;
flex-direction:column;
}
.red{
width: 100px;
height: 50px;
background: red;
margin:0 auto;
}

垂直居中

绝对定位和margin-top:-(height/2);top:50%;

缺点:需要事先知道div的尺寸(高度)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
.wrapper{
width:100%;
height:300px;
position: relative;
background:#fff;
}
.red{
width: 50%;
height: 150px;
background: red;
position: absolute;
top:50%;
margin-top:-75px;
}

绝对定位和margin:auto;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
.wrapper{
width:100%;
height:300px;
position: relative;
background:#fff;
}
.red{
width: 50%;
height: 150px;
background: red;
position: absolute;
top:0;
bottom: 0;
margin:auto;
}

flex布局

注意:flex-direction默认属性是row

1
2
3
4
5
6
7
8
9
10
11
12
13
.wrapper{
width:100%;
height:300px;
display:flex;
background:#fff;
align-items:center;
flex-direction:row;
}
.red{
width: 50%;
height: 150px;
background: red;
}

inline元素line-height=元素高度

此方法在inline-height较大时有bug

1
2
3
html structure:
<div class="wrapper clearFix">
<div class="red">123</div>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
.wrapper{
width:100%;
height:300px;
display:inline-block;
background:#fff;
line-height:300px;
}
.red{
width: 50%;
height: 150px;
background: red;
display:inline-block;
line-height:150px;
}

其他小技巧

画三角形

1
2
3
4
5
6
7
8
.triangle1{
border:10px solid red;
width: 0;
height: auto;
border-top-color:transparent;
border-right-color:transparent;
border-left-color:transparent;
}
1
2
3
4
5
6
7
8
.triangle2{
border:10px solid red;
width: 0;
height: auto;
border-top:0;
border-right-color:transparent;
border-bottom-color:transparent;
}