How to override css properties of Material UI components - css

I am working on project in which I have to use Material UI, just for simplicity, consider I am having a simple functional component in which I am having 2 Material UI components Button and Text Field
import React from 'react';
import { Button, TextField } from '#material-ui/core';
function RenderComponent(){
return(
<>
<Button variant="contained">Contained</Button>
<TextField id="outlined-basic" label="Outlined" variant="outlined" />
</>
)
}
export default RenderComponent
Can anyone please tell me how to override the css properties of these 2 Material UI components.
Thankyou 🙏

There are several ways to do this, you can directly override the built-in CSS class of an MUI component using the class name in your imported CSS file, for example in if you want to change the Button component's style, you can do this by applying your required CSS properties to .MuiButton-root class on your "CSS" file. Like bellow.
.MuiButton-root{
color: white;
height: 20px;
padding: 10px;
}
You can also use the available props of a component, you can easily find them in their individual documentation page of mui site.
Again you can use makeStyles or styled to style your component.
Here is an example to use makeStyles in your Button component.
import React from 'react';
import { makeStyles } from '#material-ui/core/styles';
import { Button, TextField } from '#material-ui/core';
const useStyles = makeStyles({
root: {
background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)',
border: 0,
borderRadius: 3,
boxShadow: '0 3px 5px 2px rgba(255, 105, 135, .3)',
color: 'white',
height: 48,
padding: '0 30px',
},
});
function RenderComponent(){
const classes = useStyles();
return(
<>
<Button className={classes.root} variant="contained">Contained</Button>
<TextField id="outlined-basic" label="Outlined" variant="outlined" />
</>
)
}
export default RenderComponent
You can find more about styles here.

Give them id's/classes and write your custom CSS for them. If it doesn't override the standard Material-UI styles, add the keyword !important. If you are using create-react-app, you can import CSS right to the file like this:
import "./styles.css";
If this doesn't help, that means Material-UI uses inline styles for the elements. In this case, you would have to write your CSS right into your components' attributes: like this. If it doesn't override standard styles, use !important again.

Related

Using Material-ui theme in styled component

Need to use theme spacing value in a styled component like below with no success.
When I first created it, style was applied to button. But now, no style is applied!
You can see it here: sandbox.
import React from "react";
import styled, { ThemeProvider } from "styled-components";
import {
createMuiTheme,
ThemeProvider as MuiThemeProvider,
darken
} from "#material-ui/core/styles";
import Button from "#material-ui/core/Button";
const customTheme = createMuiTheme({
palette: {
primary: {
main: "#6772e5"
}
},
spacing: 8
});
const StyledButton = styled(Button)`
${({ theme }) => `
background-color: ${theme.palette.primary.main};
color: #fff;
box-shadow: 0 4px 6px rgba(50, 50, 93, 0.11), 0 1px 3px rgba(0, 0, 0, 0.08);
padding-left: 20px;
padding-right: ${theme.spacing(2)};
font-size: 13px;
&:hover {
background-color: ${darken(theme.palette.primary.main, 0.2)};
}
`}
`;
export default function App() {
return (
<MuiThemeProvider theme={customTheme}>
<ThemeProvider theme={customTheme}>
<StyledButton>Customized</StyledButton>
</ThemeProvider>
</MuiThemeProvider>
);
}
Your styled-component styles is overridden by the default JSS styles. By default, JSS styles is injected at the bottom of the <head> (higher CSS specificity). You can tell MUI to override JSS styles by putting your component tree inside <StylesProvider injectFirst>. See controlling priority.
// tell MUI to inject JSS style first, so styled-component can override it
<StylesProvider injectFirst>
<MuiThemeProvider theme={customTheme}>
<ThemeProvider theme={customTheme}>
<StyledButton>Customized</StyledButton>
</ThemeProvider>
</MuiThemeProvider>
</StylesProvider>

customize antd tooltip styles using styled components

