Box

Add inline styles to any element or component with sx
Import

Usage

Box allows you to use sx prop with any element or component. Box itself does not include any styles.

Box lets you add inline styles with sx prop
<Box
sx={(theme) => ({
backgroundColor: theme.colorScheme === 'dark' ? theme.colors.dark[6] : theme.colors.gray[0],
textAlign: 'center',
padding: theme.spacing.xl,
borderRadius: theme.radius.md,
cursor: 'pointer',
'&:hover': {
backgroundColor:
theme.colorScheme === 'dark' ? theme.colors.dark[5] : theme.colors.gray[1],
},
})}
>
Box lets you add inline styles with sx prop
</Box>

Custom component

To use Box with custom component or element set component prop to tag name or react component:

// With custom element
<Box component="a" href="https://mantine.dev/" />;
// With custom component
import { Link } from 'react-router-dom';
<Box component={Link} to="/hello" />;
// With Next.js Link
import Link from 'next/link';
<Link href="/next-link">
<Box component="a">Next link</Box>
</Link>;
<Box
component="a"
href="https://mantine.dev"
target="_blank"
sx={(theme) => ({
display: 'block',
backgroundColor: theme.colorScheme === 'dark' ? theme.colors.dark[6] : theme.colors.gray[0],
color: theme.colorScheme === 'dark' ? theme.colors.blue[4] : theme.colors.blue[7],
textAlign: 'center',
padding: theme.spacing.xl,
borderRadius: theme.radius.md,
cursor: 'pointer',
'&:hover': {
backgroundColor:
theme.colorScheme === 'dark' ? theme.colors.dark[5] : theme.colors.gray[1],
},
})}
>
Set component prop to style different elements
</Box>

To use custom component with TypeScript provide type:

// With element
<Box<'a'>
component="a"
onClick={(event) => console.log(event)}
ref={(node) => {
window.myRef = node;
}}
/>
// With component
<Box<typeof Link>
component={Link}
onClick={(event) => console.log(event)}
ref={(node) => {
window.myRef = node;
}}
/>
Build fully functional accessible web applications faster than ever
Feedback
Your feedback is most valuable contribution to the project, please share how you use Mantine, what features are missing and what is done good
Leave feedback