What is the method to add gutters in Material UI grid without including margins?

I've been struggling for ages to make my responsive layout work properly! Whenever I add spacing to the Grid container with class=spotlight, things get misaligned. How can I add spacing to the gutters (the space between Grid items within the spotlight container Grid) without affecting the margins (the space outside the grid)? Here's my code:

import React from "react";
import { Grid, makeStyles, Typography } from "@material-ui/core";
import MasterLayout from "../components/MasterLayout";
import ContentThumbnail from "../components/ContentThumbnail";
import ContentList from "../components/ContentList";

const useStyles = makeStyles((theme) => ({
  masterLayout: {
    padding: theme.spacing(3),
  },
  spotlight: {},
}));

const DefaultDashboard = ({ padding }) => {
  const classes = useStyles();
  return (
    <MasterLayout>
      {/*Top row with right bar */}
      <Grid
        container
        className={classes.masterLayout}
        spacing={3}
        alignItems="flex-start"
      >
        {/* Spotlight section */}
        <Grid container xl={8} lg={12} item className={classes.spotlight}>
          <Grid item xs={12}>
            <Typography variant="h3">Spotlight</Typography>
          </Grid>
          <Grid item xl={3} lg={3} md={3} xs={6}>
            <ContentThumbnail />
          </Grid>
          <Grid item xl={3} lg={3} md={3} xs={6}>
            <ContentThumbnail />
          </Grid>
          <Grid item xl={3} lg={3} md={3} xs={6}>
            <ContentThumbnail />
          </Grid>
          <Grid item xl={3} lg={3} md={3} xs={6}>
            <ContentThumbnail />
          </Grid>
        </Grid>
        {/* List section 1 */}
        <Grid container item xl={4} lg={3} md={6} xs={12}>
          <Grid item xs={12}>
            <Typography variant="h3">Title 2</Typography>
          </Grid>
          <Grid item xs={12}>
            <ContentList />
          </Grid>
        </Grid>
        {/* List section 2 */}
        <Grid container item xl={4} lg={3} md={6} xs={12}>
          <Grid item xs={12}>
            <Typography variant="h3">Title 2</Typography>
          </Grid>
          <Grid item xs={12}>
            <ContentList />
          </Grid>
        </Grid>
        {/* List section 3 */}
        <Grid container item xl={4} lg={3} md={6} xs={12}>
          <Grid item xs={12}>
            <Typography variant="h3">Title 3</Typography>
          </Grid>
          <Grid item xs={12}>
            <ContentList />
          </Grid>
        </Grid>
        {/* List section 4 */}
        <Grid container item xl={4} lg={3} md={6} xs={12}>
          <Grid item xs={12}>
            <Typography variant="h3">Title 4</Typography>
          </Grid>
          <Grid item xs={12}>
            <ContentList />
          </Grid>
        </Grid>
      </Grid>
    </MasterLayout>
  );
};

export default DefaultDashboard;

Thank you,

Katie

Answer №1

Summary: Modify the style of your Grid items:

const useStyles = makeStyles((theme) => ({
//...
  gutter:{
    marginLeft: 80, // adjust as needed
    '&:first-child':{
      marginLeft: 0,
    }
  }
}));
//...
<Grid ... item classes={{item:classes.gutter}}>

The grid items have 80 pixel wide gutters (indicated by blue outlines, except for the first): https://i.sstatic.net/YE9Jv.png

Details: Check out this demo for more information. By utilizing the Grid API and the classes object, you can customize the internal styles of the component. I included a pseudo-class to create spacing for all children except the first one, resulting in inner gutters.

Answer №2

Perhaps my understanding is off, but have you considered adjusting the 'alignItems' prop to stretch, or experimenting with 'alignContent' instead?

You may find this link helpful.

If this doesn't address your needs, providing a sandbox demo could make it easier to assist further.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

row-1 css classes on the top in a project built with Blazor

I'm currently working on customizing the standard layout provided in Blazor projects like WASM or server, but I'm a bit perplexed by the top-row px-4 css classes. Within the HTML code that appears to define the top row/header section is this sni ...

The CSS background image is not functioning

.menubar li { float: left; width: 115px; text-align: center; background-image: url(js/left_main_bg_image.gif); } Having issues with the background image not displaying properly in IE, Firefox, and Chrome. I've tried adding it to a tab ...

Allow images to extend beyond the boundaries of the grid in Bootstrap 4

I'm trying to figure out how to display an image that starts inside a grid but scales to the left or right of the screen. The current issue is that the container limits the width, and I need it to allow the image to extend beyond the grid. If you hav ...