I am trying to have custom width for antd tooltip component: Link to docs
How can this be done ?
import React from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import { Tooltip } from "antd";
import styled from "styled-components";
const Styled = styled.div`
.ant-tooltip-inner {
color: yellow;
background-color: green;
width: 800px;
}
`;
ReactDOM.render(
<Styled>
<Tooltip title="prompt text">
<span>Tooltip will show on mouse enter.</span>
</Tooltip>
</Styled>,
document.getElementById("container")
);
The antd Tooltips docs gives you a hint for your issue. The Tooltip is added as div in the body by default, in fact your custom style won't work without any adaptions. Depending on your requirements you can use
GlobalStyle from Styled Components
Overwrite getPopupContainer from Antd Tooltip
GlobalStyle
As one workaround you can use the globalStyle
const GlobalStyle = createGlobalStyle`
body {
.ant-tooltip-inner {
color: yellow;
background-color: green;
width: 800px;
}
}
`;
ReactDOM.render(
<Tooltip title="prompt text">
<GlobalStyle />
<span>Tooltip will show on mouse enter.</span>
</Tooltip>,
document.getElementById("container")
);
Here is a CodeSandbox for this workaround.
getPopupContainer
From the Tooltip docs for getPopupContainer
The DOM container of the tip, the default behavior is to create a div
element in body
Here you can just pass the triggerNode to be the parent object and your styles are set as expected.
const Styled = styled.div`
.ant-tooltip-inner {
color: yellow;
background-color: green;
width: 800px;
}
`;
ReactDOM.render(
<Styled>
<Tooltip title="prompt text" getPopupContainer={(triggerNode) => triggerNode}>
<span>Tooltip will show on mouse enter.</span>
</Tooltip>
</Styled>,
document.getElementById("container")
);
Working CodeSandBox for using getPopupContainer.
The default behavior for DOM container of the tip is to create a div element in body. You can change it to create inside Tooltip element with getPopupContainer:
<Tooltip
getPopupContainer={(trigger) => {
console.log(trigger);
return trigger;
}}
title="prompt text"
>
With the code above you style .ant-tooltip-inner will work.
For more info, check this link -> Tooltip Antd API

Is there a way to style the border color and text color of <TextField/> in Material-UI without using makeStyles

Is it possible to style Material-UI without using the makeStyles feature, for example, css? Just trying to understand how Material-UI style works.
The red style on the bottom is the style I'm trying to achieve with simple css here.
Below is an example of how to customize the various colors in an outlined select using simple CSS.
styles.css
.customSelect {
width: 200px;
}
.customSelect .MuiInputLabel-root {
color: red;
}
.customSelect .MuiInputBase-input {
color: green;
}
.customSelect .MuiOutlinedInput-notchedOutline {
border-color: red;
}
.customSelect:hover .MuiOutlinedInput-notchedOutline {
border-color: orange;
}
.customSelect
.MuiOutlinedInput-root.Mui-focused
.MuiOutlinedInput-notchedOutline {
border-color: purple;
}
.customSelectMenu .MuiMenuItem-root {
color: blue;
}
App.js
import React from "react";
import "./styles.css";
import TextField from "#material-ui/core/TextField";
import MenuItem from "#material-ui/core/MenuItem";
export default function App() {
const [value, setValue] = React.useState("");
return (
<TextField
className="customSelect"
label="Sale Type"
required
select
value={value}
onChange={event => setValue(event.target.value)}
variant="outlined"
SelectProps={{ MenuProps: { className: "customSelectMenu" } }}
>
<MenuItem value={1}>Sale Type 1</MenuItem>
<MenuItem value={2}>Sale Type 2</MenuItem>
</TextField>
);
}
Related answers:
Change border color on Material-UI TextField
Global outlined override
Change outline for OutlinedInput with React material-ui

React - applying css via className to imported Component

