Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding the material-UI style not working

I am new to the material UI. Here, I am trying to override the CSS of material UI tabs component.

<Tab
    key={`${tab}_${index}`}
    classes={{
    flexcontainer: css.tabFlexContainer
    }}
    disableRipple
    label={tab.label}
    value={tab.value}
    icon={
    tab.icon ? <Icons className={css.tabIcons} iconname={tab.icon} /> : null
    }
/>

So, here I am trying to override the flexContainer class with this CSS:

. tabFlexContainer {
   width : 100%
  justify -content :space-between
}

So, when I am using I am getting a compiled time error only,

TS2769: No overload matches this call.

Can anyone help me with this?

like image 437
ganesh kaspate Avatar asked Mar 30 '20 06:03

ganesh kaspate


People also ask

How do you override material UI styling?

To override the styles of a specific part of the component, use the global classes provided by Material UI, as described in the previous section "Overriding nested component styles" under the sx prop section.

How do you override a style in React?

To override a style, you have two options. Either you can pass a style object, or you can pass a function that will obtain props regarding the current internal state of the component, thus enabling you to modify styles dynamically depending on the component state, such as isError or isSelected .

Is MUI makeStyles deprecated?

@mui/styles is the legacy styling solution for MUI. It is deprecated in v5.

How do I apply style to Mui components?

If you want to apply multiple styles then you can use the spread operator like so: style={{...style1,... style2}} . Usually, you are styling a specific thing in the component (root element) with the style property but some components have more than one property to style different elements of the component.


1 Answers

First of all, if you check the DOM structure

<div class="MuiTabs-root Tabs" aria-label="disabled tabs example">
  <div class="MuiTabs-scroller MuiTabs-fixed" style="overflow: hidden;">
    <div class="MuiTabs-flexContainer" role="tablist">
    </div>
  </div>
</div>;

You would find out that the demand className is MuiTabs-flexContainer (rather than tabFlexContainer)

Example: For Tabs, all the className can be found in the MUI Tabs CSS API


There are many solutions, except normal withStyles and makeStyles, for fully override:

1.Material-UI solution

1.1 Use MUI internal style HOC withStyles to fully override the component.

Using nesting selector

import { Tabs, Tab, withStyles } from "@material-ui/core";

const StyledTabs = withStyles({
  root: {
    background: "light-blue",
    ...
    boxShadow: "0 3px 5px 2px rgba(255, 105, 135, .3)",
    "& div.MuiTabs-scroller": {
      "& .MuiTabs-flexContainer": {
        background: "linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)"
      }
    }
  }
})(Tabs);

enter image description here


1.2 Use normal createStyles or makeStyles style solution

Classical component
withStyles (High order function) + createStyles

Functional component
useStyles (hooks) + makeStyles

Refer in details: https://stackoverflow.com/a/60736142/11872246


2.Styled-Components solution

If you want to use separate CSS files to style MUI component

You can try styled-components

styled-components allows you to write actual CSS code to style your components.

Usage:

import styled from 'styled-components';
import { Tabs, Tab, withStyles } from "@material-ui/core";

const StyledTabs = styled.Tabs`
  font-size: 1.5em;
  ...
`;

3.Separate Pure CSS solution

Refer: css_selectors

import "./styles.css";
div.MuiTabs-flexContainer {
  background: linear-gradient(45deg, red 30%, #ff8e53 90%);
}

enter image description here

Edit gallant-keller-kwtvj

like image 130
keikai Avatar answered Oct 10 '22 23:10

keikai