Organize Grid items in a column using Material UI and React

Seeking assistance with adjusting the positioning of my Grid Items in Material UI.

Progress so far can be found on Codesandbox.

The issue at hand is how to align items 2 & 3 to the right of item 1. I've made some progress but struggling with strange spaces that I can't seem to resolve.

This is the desired end result: https://i.sstatic.net/F9ish.png

Thank you.

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import Typography from "@material-ui/core/Typography";
import Paper from "@material-ui/core/Paper";
import Divider from "@material-ui/core/Divider";
import Grid from "@material-ui/core/Grid";

const useStyles = makeStyles(theme => ({
  container: {
    // display: "grid",
    gridTemplateColumns: "repeat(12, 1fr)",
    gridGap: theme.spacing(3)
  },
  paper: {
    padding: theme.spacing(1),
    textAlign: "center",
    color: theme.palette.text.secondary,
    whiteSpace: "nowrap",
    marginBottom: theme.spacing(1)
  },
  w: {
    height: "100px"
  },
  divider: {
    margin: theme.spacing(2, 0)
  }
}));

export default function CSSGrid() {
  const classes = useStyles();

  return (
    <div>
      <Grid container spacing={3}>
        <Grid container direction="column">
          <Grid item xs={8}>
            <Paper className={classes.paper + " " + classes.w}>xs=8</Paper>
          </Grid>
        </Grid>
        <Grid
          container
          style={{ display: "table", height: "100%" }}
          direction="row"
        >
          <Grid item xs={4}>
            <Paper className={classes.paper}>xs=4</Paper>
          </Grid>
          <Grid item xs={4}>
            <Paper className={classes.paper}>xs=4</Paper>
          </Grid>
        </Grid>
      </Grid>
      <Divider className={classes.divider} />
    </div>
  );
}

Answer №1

Here are some important points to note from your code:

  • It is recommended to use one container for each grid field structure
  • Avoid setting the direction attribute to "column"
  • The sum of xs values should be less than or equal to 12 for items to appear in a single row.

Please refer to the material-ui grid documentation for better understanding.

<Grid container spacing={3}>
  <Grid item xs={8}>
    <Paper className={classes.paper + " " + classes.w}>xs=8</Paper>
  </Grid>
  <Grid item xs={2}>
    <Paper className={classes.paper}>xs=2</Paper>
  </Grid>
  <Grid item xs={2}>
    <Paper className={classes.paper}>xs=2</Paper>
  </Grid>
</Grid>
<Divider className={classes.divider} />

https://i.sstatic.net/FJxrW.png


Update:

<Grid container spacing={3}>
  <Grid item xs={8}>
    <Paper className={classes.paper + " " + classes.w}>xs=8</Paper>
  </Grid>
  <Grid item xs={4}>
    <Grid container>
      <Grid item xs={12}>
        <Paper className={classes.paper}>xs=4</Paper>
      </Grid>
      <Grid item xs={12}>
        <Paper className={classes.paper}>xs=4</Paper>
      </Grid>
    </Grid>
  </Grid>
</Grid>
<Divider className={classes.divider} />

https://i.sstatic.net/0ID5E.png

Try it out online:

https://codesandbox.io/s/material-demo-12m0e?fontsize=14&hidenavigation=1&theme=dark

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

Multiple Google Locations

I want to give users the ability to input multiple locations, such as: Melbourne, Perth, Sydney. Currently, I am using: <input id="searchTextField"></input> <script type="text/javascript"> function initi ...

How to Ensure the Final Column in an Angular Bootstrap Row Occupies the Remaining Available Space

Within my Angular5 application, there is a container component used to display a row with multiple components arranged in n columns, as shown below: <div class="row mx-5 my-5 h-75 w-80"> <div class="col-md-4 col-lg-4 m-0 p-0" *ngIf="advanc ...

What could be the reason for React Js default page not loading when initiating the command npm start?

When attempting to run my react project using the command below: G:\Node.js> cd reactproject G:\Node.js\reactproject> npm start I encountered the following error: Starting the development server... events.js:167 throw er; // Unhandl ...

I encountered a circular dependency problem while using redux-toolkit

