material-ui <grid item =“”>组件的子项(宽度为100%)与父项重叠

时间:2018-10-10 09:04:13

标签: javascript css reactjs material-ui

当我在Grid项中有一个全角的子级时,这些子级会重叠到其父级的右侧。

我转载了遇到问题的代码。 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);

给我以下结果: enter image description here

1 个答案:

答案 0 :(得分:1)

问题不是fullWidth属性,而是您为按钮设置的边距,您可以执行以下操作:

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);