Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nesting Grid with Material-UI

So, I am having issues with getting material-ui grid to work as i want. I am trying to render a row with two columns.

import React from 'react';
import DefaultLayout from '../layouts/default';
import Grid from '@material-ui/core/Grid';

class profile extends React.Component {
  render() {

    return (
    <React.Fragment>
      <Grid container spacing={8}>
      <Grid item xs={12} style={{backgroundColor: 'blue', height: '250px'}}></Grid>
      <Grid item xs={12} style={{backgroundColor: 'grey', height: '250px'}}></Grid>

      {/* Form two columns with next row*/}

      <Grid container={'true'} item sx={12}>
        <Grid item={'true'} xs={6} md={8} style={{backgroundColor: 'green', height: '50px'}}></Grid>
        <Grid item={'true'} xs={6} md={4} style={{backgroundColor: 'orange', height: '50px'}}></Grid>
      </Grid>
    </Grid> 
  </React.Fragment>
)}}

module.exports = profile;

Currently, It is rendering:

enter image description here

I want the orange and green rows to be two columns on the same row. Side by side.

Can anyone decipher what is wrong with my code?

@material-ui/core: ^3.9.2

react: ^16.7.0

like image 347
adamUpchurch Avatar asked Feb 16 '19 02:02

adamUpchurch


2 Answers

I think the issue is that your first two 'rows' are marked with xs={12} and will take up the full page width. I'm also transitioning out of bootstrap (man...who knew react-bootstrap would be so buggy right!) and it's a bit of an adjustment...but in case you didn't figure it out, try this on?

import React from 'react';
import DefaultLayout from '../layouts/default';
import Grid from '@material-ui/core/Grid';

class profile extends React.Component {
  render() {

    return (
    <React.Fragment>
      <Grid container spacing={8}>
      //the below columns will be full width! I've changed the 12 to 6
      <Grid item xs={6} style={{backgroundColor: 'blue', height: '250px'}}></Grid>
      <Grid item xs={6} style={{backgroundColor: 'grey', height: '250px'}}></Grid>

      {/* Form two columns with next row*/}

      <Grid container={'true'} item sx={12}>
        <Grid item={'true'} xs={6} md={8} style={{backgroundColor: 'green', height: '50px'}}></Grid>
        <Grid item={'true'} xs={6} md={4} style={{backgroundColor: 'orange', height: '50px'}}></Grid>
      </Grid>
    </Grid> 
  </React.Fragment>
)}}

module.exports = profile

I haven't tested it out but should work?

like image 105
Union In Design Avatar answered Sep 26 '22 00:09

Union In Design


You need to use GridList and GridListTitle.

Please try with below code to get desired output

import React from 'react';
import DefaultLayout from '../layouts/default';
import GridList from "@material-ui/core/GridList";
import GridListTile from "@material-ui/core/GridListTile";

class profile extends React.Component {
  render() {

    return (
    <React.Fragment>
      <GridList cols={1}>
          <GridListTile style={{backgroundColor: 'green', height: '50px'}}>

          </GridListTile>
          <GridListTile style={{backgroundColor: 'orange', height: '50px'}}>

          </GridListTile>
      </GridList>
      <GridList cols={2}>
          <GridListTile style={{backgroundColor: 'green', height: '50px'}}>

          </GridListTile>
          <GridListTile style={{backgroundColor: 'orange', height: '50px'}}>

          </GridListTile>
      </GridList>

  </React.Fragment>
)}}

module.exports = profile;
like image 34
Hemadri Dasari Avatar answered Sep 25 '22 00:09

Hemadri Dasari