Skip to content

Box

The Box component serves as a wrapper component for most of the CSS utility needs.

The Box component packages all the style functions that are exposed in @material-ui/system. It's created using the experimentalStyled() function of @material-ui/core/styles.

Example

The palette style function.

The sx prop

All system properties are available via the sx prop. This property allows you to specify any CSS rules you may need, in addition to the ones already available as part of the system. Here is an example of how you can use it:

import * as React from 'react';
import Box from '@material-ui/core/Box';

export default function BoxSx() {
  return (
    <Box
      sx={{
        width: 300,
        height: 300,
        bgcolor: 'primary.dark',
        ':hover': {
          backgroundColor: 'primary.main',
          opacity: [0.9, 0.8, 0.7],
        },
      }}
    />
  );
}

Overriding Material-UI components

The Box component wraps your component. It creates a new DOM element, a <div> by default that can be changed with the component prop. Let's say you want to use a <span> instead:

import * as React from 'react';
import Box from '@material-ui/core/Box';
import Button from '@material-ui/core/Button';

export default function BoxComponent() {
  return (
    <Box component="span" sx={{ p: 2, border: '1px dashed grey' }}>
      <Button>Save</Button>
    </Box>
  );
}

This works great when the changes can be isolated to a new DOM element. For instance, you can change the margin this way.

However, sometimes you have to target the underlying DOM element. For instance, you want to change the border of the Button. The Button component defines its own styles. CSS inheritance doesn't help. To workaround the problem, you have two options:

  1. Use React.cloneElement()

The Box component has a clone prop to enable the use of the clone element method of React.

import * as React from 'react';
import Button from '@material-ui/core/Button';
import Box from '@material-ui/core/Box';

export default function BoxClone() {
  return (
    <Box sx={{ border: '1px dashed grey' }} clone>
      <Button>Save</Button>
    </Box>
  );
}
  1. Use render props

The Box children accepts a render props function. You can pull out the className.

import * as React from 'react';
import Button from '@material-ui/core/Button';
import Box from '@material-ui/core/Box';

export default function BoxClone() {
  return (
    <Box sx={{ border: '1px dashed grey' }}>
      {(props) => <Button {...props}>Save</Button>}
    </Box>
  );
}

⚠️ The CSS specificity relies on the import order. If you want the guarantee that the wrapped component's style will be overridden, you need to import the Box last.

API

import Box from '@material-ui/core/Box';
Name Type Default Description
children * union: node |
 func
Box render function or node.
clone bool false If true, the box will recycle its children DOM element. It's using React.cloneElement internally.
component union: string |
 func |
 object
'div' The component used for the root node. Either a string to use a DOM element or a component.
sx object {} Accepts all system properties, as well as any valid CSS properties.