最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin2PV0评论

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);
Post a comment

comment list (0)

  1. No comments so far