css 实现垂直居中的几种方式

东明兄 2017-12-13
0条评论 1,042 次浏览
东明兄 2017-12-130条评论 1,042 次浏览
  1. 行内元素
.parent {
    height: 高度;
}
.son {
    line-height: 高度;
}
  1. table
.parent {
  display: table;
}
.son {
  display: table-cell;
  vertical-align: middle;
}
  1. flex
.parent {
    display: flex;
    align-items: center;
}
  1. 绝对定位定高
.son {
    position: absolute;
    top: 50%;
    height: 高度;
    margin-top: -0.5高度;
}
  1. 绝对定位不定高
.son {
    position: absolute;
    top: 50%;
    transform: translate( 0, -50%);
}

  1. top/bottom: 0;
.son {
    position: absolute;
    top: 0;
    bottom: 0;
    margin: auto 0;
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注