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

前端 未结 1 1259
梦如初夏
梦如初夏 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条回答
  •  旧时难觅i
    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 (
        
    Hello
    CodeSandbox
    ); } const rootElement = document.getElementById("root"); ReactDOM.render(, 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)
提交回复
热议问题