Search documentation v0.8.1

Masonry

Grid layout with optimized available space for varying heights and widths

Source ↗️ Report Issue ↗️

Use the Masonry component to display content, like images or cards, in an organized, visually appealing manner. Unlike traditional grid layouts with fixed rows and columns, masonry layouts position elements in a way that optimizes available space, allowing for varying heights and widths of content blocks.

Preview
1
4
2
5
3
6
How to use Masonry in Astro
---
import { Masonry } from 'webcoreui/astro'
import { Box } from '@components' // Your custom component
const masonry = [
{ component: Box, props: { height: 100 }, children: 1 },
{ component: Box, props: { height: 50 }, children: 2 },
{ component: Box, props: { height: 70 }, children: 3 },
{ component: Box, props: { height: 90 }, children: 4 },
{ component: Box, props: { height: 140 }, children: 5 },
{ component: Box, props: { height: 120 }, children: 6 }
]
---
<Masonry items={masonry} />
How to use Masonry in Svelte
<script>
import { Masonry } from 'webcoreui/svelte'
import { Box } from '@components' // Your custom component
const masonry = [
{ component: Box, props: { height: 100 }, children: 1 },
{ component: Box, props: { height: 50 }, children: 2 },
{ component: Box, props: { height: 70 }, children: 3 },
{ component: Box, props: { height: 90 }, children: 4 },
{ component: Box, props: { height: 140 }, children: 5 },
{ component: Box, props: { height: 120 }, children: 6 }
]
</script>
<Masonry items={masonry} />
How to use Masonry in React
import React from 'react'
import { Masonry } from 'webcoreui/react'
import { Box } from '@components' // Your custom component
const masonry = [
{ component: Box, props: { height: 100 }, children: 1 },
{ component: Box, props: { height: 50 }, children: 2 },
{ component: Box, props: { height: 70 }, children: 3 },
{ component: Box, props: { height: 90 }, children: 4 },
{ component: Box, props: { height: 140 }, children: 5 },
{ component: Box, props: { height: 120 }, children: 6 }
]
const Component = () => (
<Masonry items={masonry} />
)
export default Component

The Masonry component expects an items prop referencing an array of object. Each object in the array can have the following properties:

The children property can be a number, a string, or reference another component object in the form of:

const children = {
component: Box,
props: { ... },
children: 1
}

Child components cannot contain other child components. If you need further nesting, create a new component for the nested part and pass it to the top-level component property of your item.

Columns

By default, the Masonry component have three columns. This can be configured using the columns property:

Two-column layout
1
3
5
2
4
6
Three-column layout (default)
1
4
2
5
3
6
Four-column layout
1
5
2
6
3
4
<Masonry items={[...]} columns={2} />
<Masonry items={[...]} /> <!-- No need to specify columns for a 3-column layout -->
<Masonry items={[...]} columns={4} />

Custom Gaps and Element

The Masonry component is rendered into a section element. This can be customized along with the gap size using the element and gap props:

Default gaps
1
4
2
5
3
6
10px gaps
1
4
2
5
3
6
No gaps
1
4
2
5
3
6
<Masonry
items={[...]}
element="article"
gap="10px"
/>
<Masonry
items={[...]}
element="div"
gap="0"
/>

If you need to globally specify gaps for your Masonry component, the preferred way is to override the following CSS variable:

html body {
--w-masonry-gap: 10px;
}

This way, you won’t need to specify the gap prop on each individual Masonry component.

Sequential Order

The items of the masonry component are ordered from left to right, top to bottom. This ordering can be changed with the sequential prop. Set sequential to true to order the items in sequential order, arranging them from top to bottom first:

Default ordering
1
4
2
5
3
6
Sequential ordering
1
2
3
4
5
6
<Masonry items={[...]} sequential={true} />

API

type MasonryProps = {
items: {
component?: AstroComponent | SvelteComponent | ReactComponent
props?: {
[key: string]: any
}
children?: {
component: AstroComponent | SvelteComponent | ReactComponent
props?: {
[key: string]: any
}
children?: string | number
} | string | number
html?: string
}[]
element?: string
gap?: string
columns?: number
sequential?: boolean
className?: string
}
PropDefaultPurpose
items [] Sets the items of the masonry.
items.component - Sets the item to a component.
items.props - Sets the props on the component.
items.children - Sets the children if the item is a component.
items.children.component - Sets the children to a component.
items.children.props - Sets props on the child component.
items.children.children - Sets the children of the child component.
items.html - Sets the item to a HTML string.
element section Sets the HTML tag of the masonry.
gap 5px Sets the gap between masonry items.
columns 3 Sets the number of columns.
sequential false Displays the masonry items in sequential order.
className - Pass additional CSS classes for the masonry.

Request improvement for this page