The spacing in MUI Grid results in the grid having extra left padding

I'm having trouble creating a grid with a group of elements, specifically when I try to add spacing between them. Take a look at the image below with spacing={0} set on the Grid container: No spacing in Grid container

Now, here's what happens when I add spacing: Spacing added to Grid container = 3

I want the grid to match the width of the buttons positioned above it (refer to the screenshot). Despite trying various solutions, I can't seem to get it to work correctly. Here's my code snippet:

 return (
<Grid
  sx={{
    width: "100vw",
    overflowX: "hidden",
    padding: { xs: "20px", md: "50px" },
    margin: 0,
    background: "hsl(0, 0%, 98%)",
  }}
  container
>
  <Grid
    sx={{ width: "100%", marginBottom: "30px" }}
    item
    display="flex"
    flexWrap={"wrap"}
    justifyContent={"space-between"}
    gap={"25px"}
  >
    <SearchInput />
    <FilterMenu />
  </Grid>

  <Grid container spacing={3} xs={12}>
    {countries?.map((country, idx) => (
      <Grid item xs={3}>
        <CountryCard key={idx} country={country} />
      </Grid>
    ))}
  </Grid>
</Grid>

Answer №1

In my opinion, the optimal approach based on the provided images would be to divide your Grid containers. Create one Grid for the Search and Filter inputs and another for displaying your flags. This will allow for better spacing control and reduce the need for extensive CSS customizations on individual Grid items. Check out this codesandbox link where I have added some comments for reference. Let me know if it helps!

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

Using the Upload feature in ReactJS

I have been searching high and low for assistance in creating a component within React that can handle file uploads to a specific endpoint I have established. I have experimented with various options, including trying to integrate filedropjs. However, I u ...

Extracting JSON data from Stripe results - A comprehensive guide

I'm having trouble displaying the 'amount_total' in an h1 tag from my Stripe JSON response. Every time I try, I encounter this error: TypeError: Cannot read property 'map' of undefined Here is a snippet of the Stripe JSON output: ...

What could be causing my React useState not to update my components?

My understanding of React might be lacking, but I am facing an issue with updating a Component after the state has been asynchronously updated. const [appState, setAppState] = useState({ countries: null, languages: null }) useEffect(() ...

ReactJS with conditional closing tags

Here is a sample json response : {id: 1, name: a} {id: 2, name: b} {id: 3, name: c} {id: 4, name: d} {id: 5, name: e} {id: 6, name: f} I am looking to organize these by pairs in my React component like so : <div className="group-item"> ...

Tips for adjusting the width of a div when printing

Imagine I have two container elements named container1 and container2. First, I assign a width of 30% to container1 and 60% to container2. Then add a border between the two containers. However, when I attempt to print, container1 takes up the full 100% o ...

What steps should be followed to construct a window identical to the one depicted in the attached image using HTML and CSS?

Check out this link to see the window style I'm trying to recreate using HTML, CSS, and Javascript. No Jquery needed. Thank you in advance. ...

"Enjoy a full-screen background that adjusts its height while you scroll

Here is how my HTML elements are nested: body { app-root { app-block-page { app-block-content { div.wrapper { *content* } } } } } body has width:100% and ...

Creating dynamic routes using react-router-domorImplementing dynamic routes in react-router

Seeking guidance: I have a good grasp of creating static routes in React, but struggling with dynamic ones. Any explanation or assistance would be greatly appreciated. Imagine two components - one for rendering routes and another as a template for the rout ...

Issues with setting up a new React Native project on Windows causing error messages during initialization

Below is an excerpt from the npm-debug.log file. I followed the instructions on the React Native official website and ran react-native init AwesomeProject. After a while, the command prompt seemed to freeze and then displayed the following error message. ...

Customizing an external module class using CSS module pattern in React

Is there a way to locally override an external module's CSS class for a specific component? Here is my current code: import `style` from './style.module.css' // local CSS module import `ExternalComponent` from 'ExternalComponent&apos ...

The React application is experiencing difficulties in receiving the response data (JSON) from the Express server, despite the fact that

When making POST or GET requests to our Express server, served through PM2 on EC2, Postman receives the complete response with JSON data. However, our front end React app (both locally and deployed via CF) only gets the response status code and message. Th ...

What is the best way to compress @font-face using gzip?

Could you demonstrate how to gzip a webfont kit? The code provided by the generator is as follows...what modifications are necessary? @font-face { font-family: 'DesigersBold'; src: url('desib__-webfont.eot'); src: url(&apos ...

Ways to eliminate the navigation button on an HTML5 video player

I'm having a great experience using the HTML5 video player to continuously play videos. However, I have noticed that when my mouse cursor hovers over the video, a navigation button appears at the end of the screen. I would like to either remove or hid ...

Exploring the World of Github on Windows: Understanding the 'master' and 'gh-pages' Branches

I have developed a simple jQuery plugin and uploaded it to Github. I am using both the Github website and Github for Windows to manage this project. However, when I try to include the .js or .css files from Github using the Raw links, my browser fails due ...

Leveraging Chrome USB tethering to evaluate the functionality of React and NodeJS applications using an Android device. Struggling to establish a connection with the

Currently, I am facing some issues with debugging errors on my mobile device. My setup includes a React front end (localhost:3000) and a NodeJS back end (localhost:5008). I have successfully connected my android phone to run the front end on its browser. A ...

Incorporate a generic type into a React Functional Component

I have developed the following component: import { FC } from "react"; export interface Option<T> { value: T; label: string; } interface TestComponentProps { name: string; options: Option<string>[]; value: string; onChang ...

Bootstrap carousel powered by AngularJS

Struggling to incorporate bootstrap, angular, and a carousel to showcase images from local folders on my drive. Unsure how to customize the angular file properly. The example provided in the link below fetches images from the Internet, but I need to modify ...

Error message 'Module not found' occurring while utilizing dynamic import

After removing CRA and setting up webpack/babel manually, I've encountered issues with dynamic imports. https://i.sstatic.net/CRAWr.png The following code snippet works: import("./" + "CloudIcon" + ".svg") .then(file => { console.log( ...

Steps to alter background image and adjust its height upon function activation

I am working on a search page with an advanced search option that only certain users can access. I need the height of my div to increase accordingly and also change the background image to a larger size when the advanced search is selected. How can I make ...

Using the mailto functionality with Material UI

Need help with using the mailto feature in Material UI. I attempted to include a simple <ListItem button component={Link} mailto='' /> within a ListItem, but it did not work. I also experimented with: <ListItem button > <i cla ...