|
| 1 | +--- |
| 2 | +title: "Masonry Layout with three lines of CSS" |
| 3 | +pubDate: 2025-06-04T22:30:00Z |
| 4 | +tags: ['css'] |
| 5 | +--- |
| 6 | +The masonry layout consists of a group of elements of unequal size, arranged in such a way that there are no uneven gaps. A clear example is the Metro menu in Microsoft Windows. |
| 7 | + |
| 8 | +The following code example shows a set of images of different sizes. |
| 9 | + |
| 10 | +```html |
| 11 | +<div> |
| 12 | + <img src="https://picsum.photos/seed/random10/320/400.jpg" /> |
| 13 | + <img src="https://picsum.photos/seed/random20/300/400.jpg" /> |
| 14 | + <img src="https://picsum.photos/seed/random30/400/400.jpg" /> |
| 15 | + <img src="https://picsum.photos/seed/random40/400/320.jpg" /> |
| 16 | + <img src="https://picsum.photos/seed/random50/400/300.jpg" /> |
| 17 | + <img src="https://picsum.photos/seed/random60/400/400.jpg" /> |
| 18 | + <img src="https://picsum.photos/seed/random70/320/400.jpg" /> |
| 19 | + <img src="https://picsum.photos/seed/random80/300/400.jpg" /> |
| 20 | + <img src="https://picsum.photos/seed/random90/400/400.jpg" /> |
| 21 | + <img src="https://picsum.photos/seed/random100/320/400.jpg" /> |
| 22 | + <img src="https://picsum.photos/seed/random110/400/300.jpg" /> |
| 23 | +</div> |
| 24 | +``` |
| 25 | + |
| 26 | +This is what the images initially look like on screen: |
| 27 | + |
| 28 | + |
| 29 | +The three lines of CSS to apply are the `columns`, `gap` and `width` properties. |
| 30 | + |
| 31 | +The CSS shorthand property `columns` sets the number of columns to use when drawing an element's content, as well as the width of those columns. |
| 32 | + |
| 33 | +The CSS shorthand property `gap` defines the spaces between rows and columns. This property applies to multi-column, flexible, and grid containers. |
| 34 | + |
| 35 | +The CSS `width` property defines the width of an element. By default, it defines the width of the content area, but if `box-sizing` is set to `border-box`, it defines the width of the border area. |
| 36 | + |
| 37 | +```css |
| 38 | +div { |
| 39 | + columns: 320px; |
| 40 | + gap: 4px; |
| 41 | +} |
| 42 | +img { |
| 43 | + width: 100%; |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +These values have been used for convenience. `columns: 320px`, `gap: 4px`, and `width: 100%` are equivalent to the Tailwind CSS classes `columns-xs`, `gap-1`, and `w-full`. The four pixel spacing is the default browser spacing between images when the `columns` property is not set. Giving images a width of 100% will make them fit properly within their column width. |
| 48 | + |
| 49 | +Images now automatically reorder to fill the available space between them. |
| 50 | + |
| 51 | +This is what the images look like after applying them: |
| 52 | + |
| 53 | + |
| 54 | +The design even adapts to different screen sizes and orientations: |
| 55 | + |
| 56 | + |
| 57 | +Thanks to the advances of CSS, a complex design that previously required multiple lines of CSS, in addition to the use of JavaScript, can now be implemented with just a few lines of CSS. |
0 commit comments