Utilizing MUI for layering components vertically: A step-by-step guide

I am looking for a way to style a div differently on Desktop and Mobile devices:

------------------------------------------------------------------
|  (icon)   |           (content)                  |(button here)|
------------------------------------------------------------------

On mobile, I want it to look like this:

--------------------------
|(icon)|    (content)    |
--------------------------
|      (button here)     |
--------------------------

I have experimented with using Card and CardHeader components, but they do not provide the desired outcome on mobile:

<Card sx={{ width: "90%", marginBottom: 2 }}>
    <CardHeader
        avatar={<img src={icon} style={{ maxWidth: "100%" }} />}
        action={
          <Button
            onClick={onClick}
            variant="contained"
            sx={{
              width: "173px",
              height: "48px",
              letterSpacing: 1.4,
            }}
          >
            {buttonText}
          </Button>
        }
        title={children}
        subheader={subContent}
      />
    </Card>

I am open to alternatives to using Card component. I have considered Grid but unsure about its stackable capabilities. Any suggestions would be appreciated. Thank you.

Answer №1

Based on the diagram you provided, it seems like Grid is the perfect solution for your needs, as it is designed specifically for creating responsive layouts. By using the xs, sm, md, lg, and xl props, you can specify how much space each item should occupy in a 12 column layout at different breakpoints. For instance, in your scenario:

<Grid container>
  <Grid item xs={4} sm={2}>
    (icon)
  </Grid>
  <Grid item xs={8} sm={8}>
    (content)
  </Grid>
  <Grid item xs={12} sm={2}>
    (button here)
  </Grid>
</Grid>

sm+:

https://i.stack.imgur.com/8J7fK.png

xs:

https://i.stack.imgur.com/9ePys.png

Here is a live example tailored to your requirements: https://codesandbox.io/s/basicgrid-material-demo-forked-q795q?file=/demo.js:0-725

Answer №2

The MUI Stack component is a versatile tool for stacking elements on a webpage. [1]: https://mui.com/material-ui/react-stack/ I've experimented with it a bit below, but be sure to review the documentation as it could save you time and effort.

import { Stack } from '@mui/material'
 
{ isMobile ? <Stack justifyItems="center" sx={{ width: `100%` }}>
    <Stack direction="row" spacing={12} alignItems="center" sx={{ margin: `auto` }}>
        <p>Icon</p>
        <p>Content</p>
     </Stack>
     <Stack sx={{ margin: `auto auto` }}>
        <button>Button here</button>
     </Stack>
</Stack> : <Stack direction="row" spacing={12} alignItems="center">
        <p>Icon</p>
        <p>Content</p>
        <button>Button here</button>
</Stack> }

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

What properties are missing from Three.js Object3D - isMesh, Material, and Geometry?

Currently, I am working with three.js version r97 and Angular 7. While I can successfully run and serve the application locally, I encounter type errors when attempting to build for production. Error TS2339: Property 'isMesh' does not exist on ...

W3C validation issue: "Failure to immediately follow a start-tag with a null end-tag" and similar errors

Encountering validation errors with the following code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <hea ...

Divide a SINGLE BACKGROUND IMAGE in HTML into two separate links of equal size, one at the top and

As a beginner in HTML, I am trying to find a way to divide a background image into two equal sections without using image mapping. I attempted to split the links by setting the style to 0% and 50% to designate the top and bottom halves, but unfortunately, ...

"Integration error: specified token_name parameters are invalid." FORTPAY INTEGRATION

I have been following the instructions provided by payfort in their email and referring to the Merchant Page 2.0 documentation for integration with nodejs. Despite sending all the necessary parameters in the request body, I encountered an issue where the T ...

A guide to deactivating the Material UI Link API element

Previously, I relied on Material UI's Button component with a disable property that allowed the button to be disabled based on a boolean value. However, I now want to use the Material UI Link component, which resembles a text link but functions like a ...

Update the TemplateUrl according to the URL Parameters (GET)

