Many developers, myself included, have reached for CSS Grid to build layouts, only to find that most masonry libraries want to dictate their own positioning, often ignoring grid-template-columns or gap. mason-it takes a different approach: you define the grid, and it handles the vertical stacking.
display: grid container and its properties like grid-template-columns and gap.data-mason-it HTML attribute or a JavaScript call: MasonIt.init().MutationObserver to automatically detect and adjust for items added or removed from the grid.requestAnimationFrame for fluid layout recalculations on content changes or window resize.display: none become visible or vice-versa.1. Install Mason-it and import it into your project.
# NPM $ npm install mason-it
import MasonIt from 'mason-it';
<!-- OR --> <script src="/dist/mason-it.min.js"></script>
2. For mason-it to work as intended, your grid container needs a specific CSS property:
.my-masonry-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); /* Your column setup */ gap: 15px; /* Your desired gap */ align-items: start; /* Essential! Prevents items from stretching to fill row height */ /* grid-auto-rows: min-content; */ /* Optional, but consider for content-driven row heights */} 3. Call MasonIt.init() after your grid elements are in the DOM.
MasonIt.init('.my-masonry-grid', {
masonDelay: 100, // Optional: delay in ms before first layout
masonPollInterval: 0 // Optional: polling interval in ms, 0 = disabled
}); 4. Or use the declarative attribute approach:
<div class="my-masonry-grid" data-mason-it>
<div class="grid-item">Content 1</div>
<div class="grid-item">Content 2</div>
<!-- More grid items -->
</div>
<!-- With options -->
<div class="my-masonry-grid" data-mason-it="mason-delay:{500} mason-poll-interval:{2000}"></div> 5. API methods:
MasonIt.init(selector, [options])
selector: String CSS selector, Element, NodeList, or Array of elements.options (Object, optional): masonDelay (Number, default: 0): Milliseconds to wait before the initial layout.masonPollInterval (Number, default: 0): Interval in ms to periodically check for content changes and refresh. 0 disables polling. MutationObserver is generally preferred.MasonIt.refresh([selector])
MutationObserver might not catch (e.g., image loads changing item dimensions significantly, or style changes affecting size).MasonIt.destroy([selector])
mason-it functionality and cleans up listeners for the specified grid(s) or all instances. Important for SPAs when components are unmounted.MasonIt.debug(enable)
enable (Boolean): Toggles console debug messages. Can be helpful during setup.MasonIt.version()
"1.0.1").MasonIt.count()
mason-it instances.The post Responsive Masonry Layouts with CSS Grid and Mason-it.js appeared first on CSS Script.
BIG COUNTRY, Texas (KTAB/KRBC) - A brand new Texas State Park is now open, and…
Aether OS puts a full-fledged desktop in your browser that ties directly into the AT…
A new weekend has arrived, and today, you can save big on Trails in the…
data-anim is a JavaScript animation library that applies CSS-powered animations to HTML elements while scrolling/hovering/clicking/loading…
The <i-html> web component allows you to dynamically import HTML content inline, similar to an…
Can’t. Stop. Dancing. | Image: Wonderwheel Recordings Shout out to subscriber N_Gorski for today's pick.…
This website uses cookies.