I'm new to react and I have a quick question about styling imported components. I made a basic Title component that simply outputs the props passed. My Title.js file:
import React from 'react';
import '../App.css'
class Title extends React.Component{
render(){
return <h1>{this.props.prop}</h1>
}
}
export default Title
I'm using it in my App.js file and trying to style it via a className
import React from 'react';
import Title from './components/Title'
import './App.css';
function App() {
return (
<Title className = 'title' prop = 'Title!'/>
);
}
export default App;
my css:
.title{
background-color: rgba(255, 0, 0, 0.2);
text-align: center;
margin-top: 100px;
padding: 20px;
border: solid black;
}
This does not works, even if I apply an inline style to the Title tag. However it does works when I apply the className to the h1 tag within the Title.js file. Is it because everything written within the Title tag is just passed as a prop? If that's true how are third-party library components styled? Any help is much appreciated!
In order for this to work the way you want to, you need to pass the className prop form your Title component to the h1 inside it:
class Title extends React.Component{
render(){
return <h1 className={this.props.className}>{this.props.prop}</h1>
}
}

How to override react-bootstrap colors?

I was looking up how to change it but none of the solutions seemed to work for me.
I want to override the color of a react-bootstrap button.
This solution as below works just fine and is exactly what i wanna do:
<Button
block
style={{backgroundColor: '#0B0C10', borderColor: '#45A293', color: '#45A293', borderRadius: '100px'}}
>
sample text
</Button>
But i don't wanna rewrite it each time i use button so i would like to have solution with css, I've tried using this:
.custom-button {
background-color: #1F2833;
border-color: #45A293;
border: 3px solid-transparent;
color: #45A293;
border-radius: 100px;
}
And then passing it in className like like so className="custom-button" but it doesn't really work.
I am using Button from react-bootstrap
import {Button} from "react-bootstrap";
Styles from bootstrap
import 'bootstrap/dist/css/bootstrap.min.css';
Using versions as below:
"react-bootstrap": "^1.0.0-beta.5",
"bootstrap": "^4.3.1",
Styles applied using the style="" attribute of HTML elements are more specific than styles applied through classes, which is why your first solution worked. Appending !important at the end of styles is one way of overriding other styles which are more specific than .custom-button class.
One quick solution that comes to my mind, that will ensure that you don't repeat yourself, is storing the styles in an object and importing them from a file.
styles.js
const styles = {
customButton: {
backgroundColor: '#0B0C10',
borderColor: '#45A293',
color: '#45A293',
borderRadius: '100px'
}
};
export default styles;
Component.jsx
import { styles } from './styles.js'
<Button
block
style={styles.customButton}
>
sample text
</Button>
Otherwise you would have to play with attaching ID's or construct more specific css selectors.
Add a bg or btn
.bg-custom-button {
background-color: #1F2833;
border-color: #45A293;
border: 3px solid-transparent;
color: #45A293;
border-radius: 100px;
}
Got mine working like that
Then in the bg="custom-button"
I am unsure if this effect is intended or not but the easiest way that I have found to override React Bootstrap css is to use Material ui withStyles. Here is an example.
import React from 'react';
import { withStyles } from '#material-ui/styles';
import Container from 'react-bootstrap/Container';
import Row from 'react-bootstrap/Row';
import Col from 'react-bootstrap/Col';
import ButtonGroup from 'react-bootstrap/ButtonGroup';
import Button from 'react-bootstrap/Button';
const styles = {
logoContainer: {
position: 'fixed',
},
rowStyles: {
marginBottom: '10px',
},
button: {
border: '3px inset #ffc107',
borderRadius: '50%',
width: '55px',
height: '55px',
fontFamily: 'fangsong',
fontSize: '1em',
fontWeight: '700',
color: 'white',
backgroundColor: 'rgb(0,0,0, 0.5)',
}
}
const Logo = (props) => {
const logoStyles = props.classes;
return (
<div>
<Container container='true' className={logoStyles.logoContainer}>
<ButtonGroup >
<Col>
<Row className={logoStyles.rowStyles}>
<Button onClick={{}} className={logoStyles.button}>BS</Button>
</Row>
</Col>
</ButtonGroup>
</Container>
</div>
);
}
export default withStyles(styles)(Logo);
Hope this helps...
You can use the "bsPrefix" prop which allows to override the underlying component CSS base class name.
bsPrefix="custom-button"

Resources