Box component in material UI reactjs:
Box component is used as a wrapper component in Material-UI library. It is actually a Box is in material-ui/core package. Below is the complete import statement for it: or and we can use it like: If you inspect the Box component, it is actually a div component. Using bgcolor, width and height, we can give background color, width and height to a Box: For example: This will create one red box: We can also add screen breakpoints. For example: It will show red box for screen size md and above. And, for smaller screens than md, it will show blue box. By default, Box is a div. But we can also change it to any other component using component props. For example: It will change it to span. We can add other components inside Box component: This example will add one Button in the Box. Similarly, we can add any other component.How to import:
import Box from '@material-ui/core/Box';
import { Box } from "@material-ui/core";
import { Box } from "@material-ui/core";
function App() {
return <Box></Box>;
}
export default App;
Adding width, height and background color:
import { Box } from "@material-ui/core";
function App() {
return (
<Box
bgcolor="red"
width={200}
height={200}
style={{ marginTop: 300, marginLeft: 300 }}
></Box>
);
}
export default App;
<Box
bgcolor={{xs: 'blue', md: 'red'}}
width={200}
height={200}
style={{ marginTop: 300, marginLeft: 300 }}
></Box>
component:
<Box
width={200}
component='span'
height={200}
></Box>
Adding child:
import { Box, Button } from "@material-ui/core";
function App() {
return (
<Box width={200} height={200}>
<Button>Click Me !!</Button>
</Box>
);
}
export default App;