LoginSignup
0
0

More than 1 year has passed since last update.

高さが違うコンテナをgridで綺麗に並べる

Last updated at Posted at 2021-12-02

【やりたいこと】

PC、タブレット表示でMasonryっぽい並びをgridレイアウトで実装する。ウィンドウ幅縮小で並び替えはしない。スマホサイズになると昇順で縦に並ぶ。

【結果】

See the Pen grid layout by yoshida (@yoshi090) on CodePen.

【解説】

ポイントは以下です。

style.scss
grid-template-areas:
    "a1 a2"
    "a1 a2"
    "a1 a4"
    "a3 a4"
    "a3 a6"
    "a5 a6"
    "a5 a7"
    "a5 a7";

エリアの指定を上記のようにすることで左右のコンテナの数や高さが違うものも勝手に揃ってくれます。

style.scss
grid-template-rows: 0.6fr 0.8fr 0.9fr 0.6fr 0.8fr 0.9fr 0.9fr 0.4fr;

また上記のrow(行)の高さを編集することで各コンテナの高さも調節できます。
IE11でも同様に表示されます(要ベンダープレフィクス)。

【余談】

Flexboxで実装するのは難しそうでした。
例えばスマホでのコンテナの並びが縦に「1→3→5→2→4→6→7」ならば左右でコンテナとして括れるので可能かと思います。

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