Material-UI typically injects its styles towards the end of the <head>
element, resulting in them taking precedence over styles generated by styled-components with equal CSS specificity.
To reverse this order and ensure that styled-components styles override those of Material-UI, you can utilize the StylesProvider
component with the injectFirst
property.
import React from "react";
import styled from "styled-components";
import Button from "@material-ui/core/Button";
import { StylesProvider } from "@material-ui/core/styles";
const StyledButton = styled(Button)`
background-color: red;
border-radius: 0;
`;
export default function App() {
return (
<StylesProvider injectFirst>
<div className="App">
<StyledButton>Hello</StyledButton>
</div>
</StylesProvider>
);
}
https://codesandbox.io/s/styled-components-65qpx?fontsize=14&hidenavigation=1&theme=dark
For more information, check out these related answers:
- Media Queries in Material-UI Using Styled-Components