The MUI Grid design features information displayed on the left side, accompanied by an image placed directly to the right

In the React MUI V5 sample code below, I am creating a profile page layout with details of a person displayed on the left side of the page in label/value format.

My question is how to position an "IMAGE" entry (assume it's a 300px x 300px profile image) to the right of the values, aligned with the label/values rows but not at the far right, just nicely placed to the right of the information.

I want the rows on the left to remain gap-free and have the image only affect the layout on the right side.

Any advice or guidance would be greatly appreciated.

import * as React from 'react';
import { styled } from '@mui/material/styles';
import Box from '@mui/material/Box';
import Paper from '@mui/material/Paper';
import Grid from '@mui/material/Grid';

const Item = styled(Paper)(({ theme }) => ({
  backgroundColor: theme.palette.mode === 'dark' ? '#1A2027' : '#fff',
  ...theme.typography.body2,
  padding: theme.spacing(1),
  textAlign: 'center',
  color: theme.palette.text.secondary,
}));

export default function NestedGrid() {
  return (
    <Box sx={{ flexGrow: 1 }}>
      <Grid container spacing={1}>
        <Grid item xs={2}>
          <Box>Username:</Box>
        </Grid>
        <Grid item xs={6}>
          <Box>Qwerty</Box>
        </Grid>
        <Grid item xs={4}></Grid>

        {/* First Name */}
        <Grid item xs={2}>
          <Box>First Name:</Box>
        </Grid>
        <Grid item xs={6}>
          <Box>Bob</Box>
        </Grid>
        <Grid item xs={4}></Grid>

        {/* Surname */}
        <Grid item xs={2}>
          <Box>Surname:</Box>
        </Grid>
        <Grid item xs={6}>
          <Box>Hope</Box>
        </Grid>
        <Grid item xs={4}></Grid>

        {/* DOB */}
        <Grid item xs={2}>
          <Box>DOB:</Box>
        </Grid>
        <Grid item xs={6}>
          <Box>01/01/2022</Box>
        </Grid>
        <Grid item xs={4}></Grid>

        {/* Occupation */}
        <Grid item xs={2}>
          <Box>Occupation:</Box>
        </Grid>
        <Grid item xs={6}>
          <Box>IT</Box>
        </Grid>
        <Grid item xs={4}></Grid>

        {/* Status */}
        <Grid item xs={2}>
          <Box>Status:</Box>
        </Grid>
        <Grid item xs={6}>
          <Box>Married</Box>
        </Grid>
        <Grid item xs={4}></Grid>

        {/* Image */}
        <Grid item xs={2}></Grid>
        <Grid
          item
          xs={6}
          style={{ display: 'inline-flex', alignItems: 'center' }}
        >
          <Box>IMAGE</Box>
        </Grid>
        <Grid item xs={4}></Grid>
      </Grid>
    </Box>
  );
}

Answer №1

If you're looking to achieve this, you can give the following code a try:

export default function NestedGrid() {
  return (
    <Box sx={{ flexGrow: 1 }}>
      <Grid container spacing={1}>
        <Grid item xs={12} sm={6}>
          <Item>Username: Qwerty</Item>
        </Grid>
        <Grid item xs={12} sm={6}>
          <Item style={{ display: 'flex', alignItems: 'center', justifyContent: 'center' }}>IMAGE</Item>
        </Grid>

        {/* First Name */}
        <Grid item xs={12} sm={6}>
          <Item>First Name: Bob</Item>
        </Grid>

        {/* Surname */}
        <Grid item xs={12} sm={6}>
          <Item>Surname: Hope</Item>
        </Grid>

        {/* DOB */}
        <Grid item xs={12} sm={6}>
          <Item>DOB: 01/01/2022</Item>
        </Grid>

        {/* Occupation */}
        <Grid item xs={12} sm={6}>
          <Item>Occupation: IT</Item>
        </Grid>

        {/* Status */}
        <Grid item xs={12} sm={6}>
          <Item>Status: Married</Item>
        </Grid>
      </Grid>
    </Box>
  );

If this solution doesn't meet your needs perfectly, feel free to adjust it accordingly.

I hope this helps you achieve what you're looking for!

Answer №2

import Box from "@mui/material/Box";
import Grid from "@mui/material/Grid";
import Logo from "../logo.svg";

export default function NestedGrid() {
  return (
    <Box display="flex">
      <Box>
        <Grid container spacing={1}>
          <Grid item xs={2}>
            <Box>Username:</Box>
          </Grid>
          <Grid item xs={6}>
            <Box>Qwerty</Box>
          </Grid>
          <Grid item xs={4}></Grid>

          {/* First Name */}
          <Grid item xs={2}>
            <Box>First Name:</Box>
          </Grid>
          <Grid item xs={6}>
            <Box>Bob</Box>
          </Grid>
          <Grid item xs={4}></Grid>

          {/* Surname */}
          <Grid item xs={2}>
            <Box>Surname:</Box>
          </Grid>
          <Grid item xs={6}>
            <Box>Hope</Box>
          </Grid>
          <Grid item xs={4}></Grid>

          {/* DOB */}
          <Grid item xs={2}>
            <Box>DOB:</Box>
          </Grid>
          <Grid item xs={6}>
            <Box>01/01/2022</Box>
          </Grid>
          <Grid item xs={4}></Grid>

          {/* Occupation */}
          <Grid item xs={2}>
            <Box>Occupation:</Box>
          </Grid>
          <Grid item xs={6}>
            <Box>IT</Box>
          </Grid>
          <Grid item xs={4}></Grid>

          {/* Status */}
          <Grid item xs={2}>
            <Box>Status:</Box>
          </Grid>
          <Grid item xs={6}>
            <Box>Married</Box>
          </Grid>
          <Grid item xs={4}></Grid>

          {/* Image */}
          <Grid item xs={2}></Grid>
          <Grid
            item
            xs={6}
            style={{ display: "inline-flex", alignItems: "center" }}
          ></Grid>
          <Grid item xs={4}></Grid>
        </Grid>
      </Box>
      <Box component="img" src={Logo} width={300} height={300} />
    </Box>
  );
}

Enclosed the entire content within a new Box element and applied flex layout styling. The left side displays information while the right side shows an image. The provided code should work without any issues. Let me know if you find it helpful.

Answer №3

import Box from "@mui/material/Box";
import Grid from "@mui/material/Grid";
import Logo from "../logo.svg";

export default function NestedGrid() {
  return (
    <Box display="flex">
      <Box maxWidth={300}>
        <Grid container spacing={1}>
          <Grid item xs={5}>
            <Box>Username:</Box>
          </Grid>
          <Grid item xs={7}>
            <Box>Qwerty</Box>
          </Grid>

          {/* First Name */}
          <Grid item xs={5}>
            <Box>First Name:</Box>
          </Grid>
          <Grid item xs={7}>
            <Box>Bob</Box>
          </Grid>

          {/* Surname */}
          <Grid item xs={5}>
            <Box>Surname:</Box>
          </Grid>
          <Grid item xs={7}>
            <Box>Hope</Box>
          </Grid>

          {/* DOB */}
          <Grid item xs={5}>
            <Box>DOB:</Box>
          </Grid>
          <Grid item xs={7}>
            <Box>01/01/2022</Box>
          </Grid>

          {/* Occupation */}
          <Grid item xs={5}>
            <Box>Occupation:</Box>
          </Grid>
          <Grid item xs={7}>
            <Box>IT</Box>
          </Grid>

          {/* Status */}
          <Grid item xs={5}>
            <Box>Status:</Box>
          </Grid>
          <Grid item xs={7}>
            <Box>Married</Box>
          </Grid>

          {/* Image */}
          <Grid item xs={5}></Grid>
          <Grid
            item
            xs={7}
            style={{ display: "inline-flex", alignItems: "center" }}
          ></Grid>
        </Grid>
      </Box>
      <Box component="img" src={Logo} width={300} height={300} />
    </Box>
  );
}

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

Should one consider building a web application by relying on browser AJAX requests a sound practice?

When developing a standard MVC web application, is it acceptable to rely heavily on ajax calls for rendering content? For instance, looking at Facebook as an example, they dynamically load a majority of their content. Should we emulate this strategy? What ...

Unlinked Checkbox in Angular Bootstrap Modal Controller

I am currently utilizing the modal feature from Bootstrap 3's Angular fork, and I am facing an issue with using a checkbox within the modal and retrieving its value in my main controller. The checkbox value is properly bound in the view but not in th ...

Determine whether the elements within an array are present on the webpage. If they are, display an alert. If not, reload the page

Initially, I had a userscript designed to search for a specific string within a webpage. If the script failed to locate the string, it would refresh the page after 4 seconds: var item = 'apple'; if(document.body.innerHTML.toString().indexOf(item ...

Trouble with overriding Material UI themes

I'm currently working on customizing the Material UI React theme, but for some reason, my overrides are not being applied. The default colors are still showing up when I view my code. I've made sure to install the necessary libraries by adding " ...

utilizing ajax to submit data with checkbox option

<html> <body> <input type="checkbox" checked="checked"> </body> </html> I am looking for a solution to pass the value of a checkbox as either 1 or 0 depending on its selection status. When the checkbox is checked, I want to s ...

Error: Unable to retrieve data due to null value (property 'detail' is undefined)

When I use useEffect() function to set the document title to 'View Orders | Bothub' and fetch data from a backend URL, I encounter an error. The error message reads: Unhandled Rejection (TypeError): Cannot read properties of null (reading 'd ...

footer extending beyond the previous div's boundaries

Struggling with frontend development and creating a layout in html, css3, and bootstrap 4? I've managed to incorporate a subtle animation in the background (https://codepen.io/mohaiman/pen/MQqMyo) but now facing issues with overlap when adding a foote ...

What is the best way to apply styles based on specific screen widths while still having default styles for other screen sizes?

Here is the code snippet I am working with: <TableCell sx={{ borderBottom: { xs: 0, lg: 1 } }}> <Typography variant="body2">{account.name} ({account.currency})</Typography> </TableCell> I am facing an issue where the ...

Using Webpack 4 to import SCSS files with aliases

Currently, I am running a node script that is using webpack to bundle multiple folders for various installations on our server. MiniCssExtractPlugin.loader, { loader: 'css-loader', options: { ...

enigmatic margin surrounding navigation dots

I have added a straightforward dot navigation to the banner of my website. You can check it out HERE. The HTML code I used is shown below: <ul class="carousel-dots"> <li> <a href=""></a> </li> <li> ...

Utilizing AngularJS with Restheart API to streamline MongoDB integration and efficiently parse JSON data outputs

Utilizing RestHeart to expose CRUD functionality from MongoBD. Attempting to call the Rest API from AngularJS and retrieve the JSON output like the one displayed below. My focus is specifically on extracting the name, age, & city fields that are stored in ...

The attempt to install myapp using npx create-react-app has failed. The installation has been aborted

Attempting to create a new project using npx create-react-app my-app resulted in an error message saying Aborting installation. https://i.sstatic.net/IhpQJ.jpg Initially, I had node v14.17.1 installed. Upgrading to the latest version 16.4.0 did not resol ...

Guide on inserting a new user to the db.json

My database file, db.json, has the following structure: { "users": [ { "id": 0, "isActive": true, "balance": "$2,309.20", "picture": "http://placehold.it/128x128", "age": 26, "accessLevel": "guest", "firstNa ...

The syntax for jQuery

While delving into the world of jQuery, I stumbled upon a code snippet that caught my attention. Although I am well versed in jQuery's basic selector syntax $('element'), I must admit that the $. syntax perplexes me. Take for instance the fo ...

Getting the value of a JavaScript variable and storing it in a Python variable within a Python-CGI script

Is there a way to capture the value of a JavaScript variable and store it in a Python variable? I have a Python-CGI script that generates a selection box where the user can choose an option from a list. I want to then take this selected value and save it ...

What is the best way to transfer information from an app.js file to an index.ejs file within Routes?

I'm encountering an error where the variable 'blogs' that I am passing as an object containing data to the index page is not defined. Here is the code for the index page: Any help or guidance on how to resolve this issue would be greatly ap ...

Ensuring consistency of Angular route data and maintaining data synchronization

In my Angular application, I have a table that displays a set of items and allows inline editing directly within the table. The data is fetched from an HTTP API through a service, which retrieves the data and injects it into the application. The issue ari ...

Using the mapState function in vuex allows for easy access to Vue methods

I have an important task to complete while the data is being computed using vuex mapState. I must ensure that the vue method countAlerts is called every time the data changes; however, the computed property needs to invoke this method and unfortunately, ...

Adding a box shadow around the entire div in Internet Explorer 11 with CSS

I am having difficulty applying box-shadow to create a shadow around the entire perimeter of a div. While it works in FireFox, it does not work in IE 11. So far, I have attempted solutions from this StackOverflow thread. For reference, here is a JSFiddle ...

Eliminating the necessity of pathing in the CloudFront distribution of an S3 bucket, without the need for .html at the end of the page name, within a Next.js

I currently have a project built with Next.js, React, and TypeScript that is stored on an S3 bucket as a static site and distributed through CloudFront. One issue I am facing is the need to add .html at the end of page names to navigate to a different pag ...