Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override material-ui button styles with styled-components

I am using a material-ui button and trying to override the border-radius (i.e, make it 0) through styled-components. However, it's not working.

Code:

import React from "react";
import styled from "styled-components";
import { Button } from "@material-ui/core";

const StyledButton = styled(Button)`
  background-color: #d29d12;
  border-radius: 0;
`;

export default function App() {
  return <StyledButton>Hello</StyledButton>;
}
like image 271
Perplexityy Avatar asked Mar 17 '20 12:03

Perplexityy


People also ask

How do you override a button style in material UI?

If you want to override a component's styles using custom classes, you can use the className prop, available on each component.

Can you use styled-components with material UI?

Just to give you an overview, Material UI doesn't limit you to only JSS based styling. If you love using Styled-Components or CSS modules, you have the freedom to use it with Material UI.

Can I use styled-components with MUI?

By default, MUI components come with Emotion as their style engine. If, however, you would like to use styled-components , you can configure your app by following the styled engine guide or starting with one of the example projects: Create React App with styled-components.

How to override styles with styled components in materialui?

2 - Override the styles with the styled components: You can control the classenames that will be generated by MaterialUI. Maybe it will require a little more effort but it can also bring more flexibility. Please see these sections of the documentation:

How to override the styling of a button in a component?

If you wanted to override some styling of this button to have a variation of it, you would pass the Buttoncomponent as an argument to the styled()wrapper, for example, like so: const PrimaryButton = styled(Button)` color: white; background: blue; `;

How do I override a component slot in material UI?

First, use your browser's dev tools to identify the class for the component slot you want to override. The styles injected into the DOM by Material UI rely on class names that all follow a standard pattern : [hash]-Mui [Component name]- [name of the slot].

How do I customize material UI components?

Learn how to customize Material UI components by taking advantage of different strategies for specific use cases. Material UI provides several different ways to customize a component's styles. Your specific context will determine which one is ideal. From narrowest to broadest use case, here are the options: 1. One-off customization


Video Answer


1 Answers

By default, Material-UI injects it styles at the end of the <head> element. This means that its styles will come after styles generated by styled-components and thus the Material-UI styles will win over the styled-components styles when CSS specificity is the same.

You can use the StylesProvider component with the injectFirst property to move the Material-UI styles to the beginning of the <head> and then the styled-components styles will come after it and win.

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>
  );
}

Edit styled-components

Related answers:

  • Media Queries in Material-UI Using Styled-Components
like image 146
Ryan Cogswell Avatar answered Oct 10 '22 04:10

Ryan Cogswell