javascript - material-ui <Grid item> component's children with 100% width overlaps parent - Stack Overflow

When I have a full width children in a Grid item, the children overlaps to the right side of its parent

When I have a full width children in a Grid item, the children overlaps to the right side of its parent.

I have reproduced the code with the problem I'm having.

import React, { Component } from "react";
import { Paper, Grid, Button } from "@material-ui/core";
import { withStyles } from "@material-ui/core/styles";
class Demo extends Component {
  render() {
    const { classes } = this.props;
    return (
      <Paper>
        <Grid container spacing={16}>
          <Grid item xs={6}>
            <Button variant="raised" fullWidth className={classes.button}>
              asdf
            </Button>
          </Grid>
          <Grid item xs={6}>
            <Button variant="raised" fullWidth className={classes.button}>
              asdf
            </Button>
          </Grid>
        </Grid>
      </Paper>
    );
  }
}
const styles = theme => ({
  button: {
    margin: theme.spacing.unit
  }
});
export default withStyles(styles)(Demo);

When I have a full width children in a Grid item, the children overlaps to the right side of its parent.

I have reproduced the code with the problem I'm having. https://codesandbox.io/s/rn88r5jmn

import React, { Component } from "react";
import { Paper, Grid, Button } from "@material-ui/core";
import { withStyles } from "@material-ui/core/styles";
class Demo extends Component {
  render() {
    const { classes } = this.props;
    return (
      <Paper>
        <Grid container spacing={16}>
          <Grid item xs={6}>
            <Button variant="raised" fullWidth className={classes.button}>
              asdf
            </Button>
          </Grid>
          <Grid item xs={6}>
            <Button variant="raised" fullWidth className={classes.button}>
              asdf
            </Button>
          </Grid>
        </Grid>
      </Paper>
    );
  }
}
const styles = theme => ({
  button: {
    margin: theme.spacing.unit
  }
});
export default withStyles(styles)(Demo);

Gives me the result below:

Share Improve this question edited Oct 10, 2018 at 10:13 Ramil Amparo asked Oct 10, 2018 at 9:04 Ramil AmparoRamil Amparo 6322 gold badges10 silver badges25 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

The problem isn't the fullWidth prop, but the margin you are setting to the buttons, you can do something like this instead:

https://codesandbox.io/s/nnxl20l2q0

import React, { Component } from "react";
import { Paper, Grid, Button } from "@material-ui/core";
import { withStyles } from "@material-ui/core/styles";
class Demo extends Component {
  render() {
    const { classes } = this.props;
    return (
      <Paper className={classes.paper}>
        <Grid container spacing={16}>
          <Grid item xs={6}>
            <Button variant="raised" fullWidth>
              asdf
            </Button>
          </Grid>
          <Grid item xs={6}>
            <Button variant="raised" fullWidth>
              asdf
            </Button>
          </Grid>
        </Grid>
      </Paper>
    );
  }
}
const styles = theme => ({
  paper: {
    padding: theme.spacing.unit
  }
});
export default withStyles(styles)(Demo);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745606624a4635708.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信