diff --git a/snippets/masonry-layout.md b/snippets/masonry-layout.md new file mode 100644 index 000000000..e0d9b3288 --- /dev/null +++ b/snippets/masonry-layout.md @@ -0,0 +1,78 @@ +--- +title: Masonry Layout +tags: layout,advanced +--- + +Creates a vertical Masonry layout using pure HTML and CSS. + +```html +
+
+ An image + Another image + Another image + One more image + And another one + Last one +
+
+``` + +```css +/* Container */ +.masonry-container { + --column-count-small: 1; + --column-count-medium: 2; + --column-count-large: 3; + --column-gap: 0.125rem; + padding: var(--column-gap); +} + +/* Columns */ +.masonry-columns { + column-gap: var(--column-gap); + column-count: var(--column-count-small); + column-width: calc(1 / var(--column-count-small) * 100%); +} + +@media only screen and (min-width: 640px) { + .masonry-columns { + column-count: var(--column-count-medium); + column-width: calc(1 / var(--column-count-medium) * 100%); + } +} + +@media only screen and (min-width: 800px) { + .masonry-columns { + column-count: var(--column-count-large); + column-width: calc(1 / var(--column-count-large) * 100%); + } +} + +/* Bricks */ +.masonry-brick { + width: 100%; + height: auto; + margin: var(--column-gap) 0; + display: block; +} + +.masonry-brick:first-child { + margin: 0 0 var(--column-gap); +} +``` + +#### Explanation + +- Create a masonry-style layout that consists of "bricks" that fall into each other with either a fidex `width` (vertical layout) or a fixed `height` (horizontal layout), forming a perfect fit. Especially useful when working with images. +- `.masonry-container` is the container for the masonry layout. Within that container, there's a `div.masonry-columns`, which will automatically put each child element, `.masonry-brick`, into the layout. +- `.masonry-brick` must be have `display: block` to allow the layout to flow properly, while the `:first-child` of this class should have a different `margin` to account for its positioning. +- CSS variables are used to allow for greater flexibility for the layout, while media queries ensure that the layout flows responsively in different viewport sizes. + +#### Browser support + +- https://www.caniuse.com/#feat=css-variables +- https://www.caniuse.com/#feat=calc +- https://www.caniuse.com/#feat=mdn-css_properties_column-count +- https://www.caniuse.com/#feat=mdn-css_properties_column-width +- https://www.caniuse.com/#feat=mdn-css_properties_column-gap_multicol_context