Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to style material-ui textfield

I have been trying to work out how to style a material-ui TextField component.

<TextField     id="email"     label="Email"     className={classes.textField}     value={this.state.form_email}     onChange={this.handle_change('form_email')}     margin="normal" /> 

My classes are created as follows:

const styles = theme => ({     textField: {         width: '90%',         marginLeft: 'auto',         marginRight: 'auto',         color: 'white',         paddingBottom: 0,         marginTop: 0,         fontWeight: 500     }, }); 

My problem is that I can not seem to get the colour of the text field to change to white. I seem to be able to apply styling to the overall text field (because the width styling works etc)... but I think the problem is that I am not applying the styles further down the chain and into the actual input.

I have tried to look at the other answers dealing with passing inputProps but have had no success.

Have tried everything to the best of my ability but think I need to ask if anyone knows what I am doing wrong.

What it currently looks like

textfield with a blue background and a slightly lighter blue label

like image 620
Andre DiCioccio Avatar asked Oct 27 '17 02:10

Andre DiCioccio


People also ask

How do I style a TextField in material UI?

To style a React Material UI text field, we can call the makeStyles function with a function that returns an object that specifies the styles. Then we can set the InputProps prop of the TextField to specify the styles of the input element in the text field.

How do you style a TextField?

const styles = theme => ({ textField: { width: '90%', marginLeft: 'auto', marginRight: 'auto', color: 'white', paddingBottom: 0, marginTop: 0, fontWeight: 500 }, });

How do you customize TextField in MUI?

To make a controlled text field, we can add the value and onChange props to the TextField . We have the name state which is passed into the value prop. And the handleChange function which is passed into the onChange prop.

How do I change the color of TextField in material UI?

To change the text field font color in React Material UI, we call the makeStyles function with an object with the styles we want to apply. to call makeStyles with an object that has the input property that's set to an object with the color property set to 'blue' . Next, we call useStyles to return the classes object.


1 Answers

You need to add the InputProps property to the TextField.

const styles = theme => ({     textField: {         width: '90%',         marginLeft: 'auto',         marginRight: 'auto',                     paddingBottom: 0,         marginTop: 0,         fontWeight: 500     },     input: {         color: 'white'     } }); 

JSX:

<TextField     id="email"     label="Email"     className={classes.textField}     value={this.state.form_email}     onChange={this.handle_change('form_email')}     margin="normal"     InputProps={{         className: classes.input,     }} /> 

As an aside, you can also style the label or use an override as described here.

like image 144
Craig Myles Avatar answered Oct 11 '22 11:10

Craig Myles