MakeStyles (Material UI) apply style to child element

MarkinMark

I was wondering if it's possible to apply a style to a child element only, using MakesStyles, for example in a normal HTML/CSS project:

<div className="parent">
  <h1>Title!</h1>
</div>
.parent h1 {
  color: #f00;
}

This would color every title inside the div.
I have tried a few differnt ways, one of them was this:

// Function
const { parent } = homeStyle()

return (
  <div className={parent}>
    <h1>Title!</h1>
  </div>
)

// Style
const homeStyle = makeStyles(theme => ({
  parent: {
    background: "#fff",
    h1: {
      color: "#f00",
    }
  },
}))

But it didn't work.

Harley Lang

If you are looking to just style material-ui H1 titles, you can select it with:

.parent .MuiTypography-h1 {
  color: #f00;
}

In the photo below for an alternative solution, you'll see the classes that are applied to elements with material-ui. The inspector can be handy in identifying the names of the material-ui elements you want to select.

Your mileage may vary, depending on your CSS setup.


What I read from your question however, is a desire to select a single H1, within perhaps a <div> amonst other styled H1s. This can be done with ThemeProvider in the material-ui library (docs here):

import { Typography } from "@material-ui/core";
import {
  createMuiTheme,
  responsiveFontSizes,
  ThemeProvider
} from "@material-ui/core/styles";

const Themed = ({ children }) => {
  const theme = createMuiTheme({
    overrides: {                          {/* <--  create a style override */}
      MuiTypography: {
        h1: {
          "&.styled": {         {/* <-- specify a className for overrides */}
            color: "red"
          }
        }
      }
    }
  });

  return (
    <ThemeProvider theme={responsiveFontSizes(theme)}>
      <>{children}</>
    </ThemeProvider>
  );
};

const App = () => {
  return (
    <Themed>

      <Typography 
        className="styled"               {/* <-- add the style from above */}
        variant="h1"> 

        Styled H1

      </Typography>

      <Typography 
        variant="h1">

        Not styled H1

      </Typography>

      <Typography 
        variant="h1">

        Me neither

      </Typography>

    </Themed>
  );
};

export default App;

enter image description here

Working CodeSandbox.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Converting css styles to material ui makeStyles

How to style components using makeStyles and still have lifecycle methods in Material UI?

Angular 2 - Apply conditional style to a directive's child HTML element

Apply a particular style to all child elements of a Xamarin.Forms element

extend style material UI

How to change style of child element by root component in material UI?

Is there a way to move style object in PaperProps to makeStyles in material UI?

Material-UI | Using `theme` in makeStyles

Style problems with higher order component & Material-UI's makeStyles

material-ui makeStyles: how to style by tag name?

How to add a className to a child element with Material UI TextField

Material UI - Apply props to pseudo-element

How to apply style on NoOptionsText in autocomplete material-ui

Material ui: how to `makeStyles` with duplicate key?

Material UI-makeStyles My style doesn't work

how apply font feature settings in material UI makeStyles and change backgorundColor dynamically through props

Apply css style to child element of parent class

material ui - makeStyles doesn't create style properly

How to use Media queries with Material UI makestyles

Select child element in Inline styling ( Material UI makestyles )

set with and height on makeStyles theme material-ui

Material-ui makeStyles with both before and after

Makestyles hook is not working in Material Ui

makeStyles in Material-ui need to use so many ( !important <= css code ) to style mui components

How to style an element onDragOver in Material-UI?

How to apply style to child of element with active state

Set custom html attribute to child element of material ui component

Apply style to child that are under certain element

How to apply style of TableRow similar to TableHead in material-ui / mui?