Ways to create a smooth transition in element height without a known fixed target height

Is it possible to create a smooth height transition for an element when the target height is unknown? Replacing height: unset with height: 100px allows the animation to work, but this presents a problem as the height needs to be based on content and may v ...

Is it possible to streamline the restart process in my game and eliminate the extra unnecessary click?

This game requires you to click each image once without clicking one twice. The issue arises when trying to reset the game after all images are clicked. Currently, it requires an extra random click after all 12 images have been clicked in order to render t ...

retrieve essential data from Firebase using JavaScript

Currently, I am utilizing the get() method to retrieve data in my React Native application. Here is the code snippet that I am using: firebase .firestore() .collection("users") .doc("test") .get() .then(response => { console.log(respo ...

How can I adjust the width of a handle/thumb for NoUiSlider?

Looking to adjust the width of a NoUiSlider using CSS: .noUi-horizontal .noUi-handle { width:8px; height:25px; left: 0px; top: -8px; border: 0px solid #000000; border-radius: 0px; background: #000; cursor: default; box- ...

Guide on incorporating a YouTube iframe in React with Typescript

It appears that Typescript is posing some challenges for me in this scenario. Here's the code snippet I am trying to include: <iframe width="560" height="315" src="https://www.youtube.com/embed/BLAH?showinfo=0" frameBorder="0" ...

Updating the material-ui checkbox state to reflect the checked, unchecked, or indeterminate status, can be achieved in reactjs without relying on state

I am currently using Material-UI checkbox components and I have a requirement to programmatically change the state of checkboxes to be checked, unchecked, or indeterminate based on the click of another checkbox. This action needs to be applied to a list of ...

Interference in the output of .innerHTML leads to disruption

I have been working on a feature to display a calculated price based on the selected quantity for each individual product. I attempted to duplicate the code and modify variable names, but encountered issues with the increase/decrease buttons triggering mul ...

Tips on accessing close autoComplete/TextField title in AppBar

Looking to add a search bar and login button in the AppBar, where the search Bar is positioned close to the title. The desired order for the AppBar components should be as follows: Title SearchBox LoginButton How can this be achieved? Below is th ...

Guide to updating the color of the disabled track on Mui's <Slider/>

Is there a way to customize the disabled color of the track in Mui Slider? Here's an example of the slider component in question: <Slider aria-label="Volume" value={60} disabled onChange={() => console.log('')} /> I atte ...

Changes in React BrowserRouter URLs are not reflected on the page or components, requiring a manual refresh for them to

Greetings, fellow developers! I have developed an app using React with a remote menu component. Despite trying numerous techniques, I am facing an issue where my URL changes but the components are not rendering on the screen. You can check out the code h ...

Turn off the functionality of Telerik in DotNetNuke

After installing the DNN module on a remote machine, I noticed that it altered the CSS styling. Upon inspecting it with Firebug, I discovered that the styles causing issues are located in telerik.web.ui.webresource.axd, making it difficult to overwrite the ...

What is the best method for updating the state of a dynamically created Switch component using React's setState()?

I have a dynamic creation of Switches based on a map like shown in the image: https://i.stack.imgur.com/jDLbS.png For example, using this code: const [enabled, setEnabled] = useState(false) return( ... {people.map((person) => ( ... ...

Error: Unable to locate module: Unable to resolve '@/src/components/layout/Header'

Compilation Error Encountered issue in Next.js (version 14.2.4) while trying to locate '@/src/components/layout/Header' at ./src/app/layout.js:3:1 Suddenly, 2 days ago the code was functioning properly but now it's throwing this error. Er ...

When using jQuery to select elements of a specific class, make sure to exclude the element that triggered the

A dynamic number of divs are generated from a data source. Each div contains an image button and another div with text. While the actual scenario is more complex, we can present a simplified version: <div id="main"> <div id="content_block_1" ...

Is it necessary to bring in 'React' while developing hooks? (React-hooks)

I came across examples at https://reactjs.org/docs/hooks-custom.html where they consistently include the following code: import React, { useState, useEffect } from 'react'; However, I noticed that React is not actually used in the file. Is it n ...

Certain parts of the CSS grid are not aligning properly within the grid lines

I'm having trouble with my CSS grid. The red section fits in the first square, but the rest of the content seems to be out of place in the grid. I'm new to all this and I can't figure out what I'm missing. I've tried everything but ...

Leverage scope variables and dynamic CSS properties when using ngClass

Is there a way to manipulate the CSS dynamically in HTML using a scope variable? <div ng-class="myClassScope, { 'dynamic-class': !ifIsNot }"> ...