Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
808 views
in Technique[技术] by (71.8m points)

reactjs - MakeStyles (Material UI) apply style to child element

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.

question from:https://stackoverflow.com/questions/65895184/makestyles-material-ui-apply-style-to-child-element

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...