垂直居中的方式

334 阅读1分钟

简单的几种垂直居中方式

1. diplay: table

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

2. display: flex

.box{
  display: flex;
  justify-content: center;  // 主轴 flex-direction默认为row
  align-items: center;      // 辅轴
}

3. position: relative

.box{
  position: relative;
  top: 50%;
  left: 50%;
  transform: tranlate(-50%, -50%);
}

4. margin: auto

.box{
  margin: auto 0;
}