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 5 years have passed since last update.

【CSS】左右中央寄せ(ブロック要素、インライン要素、インラインブロック要素)

Posted at

margin: 0 auto;

:sunny: ブロック要素を左右の中央寄せ(まんなか)にする
:sunny: 必ずwidthを指定する

スクリーンショット 2019-12-29 22.49.28.png

この赤いブロック要素をまんなかに表示したい。

例.html
<p>左右のまんなか</p>
例.css
p {
  background-color: red;
  color: white;
  width: 300px;
  margin: 0 auto;
}

CSSの一番下に margin: 0 auto; をコーディング。

Web

スクリーンショット 2019-12-29 22.53.59.png

左右のまんなかに表示されました!:smile:
注意事項としては必ず width で幅を指定しないとダメです。

text-align: center

:sunny: インライン要素、インラインブロック要素を左右の中央寄せ(まんなか)にする

詳しくは 【CSS】text-align の text-align: center; をご参考ください

まとめると・・・

ブロック要素 インライン要素 インラインブロック要素
margin: 0 auto; text-align: center text-align: center

となります:laughing:

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?