Help us understand the problem. What is going on with this article?

flexboxを用いた中央寄せ

More than 5 years have passed since last update.

ドメインが失効してしまったのでこちらで書きます\(^o^)/

天地中央寄せでレイアウトする箇所が出てきたとき、

.parent {
    position: relative;
    width: 100%;
    height: 400px;
}

.child {
    position: absolute;
    width: 300px;
    height: 200px;
    left: 50%;
    top: 50%;
    transform: translateX(-50%) translateY(-50%);
}

ってやるのが(IE9以降は)一番簡単だと思っていたのですが、
flexboxを用いるともっと簡単に中央寄せできることを知りました。

flexboxによる中央寄せ

kobito.1417253085.948391.png

.parent {
    display: flex;
    width: 100%;
    height: 400px;
}

.child {
    margin: auto;
    width: 300px;
    height: 200px;
}

ブラウザ対応状況

kobito.1417253636.761631.png
(caniuse参照)

IEは10以降からになってしまいますが、IEとAndroidの状況によっては使えるかもしれませんね。
(なんだかんだでdisplay:table最強伝説となってしまいますが)

参照元

Smashing Magazineからのツイートから拝借

また、IE10のハック的なものもツイートされていました

tanshio
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした