How to disable box-shadow globally for all MUI components?
Solution 1
You can do it by component like this:
<AppBar elevation={0} />
Solution 2
In the configuration object of a material-ui theme, you can see the shadows
property, override it in your code and just leave the none
value, remove all the rest.
You code should look like this:
const theme = createMuiTheme({
shadows: ["none"]
});
All the shadow box will be removed entirely in your application.
P/s: This configuration is for version 1.0.0-beta.8
, I must note it here because this version has some breaking changes.
Solution 3
I use the following for TypeScript:
import { createMuiTheme } from "@material-ui/core/styles"
import { Shadows } from "@material-ui/core/styles/shadows"
const theme = createMuiTheme({
shadows: Array(25).fill("none") as Shadows,
})
Solution 4
MUI v5 update.
To disable the shadow globally, you need to reset all shadow values in the shadows
array to none
:
import { createTheme, ThemeProvider } from '@mui/material/styles';
import shadows, { Shadows } from '@mui/material/styles/shadows';
const theme = createTheme({
shadows: shadows.map(() => 'none') as Shadows,
});
Solution 5
Just set the zDepthShadows
to 'none' in the theme, either by creating a custom theme, or overriding them when you import the theme.
Matt Zukowski
Updated on November 02, 2021Comments
-
Matt Zukowski over 2 years
I need to disable the default
box-shadow
for most MUI components. Right now I'm doing this by setting the style manually for each component, like this:<FloatingActionButton style={{boxShadow: "none"}} />
Is there a way to do this globally, maybe using a theme setting?
-
Leon Gaban almost 6 yearsHi, I'm using your solution here stackoverflow.com/questions/50516398/… however getting an error message :(
-
Almaju over 5 yearsI got rid of the error message by doing
shadows: new Array(25)
-
apt over 4 yearsIt does work, is it documented somewhere? How did you find out?
-
Gus over 4 yearsI think it was a guess, cause elevation applies shadows. Plus if you travel through the component typescript definitions you'll see how its props work.
-
Nn Karthik over 4 yearsIt is documented at the site. the Appbar is written above the Paper component. From the documentation
The props of the Paper component are also available.
-
Rajendran Nadar over 2 yearswhy map? When you can do Array.fill
-
NearHuscarl over 2 yearsIn case MUI changes the number of shadow in the future. If you're using
fill
, you can do the same withArray(shadows.length).fill("none")
. @RajendranNadar -
Rajendran Nadar over 2 yearsAs far as I know, there won't be a breaking change in minor or patch releases so it is safe.
-
Ovi Trif about 2 yearsI think the question was clearly about doing it globally.