Masonry
Masonry
uses CSS Grid to create a masonry layout.
It is supported natively by Firefox; we polyfilled this behaviour with a little bit of JavaScript for other browsers.
-->
Masonry
uses CSS Grid to create a masonry layout.
It is supported natively by Firefox; we polyfilled this behaviour with a little bit of JavaScript for other browsers.
Just specify the number of columns and you’re good to go.
You can span an item across multiple columns with the --span
property. (No other Masonry layouts on the internet lets you do this so easily).
Since we use CSS Grid, you can also use dense
to pack smaller items upwards.
Like SimpleGrid
, you just have to change the --cols
property at different breakpoints to make a responsive Masonry layout.
Instead of using --cols
to determine the number of columns, you can let CSS Grid create intrinsic columns with Masonry-gallery
and --item-width
.
Masonry
waits for images and videos to load before it calculates the layout. This ensures your layout is accurate regardless of the content you contained within each grid item.
Masonry requires JavaScript for cross-browser support. To use Masonry, you need to import and run the Masonry script.
More information about this script can be found in the Masonry Script section.
Masonry’s API is similar to SimpleGrid
. You can define the number of columns in your Masonry grid with --cols
.
You can span an item across multiple columns with the --span
property.
You can use dense
to pack smaller items into the empty spaces left by larger items.
You can change the gap between items with the gap
property or the --gap
variable. Row gaps cannot be adjusted independently from the column gap.
You can change --cols
at different breakpoints to make your Masonry Grid behave responsively.
Masonry
has gallery
modifier that lets you determine the size of each column with the --item-width
property. --cols
has no effect if you use this.
By default, the Masonry script searches the DOM for any Masonry
layouts. If you wish to search for Masonry
in a specific container, you can pass an element or a selector into the script.
The Masonry
script doesn’t scan the DOM for new Masonry
grids after it has been loaded. So if you create a Masonry
grid after the script has been called, it won’t be picked up.
To initialize your new Masonry
grid, just call the Masonry
script again (preferably with the container as an argument).
The Masonry
script waits for images and videos to load before calculating layouts. This ensures your layout is accurate regardless of the content it holds.
Masonry is unable to prevent Cumulative Layout Shift (CLS) because it doesn’t know the dimensions of the media elements before they load.
To prevent CLS, just provide width
and height
attributes to your media elements before they load. You can find more information about CLS prevention in our Astro Image component
The Masonry
script uses a resizeObserver
to detect changes in the layouts. So Masonry
will run whenever the container resizes, or when you add items into the container. No need to worry about responsive layouts!
Splendid Labz is free because we love developers and we want to contribute back to the ecosystem.
We only charge for access to the documentation to keep the project going. If you don't wish to pay, feel free to use the content from the features tab, our blogs or our videos as your guide.
Masonry requires JavaScript for cross-browser support. To use Masonry, you need to import and run the Masonry script.
More information about this script can be found in the Masonry Script section.
Masonry’s API is similar to SimpleGrid
. You can define the number of columns in your Masonry grid with --cols
.
You can span an item across multiple columns with the --span
property.
You can use dense
to pack smaller items into the empty spaces left by larger items.
You can change the gap between items with the gap
property or the --gap
variable. Row gaps cannot be adjusted independently from the column gap.
You can change --cols
at different breakpoints to make your Masonry Grid behave responsively.
Masonry
has gallery
modifier that lets you determine the size of each column with the --item-width
property. --cols
has no effect if you use this.
By default, the Masonry script searches the DOM for any Masonry
layouts. If you wish to search for Masonry
in a specific container, you can pass an element or a selector into the script.
The Masonry
script doesn’t scan the DOM for new Masonry
grids after it has been loaded. So if you create a Masonry
grid after the script has been called, it won’t be picked up.
To initialize your new Masonry
grid, just call the Masonry
script again (preferably with the container as an argument).
The Masonry
script waits for images and videos to load before calculating layouts. This ensures your layout is accurate regardless of the content it holds.
Masonry is unable to prevent Cumulative Layout Shift (CLS) because it doesn’t know the dimensions of the media elements before they load.
To prevent CLS, just provide width
and height
attributes to your media elements before they load. You can find more information about CLS prevention in our Astro Image component
The Masonry
script uses a resizeObserver
to detect changes in the layouts. So Masonry
will run whenever the container resizes, or when you add items into the container. No need to worry about responsive layouts!
Here’s the CSS for Masonry
.
The astro-island
and astro-slot
selectors help to alleviate styling frustrations when using Astro.
Splendid Labz is free because we love developers and we want to contribute back to the ecosystem.
We only charge for access to the documentation to keep the project going. If you don't wish to pay, feel free to use the content from the features tab, our blogs or our videos as your guide.
Here’s the CSS for Masonry
.
The astro-island
and astro-slot
selectors help to alleviate styling frustrations when using Astro.
Class | Description |
---|---|
Masonry-gallery | Creates a Gallery-like masonry layout where you size columns with --item-width . |
CSS Variable | Default | Description |
---|---|---|
--gap | 1rem | Gap between columns |
--cols | 1 | Number of columns. Used in Masonry only |
--item-width | 3rem | Size of each column. Used in Masonry-gallery only |
CSS Variable | Default | Description |
---|---|---|
--span | 1 | Number of columns that the child should occupy. |
Props | Default | Description |
---|---|---|
selector | undefined | The container for the script. You can pass a selector or element into this value. When undefined , it defaults to document.body . |
Splendid Labz is free because we love developers and we want to contribute back to the ecosystem.
We only charge for access to the documentation to keep the project going. If you don't wish to pay, feel free to use the content from the features tab, our blogs or our videos as your guide.
Class | Description |
---|---|
Masonry-gallery | Creates a Gallery-like masonry layout where you size columns with --item-width . |
CSS Variable | Default | Description |
---|---|---|
--gap | 1rem | Gap between columns |
--cols | 1 | Number of columns. Used in Masonry only |
--item-width | 3rem | Size of each column. Used in Masonry-gallery only |
CSS Variable | Default | Description |
---|---|---|
--span | 1 | Number of columns that the child should occupy. |
Props | Default | Description |
---|---|---|
selector | undefined | The container for the script. You can pass a selector or element into this value. When undefined , it defaults to document.body . |