Arrange elements with Material-UI's Grid system

Check out my codesandbox example (here) showcasing how I have set up my current app with Material-UI grid. I have 5 components that need to be positioned in a specific way for different screen sizes.

For screens sized lg and above, I want the components to look like this: https://i.sstatic.net/5fCKe.png

On screens sized md and below, I'd like the components to appear as shown here: https://i.sstatic.net/1U9SS.png

I've tried various approaches but haven't quite achieved the desired layout yet. Specifically, I'm struggling to place component 5 on the same row for larger screens and on the next row for smaller screens.

Any help or suggestions you can provide would be greatly appreciated. Thank you!

Answer №1

If you're looking to implement responsive design using Material UI, consider utilizing the breakpoints hook provided in their documentation material-ui-breakpoints

To see an example in action, check out this demo app I created specifically tailored to demonstrate how the breakpoints work for various screen sizes link-to-demo

Below is a snippet of sample code that showcases how to structure layouts based on different screen widths:

import React from "react";
import withWidth, { isWidthDown, isWidthUp } from "@material-ui/core/withWidth";
import { Grid } from "@material-ui/core";
import { makeStyles } from "@material-ui/core/styles";

const useStyles = makeStyles((theme) => ({
  // Define styles for layout items here
}));

const App = (props) => {
  const classes = useStyles();

  if (isWidthUp("lg", props.width)) {
    return layout1(classes);
  } else if (isWidthDown("md", props.width)) {
    return layout2(classes);
  }

  return <React.Fragment />;
};

// Function defining layout for larger screens
const layout1 = (classes) => {
  return (
    <Grid container>
      {/* Insert Grid items here based on specified breakpoints */}
    </Grid>
  );
};

// Function defining layout for smaller screens
const layout2 = (classes) => {
  return (
    <Grid container>
      {/* Insert Grid items here based on specified breakpoints */}
    </Grid>
  );
};

export default withWidth()(App);

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

Alpha Screen and Shading Filter

Having trouble with CSS filters in IE8. I have a div with a gradient background that needs to have opacity set to 0, and when hovered over, the opacity should change to 1. Here's my code: #myDiv { filter: alpha(opacity=0); opacity: 0; background:rgba ...

ReactJS application encounters difficulty retrieving information from CSV file

In order to demonstrate the issue, I decided to create a new App using the create-react-app tool (available at https://github.com/facebook/create-react-app) My goal is to import data from CSV files into the app. Currently, I am utilizing the CSV method pr ...

Create a fresh instance from an existing object in JavaScript

Is there a way to extract just the name and family values from my object and create a new object called newObj? I tried the following code without success: const Symbol = { city: 'canada', work: 'developer', id: 13276298846607, ...

Tips for utilizing two renderCell functions in a datagrid from Material UI without encountering duplication or repetition

Utilizing Material UI, I have designed a table to showcase my data. In this setup, I am required to use renderCell for two specific properties: 'level by user' and 'level by referent'. Issue: The problem arises when Material UI displa ...

Is it feasible to view images in various formats?

I have provided the code snippet below: <CardMedia className={classes.cardMedia} image={`/items/${item.ID}.svg`} style={{ width: "50%", height: "50%", paddingTop: "20%", }} component="img" /& ...

The issue with updating the Boolean value in the MERN stack is attributed to the absence of quotation marks in the code

I've been working on a logging web application using the MERN stack and everything seems to be functioning properly, except for one issue: When attempting to update a log entry, all fields (Message, Technician, Date) are successfully updated, except f ...

Error message authentication error: "Access token validation failed due to invalid audience"

After struggling with an error for the past two days, I finally found a solution. Despite searching extensively, I could not find a single answer that resolved my issue, but rather multiple answers that collectively led me to the solution. So, let me expla ...

Tweaking the placement of the dropdown menu in the subnavigation area

Just getting back into coding after a long hiatus - I've dabbled in the past but it's been about 6 years since I've worked with any code. Currently, I'm working on replicating a website for practice. I have created a nav bar and a drop ...

Increase the size of a button using CSS styling

Looking for guidance on extending the length of a button in HTML code? Take a look at this snippet: <span class="xp-b-submit xp-b-submit-btn xp-b-right"> <a href="#" class="xp-t-bold" id="PostSubmitLink">Post Search</a> </span> ...

What steps should I take to ensure that the content within a div also goes full screen when I expand the div?

Is there a way to make a flash game go full screen along with its container div? I have a button that expands the div to full screen, but the flash game inside remains stuck in the top left corner of the screen. How can I ensure that the flash game expands ...

Bone structure template - optimizing list spacing with CSS

Currently, I am attempting to float my navigation bar further towards the left by at least 200px, closer to the end of the line visible below. However, every time I add a margin or padding, it causes the elements within the navigation bar to stack on top o ...

Creating personalized components - a step-by-step guide

I'm looking to customize the appearance of the snackbar background, buttons, and other elements in my Material UI project. As a newcomer to Material UI, I'm unsure if I'm taking the correct approach with this code snippet: const styles = { ...

What is the reason border-box does not function properly on inline-block elements?

Demo: http://jsfiddle.net/L5jchnpm/ I've encountered an issue where I believed that using display: inline-block would make elements act as both inline and block elements. Additionally, I thought that setting box-sizing: border-box would adjust the w ...

Next.js - Uncaught ReferenceError: Cannot access 'window' before initialization

Exploring device orientation in coding This is the code I am currently using: import React, { useState, useEffect } from 'react'; export default function App() { const [isLandscape, setIsLandscape] = useState(window.innerWidth > window.i ...

The nested table is overflowing beyond the boundaries of its outer parent table

I have created a nested table, shown below: <table border="0" width="750" cellspacing="0" cellpadding="0"> <tr align="center"> <td width="530"> <table border="0" cellspacing="0" cellpadding="0" width="530"> <tr&g ...

Minor Chrome compatibility problems with CSS alignment

As someone who is new to stackoverflow, I've always found it to be a valuable resource for answers. I've had success building HTML 5 banner ads using GSAP (Greensock Animation Platform) in the past, but now I'm facing a CSS alignment issue t ...

Is there a way to manually refresh the cache for tagged data access in Next app router without utilizing the fetch method?

Looking to add a revalidation endpoint in a Next project using the app router, I am considering using options like revalidatePath and revalidateTag. The data access that needs revalidating involves a 3rd party library making requests to a CMS without dire ...

Synchronization of data on mobile devices without the need

My current project involves developing a React Native application where the server side is up and running. To enable offline synchronization, I am considering integrating realm on the mobile device for handling offline data and syncing with the server wh ...

Combining Three.js with iFrame for a mix of WebGL and CSS3D

After some experimentation, I successfully created a dynamic page that seamlessly integrates WebGL and CSS3D (with valuable guidance from this helpful SO post). To add an extra touch, I included an iframe: However, I noticed that the functionality to inte ...

Having trouble with the ::after element in my React component for my latest React 3D portfolio project

For my portfolio project, I am following a tutorial by LamaDev (https://www.youtube.com/watch?v=qALsVa-V9qo&t=2334s&ab_channel=LamaDev). At around timestamp 36:40, he creates a ::after selector which is not working for me, even though the rest of t ...