Trying to customize the width of the Material UI Select
component can be a bit tricky. To set the desired width, you need to assign a specific class to the FormControl
component. This class, such as mw-120
, should define the minimum width as 120px in your CSS.
Here is an example using the Material UI Select component:
<FormControl className='mw-120'>
<InputLabel htmlFor='selected-language'>Language</InputLabel>
<Select value={this.state.selectedLanguage}
onChange={(e) => this.onLanguageChange(e.target.value)}
inputProps={{
name: 'language',
id: 'selected-language',
}}>
{menuItems}
</Select>
</FormControl>
Your CSS class definition for the width:
.mw-120 {
min-width: 120px;
}
However, even after applying the class, the width of the Select
component may not change as expected. In some cases, like shown in the image below, the component remains narrow and does not extend beyond the label "Language."
https://i.sstatic.net/aRmEJ.png
An inspection using Chrome Developer Tools reveals that the main DIV element of the component has a class .MuiFormControl-root-234
with min-width: 0;
, which seems to override your custom class. Is there another method to adjust the width of the Material UI Select component effectively? Unfortunately, the official Material UI documentation lacks clear examples on this aspect.