Masonry
Masonry is a layout component that arranges children into columns of varying heights, similar to Pinterest’s grid layout. It automatically positions items to minimize empty space.Basic Masonry
A simple masonry layout with default settings:Responsive Columns
Use responsive values to adjust columns at different breakpoints:Image Masonry
Perfect for displaying image galleries:Responsive Spacing
Adjust spacing at different breakpoints:Sequential Order
By default, Masonry adds items to the shortest column. Usesequential to maintain order:
sequential={false} (default):
- Items are placed in the shortest column
- Minimizes height but may change visual order
sequential={true}:
- Items maintain left-to-right order within rows
- More predictable layout but may create more empty space
Server-Side Rendering
For SSR, provide default values to avoid layout shifts:Custom Component
Render as a different element:With Cards
Combine with Material UI cards:API Reference
Masonry Props
| Prop | Type | Default | Description |
|---|---|---|---|
children | node | required | The content of the component |
columns | number | string | ResponsiveStyleValue | 4 | Number of columns |
spacing | number | string | ResponsiveStyleValue | 1 | Space between children (theme spacing multiplier) |
sequential | boolean | false | Use sequential order instead of shortest column |
defaultColumns | number | - | Default columns for SSR |
defaultHeight | number | - | Default height in pixels for SSR |
defaultSpacing | number | - | Default spacing (theme spacing factor) for SSR |
component | elementType | 'div' | The component used for the root node |
classes | object | - | Override or extend styles |
sx | object | - | System prop for CSS overrides |
ResponsiveStyleValue
Bothcolumns and spacing support responsive values:
How It Works
The Masonry component uses CSS Flexbox with column wrapping:-
Layout Calculation: On mount and resize, Masonry:
- Measures the container width
- Calculates actual column count from breakpoints
- Measures each child’s height
- Assigns
orderCSS property to position items
-
Column Assignment:
- Default: Adds each item to the shortest column
- Sequential: Places items left-to-right in order
-
Dynamic Updates: Uses ResizeObserver and MutationObserver to:
- Reflow on window resize
- Update when children are added/removed
- Recalculate when child dimensions change
/home/daytona/workspace/source/packages/mui-lab/src/Masonry/Masonry.js:168
CSS Classes
You can override styles using:.MuiMasonry-root- Styles applied to the root element
Performance Considerations
Image Loading
Useloading="lazy" for images to improve initial render:
Large Lists
For very large lists, consider:- Virtualization with react-window or react-virtualized
- Pagination or infinite scroll
- Limiting initial render count
Debouncing
The component automatically debounces resize events (16ms, ~60fps) to optimize performance.Browser Compatibility
Masonry requires:- ResizeObserver - For detecting size changes
- MutationObserver - For detecting DOM changes