LoginSignup
0
0

More than 3 years have passed since last update.

【CSS】要素の中央寄せ【備忘録】

Posted at

中央寄せしなければならない時に、毎回調べてるので備忘録

ブロック要素

sample.css
/* 色、幅、高さの記述は省略 */
.contentA{
  /* 親要素 */
  display: flex;
  align-items: center;
}

.contentB{
  /* 子要素 */
  margin: 0 auto;
}
sample.html
<body>
  <div class="contentA">
    <div class="contentB"></div>
  </div>
</body>

実行結果(赤が親要素、青が子要素)

スクリーンショット 2020-02-04 22.22.28.png

インライン要素

インライン要素をdivで囲ってあげる

sample.css
/* 色、幅、高さの記述は省略 */
.contentA{
  /* 親要素 */
  display: flex;
  align-items: center;
}

.contentB{
  /* 子要素 */
  margin: 0 auto;
}

/* ボタンの位置設定は特になし */

sample.html
<body>
  <div class="contentA">
    <div class="contentB">
      <input type="button" value="テストボタン">
    </div>
  </div>
</body>

実行結果(赤が親要素、青が子要素(ボタン))

スクリーンショット 2020-02-04 23.11.37.png

0
0
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
0