LoginSignup
11
16

More than 5 years have passed since last update.

ブロック要素を、上下左右の中央寄せをする方法

Last updated at Posted at 2016-11-04

例えば以下のHTMLがある。

  <body>
    <header>
      <h1>title</h1>
    </header>
  </body>

この際、h1のtitleを上下左右に中央寄せしたい場合のメモ。

方針

  1. 左右の中央寄せをする
  2. 上下の中央寄せをする

1. 左右の中央寄せ

テキストの左右中央寄せは、text-align:center;を使う。

header{
  height: 48px;
  background-color: white;
}

h1{
  text-align: center;
}

これで左右はOK

上下の中央寄せ

上下の中央寄せは、親要素と同じピクセル数でline-heightを指定してあげればできる。

header{
  height: 48px;
  background-color: white;
}

h1{
  text-align: center;
  line-height: 48px;
}

親要素のheaderは48pxなので、子要素のh1も同じ数だけline-heightしてあげる。

以上。

11
16
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
11
16