LoginSignup
17
21

More than 5 years have passed since last update.

CSSで子要素を親要素の中央に配置する方法

Posted at

positionを使う方法とflexを使う方法があります。

HTML
    <div class="parent">
        <div class="child"></div>
    </div>

positonを使う場合

CSS
.parent {
    position: relative;
}
.child {
    width: 100px;
    height: 100px;
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom :0;
    margin:  auto;
}

※子要素に高さや幅を設定していない場合は、親要素と同じ大きさになります。

flexを使う場合

CSS
.parent {
    display: flex;
    justify-content: center;
    align-items: center;
}
.child {
    width: 100px;
    height: 100px;
}
17
21
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
17
21