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 is the best way to determine the position of a Draggable element in relation to a Droppable element that it is currently hovering over

VIEW IMAGE My Objective: I am working on a project where I need to determine the distance between my draggable element and a droppable target. Specifically, I want to calculate how far away my mouse is from the center, top border, or bottom border of the ...

Creating Custom Type Guards for Literal Types in Typescript: Is It Possible?

Note: I am new to using typescript. Before asking this question, I made sure to go through the documentation on advanced types and type guards. Additionally, I looked into several related questions on Stack Overflow such as user defined type guards [typesc ...

Obtaining the most recent state information within a functional component

I'm encountering an issue with obtaining the updated state data within a functional component. While I've come across threads on SO suggesting switching to a Class component for better state management without hooks, I believe it should be achiev ...

Fixing the problem with Materialize drop-down menus

Here are some key details: The tool I'm currently utilizing: The current outcome displayed: The desired outcome to achieve: I have incorporated a dropdown feature into the navigation bar of my website. Prior to adding the following class: .siden ...

The global variable in TypeScript is not specified; it is initialized within the declaration `declare global{}`

In my TypeScript code, I'm facing a challenge when trying to add a global scope variable. In JavaScript (NodeJS), this process is straightforward: // index.js globalThis.helloWorld = 'Hello World!'; require('./log.js') // log.js c ...

Google Chrome does not support inlined sources when it comes to source maps

Greetings to all who venture across the vast expanse of the internet! I am currently delving into the realm of typescript-code and transcending it into javascript. With the utilization of both --inlineSourceMap and --inlineSources flags, I have observed t ...

Tips for customizing the font size of a FormControlLabel

Can you provide guidance on how to set the in-line font size for a Material-UI FormControlLabel? The code snippet below is not producing the desired result. const styles: any = createStyles({ formControlLabel: { fontSize: '0.6rem', ' ...

When you reach the end of the page, the loadMore function is triggered multiple times

On my webpage, I have a list of profiles that are displayed. When the user reaches the bottom of the page, more data should be loaded through the loadMore function. While the loadMore function itself works correctly, I am facing an issue with the listener. ...

When trying to set the state in the OnMouseOver event, an error is thrown: TypeError - React is

Despite encountering numerous posts discussing this issue, I am struggling to resolve it in my particular scenario. I have a collection of items that I am attempting to apply color changes to (the div's) when they are hovered over. The coloring only ...

What could be causing the discrepancy between the smooth functioning of my web application on localhost (npm run dev) and its poor appearance when deployed on Heroku?

Not a duplicate, but I am going to sleep and leaving my tunneled localhost: here https://i.stack.imgur.com/wfTUJ.png When I run npm run dev from my machine, this is what I see: { "name": "hps_prework", "version": "1.0.0", "description": "", "main": "ind ...

Modify the colors of <a> elements with JavaScript

I am brand new to the world of UI design. I am encountering an issue with a static HTML page. (Please note that we are not utilizing any JavaScript frameworks in my project. Please provide assistance using pure JavaScript code) What I would like to achie ...

My Gatsby website is being rendered in its HTML form on Netlify

The website build is located at . It appears that the javascript functionality is not working, and only the html version (usually meant for search engines) is being displayed. It seems like this issue is only affecting the home page. You can check out the ...

When using Italics, the conflict arises between the specified line-height and the actual height of the

Recently, I encountered an issue that has me a bit perplexed: I have a span element with line-height set to 18px and font-size at 16px. Everything works perfectly when the text inside is regular; the height of the span remains at 18 pixels. The problem a ...

Trouble updating user information in prism database using Next JS

Context: I have recently developed a schema using Prism and my aim is to modify the orgName field in my table through a form. schema.prisma model User { id String @id @default(cuid()) name String? email ...

What purpose does '& .MuiTextField-root' serve within the { makeStyles } function of Material UI?

I've been working on a React JS project using Material-UI and came across some interesting styling in my Form.js file. Here's a snippet of the code: import useStyles from './styles'; const classes = useStyles(); <form autoCapitali ...

The Bootstrap Mobile Navigation transition is not displaying as intended in my CSS coding

On both desktop and mobile screen sizes, I have a white navigation bar. However, for the mobile dropdown menu, I want the background to be grey. I managed to achieve this by targeting .show on smaller screens in the CSS and specifying the grey background ...

React: Struggling to configure proxy servers on a React application

After attempting to add a proxy to the package.json of my own React app, I encountered some issues with it not functioning correctly. Interestingly, the basic create-react-app example worked flawlessly even though it lacked any webpack configuration. Belo ...

In order to effectively manage the outcome of these loaders, it might be necessary to incorporate an extra loader into the equation. Node.js version

./node_modules/html-to-react/node_modules/htmlparser2/lib/esm/index.js 67:9 Module parse failed: Unexpected token (67:9) File was processed with these loaders: * ./node_modules/babel-loader/lib/index.js You may need an additional loader to handle the resu ...

I am completely new to React and I am struggling to grasp the purpose of a React build process

After a few weeks of diving into the world of React, I must say I'm really enjoying it. But why do we need React Build? I recently completed my booking website by creating separate folders for the NodeJS/express back-end and front-end components. Now ...

Guide to generating a dropdown menu and linking it with data received from a server

I am completely new to Angular and recently received a project involving it. My task is to create a nested dropdown list for Json data retrieved from a server using Rest Api calls. The data contains a Level attribute that indicates the hierarchy level of ...