水平居中的方法:

1、父级text-align:center;

.parent{
    text-align: center;
}
.child{
    display: inline-block;
}

2、table配合margin

.child{
    display:table;
    margin: 0 auto;
}

3、使用定位

.parent{
    position:relative;
}
.child{
    position:absolute;
    left:50%;
    transform: translateX(-50%);
}

 

垂直居中的方法:

1、table-cell配合vertical-align

.parent{
    display: table-cell;
    vertical-align:middle;
}

2、absolute配合tranform

.parent{
    position:relative;
}
.child{
    position:absolute;
    top: 50%;
    transform: translateY(-50%);
}

3、全能的flex

.parent{
    display: flex;
    justify-content: center;
    align-items: center;
}

 

相关文章:

  • 2021-05-20
  • 2022-12-23
  • 2022-12-23
  • 2021-12-31
  • 2021-12-05
  • 2022-01-09
猜你喜欢
  • 2021-11-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-31
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案