[关闭]
@Sakura-W 2016-09-11T13:00:40.000000Z 字数 2232 阅读 1404

CSS Centering

CSS


CSS居中最大的难题是:居中的方法很多,要考虑不同的场景应用不同的居中方法。

一、水平居中

1.行内元素

可以给包含行内元素的父元素设置一下属性来使行内元素居中:

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

这种方式适用于inline或者inline-*类型的元素,如inline,inline-block, inline-table, inline-flex等行内元素

2.块级元素

对于块状元素,可以设置其margin-leftmargin-right值为auto即可(前提是该元素已经设置了width值)

  1. .center-children{
  2. width: 100px;
  3. margin: 0 auto;
  4. }

3.多个块级元素

如果要将多个块级元素在同一行居中,那么通常的方法就是将这些块级元素的display属性设置为其他,比如inline-block或者flex

  1. .parent{
  2. text-align: center;
  3. }
  4. .center-children{
  5. display: inline-block;
  6. }
  7. 或者
  8. .parent{
  9. display: flex;
  10. justify-content: center;
  11. }

二、垂直居中

1.行内元素

使父元素的line-height大小设置等于height的大小:

  1. .parent{
  2. height: 100px;
  3. line-height: 100px;
  4. }

或者使单行行内元素的padding-top值等于padding-bottom值:

  1. .center-children{
  2. padding-top: 30px;
  3. padding-bottom: 30px;
  4. }

2.多行行内元素:

一般使padding-top等于padding-bottom值同样可以起作用:

  1. .center-children{
  2. padding-top: 30px;
  3. padding-bottom: 30px;
  4. }

如果上面的方法不起作用,那么该元素可能是table元素,比如:

  1. <table>
  2. <tr>
  3. <td>
  4. I'm vertically centered multiple lines of text in a real table cell.
  5. </td>
  6. </tr>
  7. </table>

table里的元素默认是垂直居中的。

如果不在table里,也可以将父元素的display属性设置为table,来达到同样的效果:

  1. .parent{
  2. display: table;
  3. }
  4. .center-children{
  5. display: table-cell;
  6. vertical-align: middle;
  7. }

当然还可以尝试用flex属性来实现垂直居中(前提是父元素必须有固定的高度):

  1. .parent{
  2. display: flex;
  3. flex-direction: column;
  4. justify-content: center;
  5. height: 400px;
  6. }

还可以用伪元素来实现这一效果:

  1. .parent{
  2. position: relative;
  3. }
  4. .parent::before{
  5. content: " ";
  6. display: inline-block;
  7. height: 100%;
  8. width: 1px;
  9. vertical-align: middle;
  10. }
  11. .childrent{
  12. display: inline-block;
  13. vertical-align: middle;
  14. }

3.块级元素

如果知道元素的高度,可以采用绝对定位的形式实现垂直居中:

  1. .parent{
  2. position: relative;
  3. }
  4. .children{
  5. position: absolute;
  6. top: 50%;
  7. height: 100px;
  8. margin-top: -50px;
  9. }

如果不知道该元素的高度,可以用transform属性来实现垂直居中:

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

当然也可以采用flex来实现垂直居中:

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

三、同时实现水平居中和垂直居中

1.元素有固定的宽高

采用负margin来实现:

  1. .parent {
  2. position: relative;
  3. }
  4. .child {
  5. width: 300px;
  6. height: 100px;
  7. padding: 20px;
  8. position: absolute;
  9. top: 50%;
  10. left: 50%;
  11. margin: -70px 0 0 -170px;
  12. }

2.元素没有确定的宽高

采用transform来实现:

  1. .parent {
  2. position: relative;
  3. }
  4. .child {
  5. position: absolute;
  6. top: 50%;
  7. left: 50%;
  8. transform: translate(-50%, -50%);
  9. }

3.采用flex

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

四、总结

方法很多,可以视具体情况确定具体的方法。

参考:
Centering in CSS: A Complete Guide

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注