Grid within a grid, spanning the entire width of the screen

After taking a glance at the screenshot provided below, I am striving to achieve the design labeled as "Lg" utilizing the Grid component from Material UI.

  • The box labeled with the number "3" has been fashioned by employing a grid configuration of 9 by 3
  • On the other hand, the box bearing the number "9" has been structured using a grid layout of 9 by 9

Query:
How do I determine the width of Lg using the Grid system? Essentially, the dimensions of Sm and Lg are calculated as follows:

  • Sm: 100% / 12 * 9 / 12 * 3
  • Lg: 100% - 100% / 12 * 9 / 12 * 3

Sample code:

import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import Paper from '@material-ui/core/Paper';
import { Grid, Box } from '@material-ui/core';

const useStyles = makeStyles((theme) => ({
  root: {
    flexGrow: 1,
    backgroundColor: "bisque",
    padding: theme.spacing(1),
  },
  paper: {
    padding: theme.spacing(2),
    textAlign: 'center',
    color: theme.palette.text.secondary,
  },
  container: {
    display: "flex",
  },
  boxSm: {
    marginTop: theme.spacing(1),
    width: "calc(100% / 12 * 9 / 12 * 3)",
  },
  boxLg: {
    marginTop: theme.spacing(1),
    width: "calc(100% - 100% / 12 * 9 / 12 * 3)",
  },
  box: {
    marginTop: theme.spacing(1),
  },
}));

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

  return (
    <div className={classes.root}>
      <Grid container>
        <Grid container xs={9} spacing={0}>
          <Grid item xs={3}>
            <Paper className={classes.paper}>3</Paper>
          </Grid>
          <Grid item xs={9}>
            <Paper className={classes.paper}>9</Paper>
          </Grid>
        </Grid>
      </Grid>
      <Box className={classes.container}>
        <Box className={classes.boxSm}>
          <Paper className={classes.paper}>Sm</Paper>
        </Box>
        <Box className={classes.boxLg}>
          <Paper className={classes.paper}>Lg</Paper>
        </Box>
      </Box>
      <Box className={classes.box}>
        <Paper className={classes.paper}>Full width</Paper>
      </Box>
    </div>
  );
}

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

Answer №1

If you define the breakpoints for xs and lg screens, your layout will automatically adjust when switching from small to large screens.

<Grid container xs={9} lg={12} spacing={0}>
      <Grid item lg={6} xs={3}>
        <Paper className={classes.paper}>3</Paper>
      </Grid>
      <Grid item lg={6} xs={9}>
        <Paper className={classes.paper}>9</Paper>
      </Grid>
    </Grid>

By specifying the size for each screen, your content will extend to fit the screen when switching to a larger display. Full code below:

import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import Paper from '@material-ui/core/Paper';
import { Grid, Box } from '@material-ui/core';

const useStyles = makeStyles((theme) => ({
root: {
flexGrow: 1,
backgroundColor: "bisque",
padding: theme.spacing(1),
},
paper: {
 padding: theme.spacing(2),
 textAlign: 'center',
 color: theme.palette.text.secondary,
},
container: {
display: "flex",
},
boxSm: {
marginTop: theme.spacing(1),
width: "calc(100% / 12 * 9 / 12 * 3)",
},
boxLg: {
marginTop: theme.spacing(1),
width: "calc(100% - 100% / 12 * 9 / 12 * 3)",
},
box: {
marginTop: theme.spacing(1),
},
}));

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

return (
 <div className={classes.root}>
  <Grid container>
    <Grid container xs={9} lg={12} spacing={0}>
      <Grid item lg={6} xs={3}>
        <Paper className={classes.paper}>3</Paper>
      </Grid>
      <Grid item lg={6} xs={9}>
        <Paper className={classes.paper}>9</Paper>
      </Grid>
    </Grid>
  </Grid>
   <Box className={classes.container}>
    <Box className={classes.boxSm}>
      <Paper className={classes.paper}>Sm</Paper>
    </Box>
    <Box className={classes.boxLg}>
      <Paper className={classes.paper}>Lg</Paper>
    </Box>
  </Box>
  <Box className={classes.box}>
    <Paper className={classes.paper}>Full width</Paper>
  </Box>
</div>
);
}

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

Minimize unnecessary rendering in React when toggling between tabs

I am currently working on a React application that utilizes material-ui to generate tabs. <div className={classes.root}> <AppBar position="static"> <Tabs value={value} onChange={handleChange}> <Tab label="Item One" /> ...

What could be causing my Page to not update when the Context changes?

In my Base Context, I store essential information like the current logged-in user. I have a User Page that should display this information but fails to re-render when the Context changes. Initially, the Context is empty (isLoaded = false). Once the init fu ...

Tips for determining the width of an image and utilizing that measurement as the height in order to create a balanced square image

