DEV Community

Cover image for Quick Tips: Styling MUI Components with the sx Prop
Aya Ishimura
Aya Ishimura

Posted on • Updated on

Quick Tips: Styling MUI Components with the sx Prop

Elevate Your MUI Components: Mastering the sx Prop for Styling

Material-UI (now known as MUI) has always been a popular choice among React developers for its rich component library and flexibility. One of the game-changers introduced in MUI is the sx prop, which provides a powerful and concise way to style components directly. In today's quick tip, we'll explore how to use the sx prop to change button hover effects and to integrate images seamlessly into Box components.


To change a button color on hover with MUI, you can use the inline css sx prop with ":hover".

Example:

<Button
  variant="contained"
  sx={{bgcolor: "#525252",
       ":hover": {bgcolor: "#5c5c5c",color: "white"},
     }}
>
Click Me
</Button>
Enter fullscreen mode Exit fullscreen mode

To put an image into a box component, you can set 'component="img"'.

<Box
   component="img"
   src={`http://localhost:4000/${cover}`}
   sx={{
        // Match the width of the image to the parent element
        width: "100%", 
        height: "auto", 
        maxWidth: "250px", 
        maxHeight: "200px", 
        borderRadius: "10px",
        objectFit: "cover",
      }}
 ></Box>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)