垂直方向と水平方向のやり方の三種類

CSS水平方向と垂直方向のセンタリング

1つの+絶対位置変換

<div class="parent">
  <div class="child">Demo</div>
</div>
.parent {
  position: relative;
  width: 400px;
  height: 400px;
  background: skyblue;
}
.child {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 200px;
  height: 200px;
  background: pink;
}

2弾性モデル

<div class="parent">
  <div class="child">弹性模型</div>
</div>
.parent {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 400px;
  height: 400px;
  background: skyblue;
}
.child {
  width: 200px;
  height: 200px;
  background: pink;
}  

図3の実施形態セル

<div class="parent">
  <div class="child">单元格方式</div>
</div>  
.parent {
  display: table-cell;
  text-align: center;
  vertical-align: middle;
  width: 400px;
  height: 400px;
  background: skyblue;
}
.child {
  display: inline-block;
  width: 200px;
  height: 200px;
  background: pink;
}  

おすすめ

転載: www.cnblogs.com/0x29a/p/11229348.html