Material UI inline styling - specific component colours

末鹿安然 提交于 2021-02-10 14:53:17

问题


I'm trying to style my TextFields from Material-UI. I have a black background and I need both the textField border and text to be white. Here's my (relevant) code:

render() {
    const styles = {
      width: {
        width: '90%',
        margin: '5px',
        padding: '5px',
        textColor: '#ffffff',
        hintColor: '#ffffff',
        floatingLabelColor: '#ffffff',
        disabledTextColor: '#673ab7',
        focusColor: '#c2185b',
        borderColor: '#ffffff'
      },
      button: {
        margin: '15px',
        padding: '20px',
        width: '60%'
      }
    };

<TextField
              className="classes.textField"
              label="Name Your Vice"
              type="text"
              name="vice"
              value={this.props.vice}
              margin="normal"
              variant="outlined"
              style={styles.width}
              onChange={this.props.handleInputChange}
            />

What am I missing to get this to work?

Thanks


回答1:


I found that Material UI required a lot of digging down into the components to make very basic changes. I instead used Materialize (a more friendly version of Material UI) and found it relatively simple to customise my components.




回答2:


TextField is an abstraction of several other components. from the documentation:

Advanced Configuration

It's important to understand that the text field is a simple abstraction on top of the following components:

  • FormControl
  • InputLabel
  • List item
  • Input
  • FormHelperText

Some of the stylings you are trying to do apply to the Input.

So your code should look a bit like this:

const styles = {
  input: {
    backgroundColor: 'red',
  },
}

< TextField InputProps = {{ style: styles.input }} />



回答3:


To set the border and background color with TextField variant "outlined" you need to target the fieldset.

You can do something like this:

const styles = {
  textField: {
    [`& fieldset`]: {
      border:"1px solid #fff",
      backgroundColor: "#fff"
    }
};


来源:https://stackoverflow.com/questions/52794248/material-ui-inline-styling-specific-component-colours

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!