LoginSignup
0
3

More than 5 years have passed since last update.

CSS 上下中央揃え

Last updated at Posted at 2018-04-22

縦方向の中央揃え

line-height 一行

line-heightは行間のサイズ。
line-height - font-size/2(上下の行間高さ)
※複数行に対応できない
line-height 行間について
スクリーンショット 2018-04-22 14.19.43.png


.wrapper01
  .box
    p ほげほげほげ
.wrapper01 {
  width: 400px;
  height: 400px;
  background: #ccc;
  .box {
    width: 200px;
    height: 200px;
    border: 1px solid black;
    p {
      font-size: 10px;
      line-height: 200px;
    }
  }
}

vertical-aline

適用できるのは、インライン要素とテーブルセルです。ブロックレベル要素には適用できない
スクリーンショット 2018-04-22 14.19.51.png

.wrapper02
  .box
    p
     | ほげほげほげ
     br
     | ほげほげほげ
.wrapper02 {
  width: 400px;
  height: 400px;
  background-color: #ccc;
  .box {
    width: 200px;
    height: 200px;
    border: 1px solid black;
    display: table;
    p {
      font-size: 10px;
      display: table-cell;
      vertical-align: middle;
    }
  }
}

flex

スクリーンショット 2018-04-22 14.19.51.png

.wrapper03
  .box
    p
     | ほげほげほげ
     br
     | ほげほげほげ

.wrapper03 {
  width: 400px;
  height: 400px;
  background-color: #ccc;
  .box {
    width: 200px;
    height: 200px;
    border: 1px solid black;
    display: flex;
    align-items: center;
    p {
      font-size: 10px;
    }
  }
}

コメント

上下中央揃えは、flexで。flexは素晴らしい。

0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3