css实现垂直居中

2019-07-30  本文已影响0人  Johnson23
.parent {
  display: flex;
  align-items: center;
  justify-content: center;
}
.parent {
  position: relative;
  width: 500px;
  height: 500px;
}
.child {
  position: absolute;
  width: 300px;
  height: 200px;
  top: 50%;
  left: 50%;
  margin-top: -100px;
  margin-left: -150px;
}
.parent {
  position: relative;
  width: 500px;
  height: 400px;
}
.child {
  position: absolute;
  top: 50%;
  left: 50%;
  /*作用是,往上(x轴),左(y轴)移动自身长宽的 50%,以使其居于中心位置。*/
  transform: translate(-50%, -50%);
}
上一篇 下一篇

猜你喜欢

热点阅读