CSS居中布局

时间:2020-01-29 12:38:41   收藏:0   阅读:89

CSS居中布局在实际开发中是经常使用的一种布局方式,同时也是面试中会问的基础CSS布局中常见的问题之一。

水平居中

元素的宽未知

方法1.1: text-align:center,适合的元素通常为文档流中的内联元素,例如span,a,img,input等等.

1
2
3
.parent {
text-align: center;
}

方法1.2: text-align:center,适合的元素通常为文档流中的内联元素,将块级元素转换为inline

1
2
3
4
5
6
.parent{
text-align: center;
}
.son{
display: inline-block;
}

方法2.1: 定位+CSS3 transform

1
2
3
4
5
6
7
8
.parent {
position: relative;
}
.son {
position: absolute;
left: 50%;
transform: translateX(-50%);
}

方法2.2: 定位+margin:0,auto

1
2
3
4
5
6
7
8
9
10
.parent {
position: relative;
}
.son {
position: absolute;
width: 100px;
left: 0;
right: 0;
margin: 0 auto;
}

方法3: flex布局

1
2
3
4
.parent {
display: flex;
justify-content: center;
}

元素的宽已知

方法1: margin:0 auto,适合单个块级元素,在margin有节余的同时如果左右margin设置了auto,将会均分剩余空间.

1
2
3
4
.son{
width: 100px; /*必须定宽*/
margin: 0 auto;
}

方法2: margin-left.

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

垂直居中

元素的高未知

方法1: 定位+CSS3 transform

1
2
3
4
5
6
7
8
.parent {
position: relative;
}
.son {
position: absolute;
top: 50%;
transform: translateY(-50%);
}

方法2.1: flex布局

1
2
3
4
.parent {
display: flex;
align-items: center;
}

方法2.2: flex布局

1
2
3
4
5
6
.parent{
display: flex;
}
.son{
align-self: center;
}

元素的高已知

方法1.1: line-height,文字内容公用水平中垂线.

1
2
3
4
.parent{
height: 150px;
line-height: 150px; /*与height等值*/
}

方法1.2: line-height.

1
2
3
4
.parent{  /*或者用span把所有文字包裹起来,设置display:inline-block转换成图片的方式解决*/
height: 150px;
line-height: 30px; /*元素在页面呈现为5行,则line-height的值为height/5*/
}

方法2: margin:auto 0.

1
2
3
4
5
6
7
8
9
10
.parent{
position: relative;
}
.son{
position: absolute;
margin: auto 0;
top: 0;
bottom: 0;
height: 50px;
}

方法3: 定位+CSS3 transform

1
2
3
4
5
6
7
8
.parent {
position: relative;
}
.son {
position: absolute; /*子绝*/
大专栏  CSS居中布局/> top: 50%; /*父元素高度一半,这里等同于top:75px;*/
transform: translateY(-50%); /*自身高度一半,这里等同于margin-top:-25px;*/
}

方法4: 定位+tabel-cell

1
2
3
4
.parent{
display: table-cell;
vertical-align: middle;
}

方法5: margin-top.

1
2
3
4
5
6
7
8
.son {
width: 100px;
height: 100px;
position: absolute;
top: 50%;
left: 50%;
margin-top: -50px;
}

水平垂直居中

行内元素

行内元素,行内块级元素:text-align: center; 控制行内内容相对于块父元素水平居中,然后就是line-heightvertical-align使其垂直居中,font-size: 0; 是为了消除近似居中的bug

1
2
3
4
5
6
7
8
9
10
.parent{
height: 150px;
line-height: 150px; /*行高的值与height相等*/
text-align: center;
font-size: 0; /*消除幽灵空白节点的bug*/
}
.son{
/*display: inline-block;*/ /*如果是块级元素需改为行内或行内块级才生效*/
vertical-align: middle;
}

table-cell方法

CSS Table,使表格内容垂直对齐方式为middle,然后根据是行内内容还是块级内容采取不同的方式达到水平居中

1
2
3
4
5
6
7
8
9
10
11
12
.parent{
height: 150px;
width: 200px;
display: table-cell;
vertical-align: middle;
/*text-align: center;*/ /*如果是行内元素就添加这个*/
}
.son{
width: 100px;
height: 50px;
/*margin: 0 auto;*/ /*如果是块级元素就添加这个*/
}

绝对定位

方法1: 定位+CSS3 transform

1
2
3
4
5
6
7
8
9
10
.parent{
position: relative;
}
.son{
position: absolute;
top: 50%;
left: 50%;
/*定宽高时等同于margin-left:负自身宽度一半;margin-top:负自身高度一半;*/
transform: translate(-50%,-50%);
}

方法2: 定位+margin

1
2
3
4
5
6
7
8
9
10
11
12
.parent{
position: relative;
}
.son{
width:100px;
height:100px;
position: absolute;
top: 50%;
left: 50%;
margin-left:50px;
margin-right:50px;
}

绝对居中

当top、bottom为0时,margin-top&bottom设置auto的话会无限延伸占满空间并且平分;当left、right为0时,margin-left&right设置auto的话会无限延伸占满空间并且平分

1
2
3
4
5
6
7
8
9
10
11
12
13
.parent{
position: relative;
}
.son{
position: absolute;
margin: auto;
width: 100px;
height: 50px;
top: 0;
bottom: 0;
left: 0;
right: 0;
}

flex方法

方法1:

1
2
3
4
5
6
.parent{
display: flex;
}
.son{
margin: auto;
}

方法2:

1
2
3
4
5
.parent{
display: flex;
justify-content: center;
align-items: center;
}

方法3:

1
2
3
4
5
6
7
.parent{
display: flex;
justify-content: center;
}
.son{
align-self: center;
}
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!