CSS 定宽高与不定宽高的元素 如何居中

网易17届春招实习面试被问到了这题,但是苦于平时积累一般,回答得有很多纰漏,特地整理一下。


水平居中:

1.满足定宽和block元素:对需要居中的元素应用margin:0 auto实现

2.满足inline元素或inline-*元素:对父元素应用text-align:center实现

3.满足不定宽和block元素的水平居中:

  • 加入table标签(包括tbody、tr、td),对table标签应用margin:0 auto
1
2
3
4
5
6
7
8
9
10
11
12
13
<div class="parentBox-2">
<table>
<tbody>
<tr>
<td>
<div class="childBox-2">
我是一个子容器,没有固定的宽度和高度
</div>
</td>
</tr>
</tbody>
</table>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*table margin:0 auto*/
.parentBox-2
{
width: 500px;
height: 300px;
margin: 50px;

background-color: #7f7f7f;
}

.parentBox-2 table
{
margin: 0 auto;
}
.childBox-2
{
background-color: #ff6;
}

增加了无意义的语义化标签,复杂了嵌套深度

  • 设置display:inline | inline-block,对父元素应用text-align:center
    • 如果设定为inline元素,则无法设定宽高
    • 因此设定为inline-block相对比较好
  • 父元素设定position:relative定位,应用left:50%,子元素设定position:relative定位,应用left:-50%
1
2
3
4
5
<div class="parentBox-4">
<div class="childBox-4">
我是一个子容器,没有固定的宽度和高度
</div>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*relative left:50% left:-50%;*/
.parentBox-4
{
position: relative;
left: 50%;

width: 500px;
height: 300px;
margin: 50px;

background-color: #7f7f7f;
}

.childBox-4
{
position: relative;
left: -50%;

background-color: #ff6;
}

父元素偏移50%后,子元素在父元素的基础上反向偏移父元素长度的一半,从而达到居中

但是设定position:relative会带来一定的副作用

  • 父元素设定flex定位
1
2
display:flex; 
justify-content: center;
  • 父元素设定position:relative定位,子元素设定position:absolute定位,应用left:50%,translateX(-50%)
1
2
3
4
5
<div class="parentBox-6">
<div class="childBox-6">
我是一个子容器,没有固定的宽度和高度
</div>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
.parentBox-6
{
position: relative;

width: 500px;
height: 300px;
margin: 50px;

background-color: #7f7f7f;
}
.childBox-6
{
position: absolute;
left: 50%;

transform: translateX(-50%);

background-color: #ff6;
}

垂直居中:

1.满足定高和单行文本:设定父元素的line-height 和 height一致

2.满足定高和多行文本|图片|block元素:

  • 加入table标签(包括tbody、tr、td),对td标签应用height:xxxpx;
1
2
3
4
5
6
7
8
9
10
11
<div class="parentBox-8">
<table>
<tbody>
<tr>
<td>
<img src="img/qrcode.jpg" alt="图片">
</td>
</tr>
</tbody>
</table>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
/*table td'sheight = height*/
.parentBox-8
{
width: 500px;
height: 300px;
margin: 50px;

background-color: #7f7f7f;
}
.parentBox-8 table td
{
height: 300px;
}

增加了无意义的语义化标签,复杂了嵌套深度

  • 使用position:absolute top:50%、margin-top:-XXpx负补偿进行居中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
.parentBox-13
{
position: relative;

width: 500px;
height: 300px;
margin: 50px;

background-color: #7f7f7f;
}
.parentBox-13 img
{

position: absolute;
left: 50%;
width: 100px;
margin-left: -50px;
}

3.满足不定高和多行文本 | 图片 | block元素:

  • 对父元素设定display: table-cell; vertical-align: middle;
1
2
3
<div class="parentBox-7">
<img src="img/qrcode.jpg" alt="图片">
</div>
1
2
3
4
5
6
7
8
9
10
11
12
.parentBox-7
{
display: table-cell;

width: 500px;
height: 300px;
margin: 50px;

vertical-align: middle;

background-color: #7f7f7f;
}

注意:设定margin/padding无效,需要对父元素的父元素使用

