In Material UI, How can I override a selector selected component style?
Asked Answered
M

1

1

In Material UI, to extend the distance between MuiInputLabel and MuiInput, I have to override the marginTop of label + .MuiInput-formControl.

However, createMuiTheme's override only provide direct override of a Mui Component CSS, such as:

createMuiTheme({
  overrides: {
    MuiInput: {
      formControl: {
        marginTop: '1.5rem',
      },
    },
  }
})

How can I do something like:

createMuiTheme({
  overrides: {
    'label+MuiInput': {
      formControl: {
        marginTop: '1.5rem',
      },
    },
  }
})

Thanks...

Murky answered 16/2, 2019 at 16:3 Comment(0)
E
14

Here's the relevant JSS documentation:

https://cssinjs.org/jss-plugin-nested?v=v10.0.0-alpha.10#use--to-reference-selector-of-the-parent-rule

Here's the syntax you need:

const theme = createMuiTheme({
  overrides: {
    MuiInput: {
      formControl: {
        "label + &": {
          marginTop: "1.5rem"
        }
      }
    }
  }
});

Here's a working example:

Edit 24v1wr9x0n

Expertise answered 16/2, 2019 at 18:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.