Encountering this issue for the first time, but it's causing trouble now... // TS7022: 'rootReducer' implicitly has type 'any' because it does not // have a type annotation and is referenced directly or indirectly in its own initia ...

React Native: Issue with Higher Order Component when utilizing the `onLayout` prop

The functionality of this component involves rendering the Placeholder. Once it acquires its layout, this higher-order component (HOC) updates the state and invokes the child component with the values. Everything seems to be working fine up to this point. ...

Float over a specific line in a drawing

I am looking to develop a unique rating system using css, html, and potentially js : https://i.sstatic.net/pQP79.png My goal is for the user to hover over a specific section of a circular stroke and have it fill with a particular color, all while maintai ...

What is the best way to utilize the existing MUI state in order to calculate and show column totals?

I am currently in the process of developing an MUI web application to keep track of some personal data. Within this application, I have incorporated the MUI datagrid pro component to efficiently display the data with its robust filtering capabilities. In ...

Using JavaScript to add a class when hovering over an element

I am trying to customize the ul inside one of my li elements in the nav by adding a class when hovered. However, I am encountering an issue where the menu disappears when I try to click on it after hovering over it. I want to achieve this functionality usi ...

When a user hovers over the image, a button is revealed

I have a test page on my website: If you'd like to check it out, feel free to visit: I am trying to create a feature where when you hover over an image, a black button with text and links will appear. When you move your mouse away from the image, ...

Material-UI X-Grid encounters a resizing columns issue when utilized in RTL mode

When using the Material UI X-Grid component with an RTL theme, there seems to be an issue with resizing columns. When attempting to expand a column by dragging the left portion of the column separator to the left, it actually shrinks instead. This unexpect ...

Implementing a dropdown feature upon clicking a button with Material UI

Is there a way to display a dropdown menu when a button is clicked using material ui? Currently, I have managed to achieve this, but an additional select field is being populated unnecessarily. I am looking for a solution that does not involve hiding the ...

My toggleclass function seems to be malfunctioning

I am encountering a strange issue with my jQuery script. It seems to work initially when I toggle between classes, but then requires an extra click every time I want to repeat the process. The classes switch as expected at first, but subsequent toggles req ...

Dynamically adjust the image link in responsive mode to improve user experience

Is there a way to dynamically change the image links in responsive mode on WordPress? I am displaying thumbnails with a fixed size of 280*200, but I want to switch to medium-sized thumbnails (480*200) when the resolution is between 480px to 600px. Is there ...

Experience genuine native navigation with ReactNative

I'm interested in learning about native navigation in ReactNative, similar to how a new activity is pushed in Java (as seen in the Instagram app). I haven't been able to find information on it yet, but I'm looking for real native navigation ...

Create a basic grid layout using Bootstrap

I'm struggling to create this straightforward grid layout using Bootstrap: https://i.sstatic.net/fnQRt.png Essentially, I attempted the following method based on Bootstrap documentation: <td> <div class="row"> <div class=&q ...

Tips on adjusting the Leaflet Map's zoom level to display all markers in React Leaflet

I am currently working on a project with React Leaflet map that requires changing the center and zoom based on a set of markers. The goal is to adjust the zoom level so that all the markers are visible on the map. To achieve this change in view, I am util ...

What is the proper way to manage an action using redux-saga?

I have integrated redux-saga into my react project. One of the sagas in use is as follows: function* deleteCitySaga(payload) { const config = { headers: { Authorization: 'Bearer ' + loadState(), Accept: 'application/js ...

Utilizing Django framework for crafting a captivating homepage

Successfully set up the django framework and developed an app for my homepage. I added the app to the settings in the project folder and was able to get the HTML running smoothly. However, I am experiencing issues with styling as the CSS won't apply. ...

What is the best way to sort a list and then display it in pages?

In my dataset, a collection of objects is stored in a JSON array, which will be displayed using React. class App extends React.Component { constructor(props) { super(props); this.state = { data: [], ...

Changing the React Navigation header based on the screen displayed

I am working with a StackNavigation and I want to have a default Header (component Header) that will be applied to the "deep pages". These deep pages should show the default header generated for the React Navigation. In my index page **Index**, I only wan ...