1
2
3
display: table;
border-collapse:separate;
border-spacing: 5px;
  • 父元素设定flex定位
1
2
display: flex;
align-items: center;
  • 父元素设定position: relative定位,子元素设定position: absolute定位,应用top:50%,translateY(-50%);
1
2
3
4
5
6
7
8
<div class="parentBox-14">
<div class="childBox-14">
<p>我是一个子容器,没有固定的宽度和高度</p>
<p>我是一个子容器,没有固定的宽度和高度</p>
<p>我是一个子容器,没有固定的宽度和高度</p>
<p>我是一个子容器,没有固定的宽度和高度</p>
</div>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*absolute translateY*/
.parentBox-14
{
position: relative;

width: 500px;
height: 300px;
margin: 50px;

background-color: #7f7f7f;
}
.childBox-14
{
position: absolute;
top: 50%;

transform: translateY(-50%);

background-color: #ff6;
}

水平垂直居中:

1.父元素设定flex布局

1
2
3
4
5
<div class="parentBox-1">
<div class="childBox-1">
我是一个子容器,没有固定的宽度和高度
</div>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*flexbox 居中*/
.parentBox-1
{
display: flex;

width: 500px;
height: 300px;
margin: 50px;

background-color: #7f7f7f;

justify-content: center;
align-items: center;
}

.childBox-1
{
background-color: #ff6;
}

2.父元素设定table-cell

1
2
3
display:table-cell;
vertical-align: middle;
text-align: center;

注意:设定margin/padding无效,需要对父元素的父元素使用

1
2
3
display: table;
border-collapse:separate;
border-spacing: 5px;

3.需要居中的元素外套一个空div元素,然后父元素应用position:relative,空div应用position:absolute left:50% top:50%,子元素用margin-left: -50% margin-top:-50%

1
2
3
4
5
<div class="parentBox-10">
<div class="spaceBox">
<img src="img/qrcode.jpg" alt="图片">
</div>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
.parentBox-10
{
position: relative;

width: 500px;
height: 300px;
margin: 50px;

background-color: #7f7f7f;
}
.spaceBox
{
position: absolute;
top: 50%;
left: 50%;

background-color: #6f6;
}

.parentBox-10 img
{
margin-top: -50%;
margin-left: -50%;
}

效果:


4.父元素使用position:relative 子元素使用position:absolute定位,left:50% top50%,然后使用translate(-50%,-50%)

1
2
3
4
5
6
7
8
<div class="parentBox-12">
<div class="childBox-12">
<p>我是一个子容器,没有固定的宽度和高度</p>
<p>我是一个子容器,没有固定的宽度和高度</p>
<p>我是一个子容器,没有固定的宽度和高度</p>
<p>我是一个子容器,没有固定的宽度和高度</p>
</div>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
.parentBox-12
{
position: relative;

width: 500px;
height: 300px;
margin: 50px;

background-color: #7f7f7f;
}
.parentBox-12 .childBox-12
{
position: absolute;
top: 50%;
left: 50%;

transform: translate(-50%,-50%);
}

5.定宽定高的情况下使用margin定值负补偿即可

6.神器: margin:auto; margin: auto; position: absolute; top: 0; left: 0; bottom: 0; right: 0; 居中

缺陷:

  • 必须设定高度
  • 建议设置overflow:auto来防止内容越界溢出。(查看溢出Overflow)
  • 在Windows Phone设备上不起作用
1
2
3
4
<div class="parentBox-15">
<div class="boxContainer">
</div>
</div>
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
.parentBox-15
{
position: relative;

width: 500px;
height: 300px;
margin: 50px;

background-color: #7f7f7f;
}

.boxContainer
{
position: absolute;


width: 400px;
height: 200px;

margin: auto;
left: 0;
top: 0;
bottom: 0;
right: 0;

background-color: #ccc;

overflow: hidden;
}

参考资料:

https://css-tricks.com/centering-css-complete-guide/ (CSS TRICKS)

http://blog.csdn.net/freshlover/article/details/11579669 (盘点8种CSS实现垂直居中水平居中的绝对定位居中技术)