I've created a basic Angular code snippet (test.component.ts) that retrieves a variable from GET parameters: import { Component, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; @Component({ select ...

Activate the Select List to Appear Only When a Search is Conducted

Currently, I have implemented the react-select API in order to provide language options for selection. <Select isMulti name="langs" options={langOptions} defaultValue={{ value: "English", label: "English", nativeNam ...

Is there a way to make a button on a single div only affect that specific div

I have a PHP query that echoes a div for each row in the table. I want the div to slide up and then, when the user clicks the "read more" button, the div slides down. However, since it is echoed in a loop, all the divs have the same IDs and classes. I wo ...

React Hook Form: Issue with useForm not displaying any errors in formState

Currently in the process of developing an app using T3 stack along with react-hook-form and zodResolver:@hookform/resolvers/zod Below is the zod schema that I have defined: export const AccountSchema = z.object({ id: z.string().uuid().optional(), titl ...

When working in React, I encountered a problem with the for of loop, as it returned an error stating "x is undefined." Although I could easily switch to using a simple for loop, I find the for of loop to

I'm encountering an issue when using a for of loop in React, as it gives me an error stating that "x is undefined". import { useEffect } from "react"; export default function HomeContent() { useEffect(() => { let content = document ...

Adjust the formatDate function in the Material UI datepicker

I recently implemented the material-ui datepicker component with redux form and it's been working great. However, I have encountered a minor issue. Whenever I change the date, it displays in the input field as yyyy-mm-dd format. I would like it to app ...

Lazy loading in Angular 2 hits a snag when using a module that is shared

After successfully lazy loading my AccountModule, I encountered an issue when adding my SharedModule to it. All of my eagerly loaded modules seemed to be forgotten and I started receiving the following error: The component FoodComponent is not part of a ...

Custom React Hooks Eliciting Actions in All Corners

I developed a custom React hook that takes an image file from a file input, converts it into Base64 format, and returns it as a callback. I used this hook in two components (Avatar and Cover) on the same page. However, when I select an image, both componen ...

Managing form submissions using Material UI and Next.js

Can someone provide insights on using Material UI with Next Js? I am experimenting with a Login template from Material UI, but I am facing an issue where query params are added to the URL upon Submit. For example: localhost:3000/auth/login changes to: ...

How to display only the thumbnail on WordPress archive page and remove the post excerpt

I am currently in the process of revamping my category archive to resemble a grid layout by utilizing custom CSS. I have successfully eliminated the elements I desired using code like this: .archive .entry-footer { display: none; } Now, I only have t ...

The bug in Polymer 1.0 where a custom element is causing issues when clicking under the toolbar

Issues have arisen with a custom element I created, named <little-game></little-game>. Here is the template code for <little-game></little-game>: <template> <a href="{{link}}"> <paper-material elevation=& ...

Are there ways to incorporate extra icons into NavMenu in Blazor 8?

I am exploring ways to incorporate extra icons into the NavMenu.razor component of my Blazor version 8 application. In the earlier version, Blazor 7, there was an iconset setup located in wwwroot/css/, which allowed me to include additional icons simply by ...

The issue of flickering while scrolling occurs when transitioning to a new page in Angular

I have encountered an unusual issue in my Angular 13 + Bootstrap 5 application. When accessing a scrollable page on small screen devices, the scrolling function does not work properly and causes the page to flicker. I observed that this problem does not o ...

Oops! There seems to be an error with the <path> attribute. It looks like we were expecting a number, but received something different: "

I'm currently working on creating a basic line graph using d3.js and integrating it into a React component. However, I'm encountering this error: Error: <path> attribute d: Expected number, "MNaN,36.393574100…" Unfortunately, the similar ...

Some elements are not responding to margin-top properly

I am facing a problem where 2 elements are not responding to the specified margins of margin: 260px 0 0 0; or margin-top: 260px;. Despite inspecting the elements in IE's dev tools and confirming that the margin is set, the elements appear at the top o ...