0
1

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.

中央揃えの手法とその使う場面

Last updated at Posted at 2020-12-11

中央揃えの手法とその使う場面

1 margin: auto;

何に対してmargin: auto;を適用させるか

中央揃えしたい要素

使う場面

中央揃えしたい要素がブロック要素の時

ただし、この中央揃えしたい要素にはしっかり幅を設定してあげておいてください

See the Pen margin: auto; by Kazuhito Nakayama (@kazuhito-nakayama) on CodePen.

2 text-align: center;

何に対してtext-align: center;を適用させるか

中央揃えしたい要素の親要素

使う場面

中央揃えしたい要素がインライン要素の時

See the Pen inline-textalign by Kazuhito Nakayama (@kazuhito-nakayama) on CodePen.

ただし、インライン要素にdisplay: blockをかけてブロック要素にしてしまう別解もあります

See the Pen inline-to-block by Kazuhito Nakayama (@kazuhito-nakayama) on CodePen.

3 positionプロパティ

何に対してpositionプロパティを適用させるか

中央揃えしたい要素の親要素に

index.html
position: relative;

中央揃えしたい要素の親要素に

index.css
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);

使う場面

中央揃えしたい要素は、ブロック要素でもインライン要素でも使える。

See the Pen position by Kazuhito Nakayama (@kazuhito-nakayama) on CodePen.

author: Kazuhito Nakayama twitter

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?