70

I'm trying to reduce the width of the TextField component :

Here is the render method:

  render() {
    return (
      <div>
          <div>
            <form onSubmit={this.handleSubmit}>
                <TextField
                  hintText="Email"
                  ref="email"
                /><br/>
                <TextField
                  hintText="Password"
                  type="password"
                  ref="password"
                /><br/>
              <button className="btn btn-success" onClick={this.loginCommand}><i className="fa fa-sign-in"/>{' '}Log In</button>
            </form>
          </div>
      }
      </div>
    );
  }
}

enter image description here

1
  • I've looked at the parameters, inputStyle etc... Commented Jan 29, 2016 at 20:37

4 Answers 4

90

You also can look at fullWidth property to make sure it is set properly.

<TextField
      id="full-width-text-field"
      label="Label"
      placeholder="Placeholder"
      helperText="Full width!"
      margin="normal"
      fullWidth // this may override your custom width
/>
Sign up to request clarification or add additional context in comments.

1 Comment

This does not "reduce" the width of the component as OP had asked. This actually will increase the width in certain cases.
51

You could either specify inline style on element like below

  <TextField
        hintText="Email"
        ref="email"
        style = {{width: 100}} //assign the width as your requirement
   />

Or you could do as below.

Declare a styles variable with css properties.

var styles = StyleSheet.create({
    textFld: { width: 100, height: 40}   //assign the width as your requirement
});

Assign it to style in your render code.

<TextField
              hintText="Email"
              ref="email"
              style = {styles.textFld}
            />

Give it try let me know if it works for you. I am new to react js as well.

You could refer to documentations and other similar question on SO for clarity. http://facebook.github.io/react-native/docs/style.html#content

http://facebook.github.io/react-native/

http://facebook.github.io/react-native/docs/flexbox.html#content

React.js inline style best practices

2 Comments

is it working? My text field is working correct if width is 250 px but for smaller width it is not working
@MuneemHabib Same, the width on my TextField doesn't go below 220px. Edit: the solution is to set the minWidth and width. eg: sx={{ minWidth: 100, width:100}}
11

If you want to make the TextField's width expanded as much as the parent's width (width: 100%):

<TextField label="Full width" fullWidth />

If you want to set the TextField's width to the exact value:

<Box width={350}>
  <TextField label="width: 350px" fullWidth />
</Box>

You can also set the style of the TextField directly if you don't want to add a wrapper component:

V5

<TextField label="width: 350px" sx={{ width: 350 }} />

V4

const useStyles = makeStyles({
  root: {
    width: 350
  }
});

function App() {
  const classes = useStyles();
  return <TextField label="width: 350px" className={classes.root} />;
}

I'd advice you against using inline styles as suggested in other answers since inline styles are harder to override. You should by default use Material-UI styles because it's more flexible and better integrated with Material-UI components. For example, you can cherry-pick the sub-components to override in a larger components, or style psuedo classes and elements, which can't be done with inline style.

Live Demo

Edit 35093107/how-to-override-the-width-of-a-textfield-component-with-react-material-ui

1 Comment

The sx part of the answer should work; it doesn't :(
0

In "modern" MUI the width of the <TextField> component can be set through the sx prop:

The sx prop is the best option for adding style overrides to a single instance of a component in most cases. It can be used with all Material UI components.

<TextField sx={{width: 200}}/>

This will override the default width style of the component to 200px

https://mui.com/material-ui/api/text-field/#props

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.