3
2

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 左右中央揃え と 左右中央揃え&横並び

Last updated at Posted at 2018-04-22

したいこと

左右中央揃え

スクリーンショット 2018-04-22 13.00.25.png

text-align: center;

sample01.html.slim
.wrapper01
  ul
    li sample01
    li sample01
    li sample01
sample01.scss
.wrapper01 {
  width: 400px;
  height: 400px;
  background-color: #ccc;
  ul {
    text-align: center;
    li {
    }
  }
}

margin: 0 auto;

widthの指定が必須

sample02.html.slim
.wrapper02
  ul
    li sample02
    li sample02
    li sample02
sample02.scss
.wrapper02 {
  width: 400px;
  height: 400px;
  background-color: #ccc;
  ul {
    margin: 0 auto;
    width: 18%;
    li {
    }
  }
}

display: flex;

中央に寄せたい要素の親にflexをかける。

sample03.html.slim
.wrapper03
  ul
    li sample03
    li sample03
    li sample03
sample03.scss
.wrapper03 {
  width: 400px;
  height: 400px;
  background-color: #ccc;
  display: flex;
  justify-content: center;
  ul {
    li {
    }
  }
}

左右中央揃えで、横並び

スクリーンショット 2018-04-22 13.00.38.png

text-align: center; + display: inline-block;

sample01-1.html.slim
.wrapper01-1
  ul
    li sample01-1
    li sample01-1
    li sample01-1
sample01-1.scss
.wrapper01-1 {
  width: 400px;
  height: 400px;
  background-color: #808080;
  ul {
    text-align: center;
    li {
      display: inline-block;
    }
  }
}

margin: 0 auto; + display: inline-block;

widthの指定が必須

sample02-2.html.slim
.wrapper02-2
  ul
    li sample02-2
    li sample02-2
    li sample02-2
sample02-2.scss
.wrapper02-2 {
  width: 400px;
  height: 400px;
  background-color: #808080;
  ul {
    margin: 0 auto;
    width: 71%;
    li {
      display: inline-block;
    }
  }
}

display: flex;

中央に寄せたい要素の親にflexをかける。

sample03-3.html.slim
.wrapper03-3
  ul
    li sample03-3
    li sample03-3
    li sample03-3
sample03-3.scss
.wrapper03-3 {
  width: 400px;
  height: 400px;
  background-color: #808080;
  ul {
    display: flex;
    justify-content: center;
    li {
    }
  }
}

コメント

左右中央揃え/横並びは、flexを使用するのがとても便利

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?