how to use css in JS for nested hover styles, Material UI

前端 未结 1 1236
梦如初夏
梦如初夏 2020-12-07 02:40

I\'m using Material UI and trying to convert normal css classes into js file.

.nav {
    list-style-type: none;
    m         


        
相关标签:
1条回答
  • 2020-12-07 03:45

    The correct syntax is "&:hover > div:hover": { ... }.

    Here is a working example demonstrating the syntax:

    import React from "react";
    import ReactDOM from "react-dom";
    
    import { makeStyles } from "@material-ui/core/styles";
    
    const useStyles = makeStyles({
      navlink: {
        border: "1px solid green",
        fontSize: "16pt",
        "&:hover": {
          backgroundColor: "lightgreen"
        },
        "&:hover > div:hover": {
          backgroundColor: "lightblue"
        }
      }
    });
    function App() {
      const classes = useStyles();
      return (
        <div className="App">
          <div className={classes.navlink}>
            Hello <div>CodeSandbox</div>
          </div>
        </div>
      );
    }
    
    const rootElement = document.getElementById("root");
    ReactDOM.render(<App />, rootElement);
    

    It also works to deeply nest with this syntax:

    const useStyles = makeStyles({
      navlink: {
        border: "1px solid green",
        fontSize: "16pt",
        "&:hover": {
          backgroundColor: "lightgreen",
          "& > div:hover": {
            backgroundColor: "lightblue"
          }
        }
      }
    });
    

    Here is the relevant JSS documentation: https://cssinjs.org/jss-plugin-nested/?v=v10.0.0-alpha.24

    Related answer:

    • How do you change a style of a child when hovering over a parent using material-ui jss styles
    0 讨论(0)
提交回复
热议问题