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 styled() function of @material-ui/core/styles.

Example

The palette style function.

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 property. Let's say you want to use a <span> instead:

<Box component="span" m={1}>
  <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 text color of the button. The Button component defines its own color. CSS inheritance doesn't help. To workaround the problem, you have two options:

  1. Use React.cloneElement()

The Box component has a clone property to enable the usage of the clone element method of React.

<Box color="text.primary" clone>
  <Button />
</Box>
  1. Use render props

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

<Box color="text.primary">
  {props => <Button {...props} />}
</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.

Any other properties supplied will be used by the style functions or spread to the root element.