Gridレイアウトで、全体を中央寄せしつつ、最終行を左寄せにする
flexboxを使うとうまくいかない
justify-content: center
だとこうなる。
justify-content: flex-start
だとこうなる。
justify-content: flex-start
+width
を指定+margin: auto
だとこうなる。
解決策
display: grid
を使う
html
<div class="container">
<div class="item">item1</div>
<div class="item">item2</div>
<div class="item">item3</div>
<div class="item">item4</div>
<div class="item">item5</div>
<div class="item">item6</div>
<div class="item">item7</div>
</div>
css
.container {
display: grid;
grid-template-columns: repeat(auto-fit, 300px);
grid-row-gap: 20px;
grid-column-gap: 10px;
justify-content: center;
width: 90%;
margin: 0 auto;
}
.item {
padding: 12px;
background-color: yellow;
}
See the Pen xxRNwLo by d0ne1s (@d0ne1s) on CodePen.
参考
Author And Source
この問題について(Gridレイアウトで、全体を中央寄せしつつ、最終行を左寄せにする), 我々は、より多くの情報をここで見つけました https://qiita.com/d0ne1s/items/af048336012ef1fe3cf0著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .