Skip to main content
CSS Tip

Responsive Mosaic Grid using Modern CSS

Improving the previous mosaic grid implementation with a responsive version that works with any number of elements. Powered by random(), ident(), border-shape, sibling-*() functions, and more!

A CSS-only responsive grid of irregular shapes

.container {
--s: 130px; /* image size */
--g: 10px; /* gap */
--d: 20px; /* control the randomness */

--_r:,-1*var(--d),var(--d);
display: grid;
grid-template-columns: repeat(auto-fill,minmax(var(--s),1fr));
gap: var(--g);
margin: calc(2*var(--d));
container-type: inline-size;
}

img {
width: calc(100% + 2*var(--d));
aspect-ratio: 1;
margin-block: calc(-1*var(--d));
place-self: center;
box-sizing: border-box;
--n: round(down,(100cqw + var(--g))/(var(--s) + var(--g)));
--m: round(up,sibling-count()/var(--n));
--y: round(down,(sibling-index() - 1)/var(--n));
--x: mod(sibling-index() - 1,var(--n));
border: 3px solid;
border-shape: polygon(
calc(000% + var(--d) + random(ident(--x var(--x) var(--y)) var(--_r)))
calc(000% + var(--d) + random(ident(--y var(--x) var(--y)) var(--_r))),
calc(100% - var(--d) + random(ident(--x calc(var(--x) + 1) var(--y)) var(--_r)))
calc(000% + var(--d) + random(ident(--y calc(var(--x) + 1) var(--y)) var(--_r))),
calc(100% - var(--d) + random(ident(--x calc(var(--x) + 1) calc(var(--y) + 1)) var(--_r)))
calc(100% - var(--d) + random(ident(--y calc(var(--x) + 1) calc(var(--y) + 1)) var(--_r))),
calc(000% + var(--d) + random(ident(--x var(--x) calc(var(--y) + 1)) var(--_r)))
calc(100% - var(--d) + random(ident(--y var(--x) calc(var(--y) + 1)) var(--_r)))
);
}

Fallback for old browsers included in the demo

See the Pen Random & Responsive mosaic grid by Temani Afif (@t_afif) on CodePen.

Another version where the randomness is only inside the grid. The edges are kept straight.

A CSS-only responsive mosaic grid with straight edges

.container {
--s: 130px; /* image size */
--g: 10px; /* gap */
--d: 20px; /* control the randomness */

--_r:,-1*var(--d),var(--d);
display: grid;
grid-template-columns: repeat(auto-fill,minmax(var(--s),1fr));
gap: var(--g);
margin: calc(2*var(--d));
container-type: inline-size;
}

img {
width: calc(100% + 2*var(--d));
aspect-ratio: 1;
margin-block: calc(-1*var(--d));
place-self: center;
box-sizing: border-box;
--n: round(down,(100cqw + var(--g))/(var(--s) + var(--g)));
--m: round(up,sibling-count()/var(--n));
--y: round(down,(sibling-index() - 1)/var(--n));
--x: mod(sibling-index() - 1,var(--n));
border: 3px solid;
border-shape: polygon(
calc(000% + var(--d) + sign(var(--x))*random(ident(--x var(--x) var(--y)) var(--_r)))
calc(000% + var(--d) + sign(var(--y))*random(ident(--y var(--x) var(--y)) var(--_r))),
calc(100% - var(--d) + sign(var(--n) - var(--x) - 1)*random(ident(--x calc(var(--x) + 1) var(--y)) var(--_r)))
calc(000% + var(--d) + sign(var(--y))*random(ident(--y calc(var(--x) + 1) var(--y)) var(--_r))),
calc(100% - var(--d) + sign(var(--n) - var(--x) - 1)*random(ident(--x calc(var(--x) + 1) calc(var(--y) + 1)) var(--_r)))
calc(100% - var(--d) + sign(var(--m) - var(--y) - 1)*random(ident(--y calc(var(--x) + 1) calc(var(--y) + 1)) var(--_r))),
calc(000% + var(--d) + sign(var(--x))*random(ident(--x var(--x) calc(var(--y) + 1)) var(--_r)))
calc(100% - var(--d) + sign(var(--m) - var(--y) - 1)*random(ident(--y var(--x) calc(var(--y) + 1)) var(--_r)))
);
}

See the Pen Random & Responsive mosaic grid II by Temani Afif (@t_afif) on CodePen.


More CSS Tips