0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

CSSで中央寄せする方法

Posted at

はじめに

 よく使う要素の中央寄せ。いまだにうまく動いてくれないことが多々あるが、今日の学びでこれだけは!

インライン要素とブロック要素

それぞれの特徴についてはここではあげないが、そもそもインライン要素とブロック要素とで、中央寄せの方法が違うことを理解する。

インライン要素の場合

「インライン要素」という文字を中央寄せしたいとして、

<div class="parent">
    <span class="inline">
        インライン要素
    </span>
</div>
.parent {
 text-aline: center;
}

親要素text-aline: center;を当てる。

ブロック要素の場合

「ブロック要素」という文字を中央寄せしたいとして、

<div class="parent">
    <div class="block">
        ブロック要素
    </div>
</div>
.parent {
 width: 500px
}

.block {
 width: 20px
 margin: 0 auto;
}

中央寄せさせたい要素自体に margin: 0 auto;を当てることで、親要素の大きさの中央に寄る。

終わりに

 恥ずかしながら、あまりブロック要素・インライン要素を意識していなくて、毎回 margin: 0 auto;をして、うまくいかずに悩んでいた。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?