I am currently facing an issue with my code that is used to retrieve the width of an image which is set in percentages. Although I am able to obtain the width in pixels, I am struggling with correctly inserting the variable into the CSS property value usin ...

Warning in the console for react-ga: "Hey there! Don't forget to include the gaTrackingID in the initialize()

Currently, I am attempting to integrate React-GA into my create-react-app project which has already been ejected. However, I keep encountering a console warning that reads as follows. [react-ga] gaTrackingID is required in initialize() In order to addres ...

Difficulty in retrieving nested objects from JSON response using ReactJS and axios

Encountering difficulties in extracting data from the JSON response retrieved from the wagtail CMS: { "meta": { "total_count": 1 }, "items": [ { "id": 13, "meta": { "type": "home.HomePage", "detail_url": "http://l ...

Modifying the appearance and behavior of an element dynamically as the page is being loaded using AngularJS

Struggling with a challenge for two days now, I have attempted to implement a panel-box using bootstrap and AngularJS. Within the confines of a controller, my code looks like this: <div id="menu2a"> <div class="panel list-group"> <div ...

"Unlocking the Dialog Box: A Step-by-Step Guide to Programatically Opening Material UI Dialog

Typically, Material UI's Dialog is used as shown below, following the documentation: export default function AlertDialog() { const [open, setOpen] = React.useState(false); const handleClickOpen = () => setOpen(true); const handleClose = () =& ...

What is the proper way to define an element's style property in strict mode?

Assuming: const body = document.getElementsByTagName('body')[0]; const iframe = document.createElement('iframe'); iframe.src = protocol + settings.scriptUrl + a; iframe.height = 1; iframe.width = 1; iframe.scrolling = 'no'; ...

Although server-side rendering is utilized with react-query, JSON data continues to be displayed in the network tab

As per my understanding, the only way to conceal a JSON return from an API call is through server rendering. I have implemented this on all pages using Next.js; however, the network tab still displays a JSON with my data on pages where useInfiniteQuery and ...

The width of the Div is set to 100%, yet there is still some empty space visible on

I am having trouble setting the background of my code to display a picture that is 300px in height and 100% in width. However, when I set the width to 100%, there are about 15px gaps on each side. I could adjust the width to 1000px, but that presents issue ...

Attempting to prevent the use of the <p> tag solely before the text

My toggle function is not working correctly in my FaQ section. When a user clicks on a topic, the bottom section should appear, but the <p> tag is being displayed across the entire line instead of just one word.. Here is an image to help illustrate ...

The search results fail to show the required information

I am trying to retrieve data from an API based on a search query. When the user enters the name of the film they are looking for and hits enter to submit the form, the matching films should be displayed on the screen. However, my console is showing errors ...

What is the best way to update an object within a deeply nested array of objects using lodash?

I am facing a challenge in my React JS application where I have an array of objects that I need to dynamically modify. Here is the structure of my array : sections: [ { id: 'e9904688-fd8a-476d-8f46-930bc4d888d1', ...

Can the Bootstrap grid be arranged vertically?

I'm finding it difficult to work on a project with Bootstrap's horizontal grid system. What I want to achieve is to have my page divided into 4 sections, with the left side blocks having equal height and the right side blocks having varying heigh ...

Adding a hyperlink to each table cell (td) in HTML and CSS that is visible outside of the table

In my table, there is a link on every td. I want these links to appear outside the table. Although I have achieved this, the first cell that was created needs to be hidden. When I try to hide the td using CSS, the link also disappears. Here is the HTML: ...

Integrate the dForm plugin with a convenient time picker widget

Currently, I am attempting to integrate a time picker widget into a jQuery plugin called dForm. The specific timepicker widget can be found at the following link: https://github.com/jonthornton/jquery-timepicker Despite reviewing the dForm documentation, ...

Can a child element be positioned above its parent's y-scrollbar using CSS?

I have created a selection box using <ul> and <li> elements. Some of the list items are wider than the parent box, so I used an :after pseudo-element to overlay them with full content when hovered over. Everything was working perfectly, until ...

The button is only partially visible

My HTML code for a small web app has a button that says "ok," but it's too tiny to display the text fully. I wonder if I'm missing something with the span element? How can I enlarge the button so that it's bigger and shows the entire "ok" ...

Where do I find the resultant value following the completion of a video production through editly?

Hey there, I have a quick question... I was following the instructions in the README for editly, and I successfully created videos by calling editly like this: // creating video editly(editSpec) .catch(console.error); The only issue is that I am using Ex ...

What type of HTML tag does the MUI Autocomplete represent?

Having trouble calling a function to handle the onchange event on an autocomplete MUI element. I've tried using `e: React.ChangeEvent`, but I can't seem to locate the element for the autocomplete component as it throws this error: The type &apos ...