When to Use MUI's sx Prop vs. styled() in React
Decide when Material UI's sx prop keeps styles local and when to extract them into styled components for team consistency.
07 Sept 2026, 20:31 UTC

A design system that drifts with every new component
You inherit a React dashboard built with Material UI. One developer writes <Box sx={{ bgcolor: 'primary.main', p: 2 }} />, another writes <Box sx={{ backgroundColor: theme.palette.primary.main, padding: theme.spacing(2) }} />, and a third hardcodes style={{ backgroundColor: '#1976d2', padding: '16px' }} . The app renders, but the tokens drift. Padding values stop matching, dark mode ignores half the components, and a global color refactor becomes a grep across twenty files.
The useful takeaway is simple: use sx for component-local, theme-aware styles that appear once. Extract anything reused across screens into styled() or theme overrides so the design system stays in one place.
What sx actually reads from the theme
ThemeProvider is a React context provider from Material UI. It places a theme object into context so that sx functions, styled() components, and MUI components can read palette colors, spacing multiples, breakpoints, and typography tokens. The sx prop is a shortcut property on Box and most MUI components. It accepts plain style objects, arrays, or functions that receive the theme.
MUI's default theme uses an 8px spacing base. That means p: 2 resolves to 16px of padding. Breakpoints run from xs through xl, and palette shorthand like bgcolor: 'primary.main' reads directly from the active theme.
Worked example: a status badge
Here is a one-off badge written with sx:
import { Box, ThemeProvider, createTheme } from '@mui/material';
const theme = createTheme({ palette: { mode: 'dark' } });
export function StatusBadge({ children }) {
return (
{children}
);
}
function App() {
return (
Active
);
}
If that badge appears on five pages, copying the sx object five times is the drift problem in miniature. Extract it with styled():
import { styled } from '@mui/material';
import { Box } from '@mui/material';
const StatusBadge = styled(Box)(({ theme }) => ({
display: 'inline-flex',
alignItems: 'center',
gap: 1,
px: 1.5,
py: 0.5,
borderRadius: 2,
bgcolor: theme.palette.primary.main,
color: theme.palette.primary.contrastText,
fontSize: '0.875rem',
fontWeight: 600,
}));
The styled() version keeps the theme lookup in one file. Any developer who needs the badge imports the component instead of reimplementing the design tokens.
Trade-offs and when to stop using sx
- Specificity:
sxstyles carry high specificity and can unintentionally override MUI component base styles or custom CSS. - Readability: large inline objects make components harder to scan than a small set of reusable styled components or CSS modules.
- Version stability: theme structure and
sxbehavior can change between MUI major versions, so code written for one version may need migration adjustments in another.
Diagnostic: verify sx is theme-aware
- Open a terminal in the project root and run
npm list @mui/material. No special permissions are required. Confirm the installed major version matches the migration guide you are following. - Render
<Box sx={{ bgcolor: 'primary.main', p: 2 }} />insideThemeProvider. - Open browser DevTools, inspect the element, and check the Computed tab. Expect
paddingto be16pxandbackground-colorto match the theme'sprimary.mainvalue. - Toggle the app theme between light and dark. The rendered values should update through
sxwithout a page reload.
If the computed padding is not 16px or the color does not change when the theme toggles, the component is likely missing ThemeProvider or using a hardcoded value instead of a theme token.
Closing decision
Reach for sx when a style is local, temporary, or needs a breakpoint-specific override inside a single component. Reach for styled() when the same visual pattern appears in more than one place, or when you want a code-review-friendly boundary around a design token. The goal is not to eliminate sx; it is to keep theme decisions where the whole team can find them.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.