Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply margin between Material-UI Grid items?

How can we add margin (empty space) between Material-UI Grid items?

Container's spacing attribute only adds padding on items.

import React from "react";
import ReactDOM from "react-dom";
import { Grid } from "@material-ui/core";
import { makeStyles } from "@material-ui/core/styles";

const useStyles = makeStyles({
  root: {
    backgroundColor: "red"
  },
  root2: {
    backgroundColor: "green"
  }
});

function App() {
  const classes = useStyles();
  return (
    <Grid container spacing={2}>
      <Grid item xs={6} className={classes.root}>
        hi
      </Grid>
      <Grid item xs={6} className={classes.root2}>
        hi
      </Grid>
    </Grid>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
like image 813
Hasan Sefa Ozalp Avatar asked Oct 15 '22 06:10

Hasan Sefa Ozalp


1 Answers

Change xs attribute so that the total is less than 12 in that row to have some space.

Add margin: "auto" or any other margin as you wish to your items.

import React from "react";
import ReactDOM from "react-dom";
import { Grid } from "@material-ui/core";
import { makeStyles } from "@material-ui/core/styles";

const useStyles = makeStyles({
  root: {
    backgroundColor: "red",
    margin: "auto"
  },
  root2: {
    backgroundColor: "green",
    margin: "auto"
  }
});

function App() {
  const classes = useStyles();
  return (
    <Grid container spacing={2}>
      <Grid item xs={5} className={classes.root}>
        hi
      </Grid>
      <Grid item xs={5} className={classes.root2}>
        hi
      </Grid>
    </Grid>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
like image 64
Hasan Sefa Ozalp Avatar answered Oct 21 '22 06:10

Hasan Sefa Ozalp