How can I get input radio elements to horizontally align in react [material-ui]?
To render the radio buttons in a row:
<RadioButtonGroup style={{ display: 'flex' }}>
To reset sizing according to content:
<RadioButton style={{ width: 'auto' }} />
Simply add the prop row={true} on the RadioGroup control.
<RadioGroup
aria-label="Location"
name="location"
className={classes.group}
value={location}
onChange={handleChange}
row={true}
>
<FormControlLabel value="company" control={<Radio />} label="Company" />
<FormControlLabel value="home" control={<Radio />} label="Home" />
<FormControlLabel value="other" control={<Radio />} label="Other" />
</RadioGroup>
Simply use the row
property:
<RadioGroup row><Radio /><Radio /></RadioGroup>
RadioGroup
inherits from FormGroup
so the properties of the FormGroup
component are also available.
Another example, with labels:
<RadioGroup aria-label="anonymous" name="anonymous" value={false} row>
<FormControlLabel value="true" control={<Radio />} label="Yes" />
<FormControlLabel value="false" control={<Radio />} label="No" />
</RadioGroup>
For those who are still struggling, use this style:
const styles = theme => ({
group: {
width: 'auto',
height: 'auto',
display: 'flex',
flexWrap: 'nowrap',
flexDirection: 'row',
}
});
class MyComponent extends React.Component {
render() {
const { classes } = this.props;
<RadioGroup className={classes.group} ...>
}
};
export default withStyles(styles)(MyComponent);