Guide to creating adaptive content using React and Material UI

For my current project, I am working on designing a product detail page using React and Material UI. The challenge I am facing is ensuring that the page remains responsive across different screen sizes. Specifically, I am struggling with the description section overlapping the image until the screen size is reduced to mobile dimensions. Even when the content gets stacked properly, the images and text are not adjusting as expected.

I suspect that there might be an issue with the styling of the images. How can I make sure that the images are responsive and adapt to varying screen sizes?

Below is a snippet of my code for reference:

styling section

const useStyles = makeStyles((theme) => ({
  content: {
    flexGrow: 1,
    padding: theme.spacing(3),
  },
  toolbar: {
    display: "flex",
    alignItems: "center",
    justifyContent: "flex-end",
    padding: theme.spacing(0, 1),
    // necessary for content to be below app bar
    ...theme.mixins.toolbar,
  },
}));

function

export default function ProductDetails() {
  const classes = useStyles();

  return (
    <main className={classes.content}>
      <div className={classes.toolbar} />

      <Grid container spacing={3}> {/* image and description container */}
        <Grid item xs={12} sm={6}> {/* image container */}
          <Grid> {/* preview image */}
            <img src="luxury-watch.jpg" width="600" id="preview" alt="" />
          </Grid>
          <Grid> {/* sub images */}
            <img src="luxury-watch.jpg" width="140" id="img1" alt="" />
            <img src="luxury-watch.jpg" width="140" id="img2" alt="" />
            <img src="luxury-watch.jpg" width="140" id="img3" alt="" />
            <img src="luxury-watch.jpg" width="140" id="img4" alt="" />
          </Grid>
        </Grid>

        <Grid item xs={12} sm={6}> {/* description container */}
          <div>
            <h1>ROLEX</h1>
            <p> .... </p>
            <h2><span><sup>$</sup></span>1250</h2>
            <Button variant="contained" color="secondary">ADD TO CART</Button>
          </div>
        </Grid>
      </Grid>
    </main>
  );
}

Answer №1

It seems like a few props are missing from your Grid elements. Here's a revised version:

export default function ProductDetails() {
  const classes = useStyles();

  return (
    <main className={classes.content}>
      <div className={classes.toolbar} />

      <Grid container spacing={3}> {/* image and description container */}
        <Grid item container xs={12} sm={6}> {/* image container */}
          <Grid item> {/* preview image */}
            <img src="luxury-watch.jpg" width="600" id="preview" alt="" />
          </Grid>
          <Grid item> {/* sub images */}
            <img src="luxury-watch.jpg" width="140" id="img1" alt="" />
            <img src="luxury-watch.jpg" width="140" id="img2" alt="" />
            <img src="luxury-watch.jpg" width="140" id="img3" alt="" />
            <img src="luxury-watch.jpg" width="140" id="img4" alt="" />
          </Grid>
        </Grid>

        <Grid item container xs={12} sm={6}> {/* description container */}
          <div>
            <h1>ROLEX</h1>
            <p> .... </p>
            <h2><span><sup>$</sup></span>1250</h2>
            <Button variant="contained" color="secondary">ADD TO CART</Button>
          </div>
        </Grid>
      </Grid>
    </main>
  );
}

If the Grid items are not containers, you can't use the xs attribute, and your console should display a warning about it as well

Answer №2

One way to ensure your website is responsive is by using Material UI breakpoints.

const useStyles = makeStyles((theme) => ({
  content: {
    flexGrow: 1,
    padding: theme.spacing(3),
  },
  toolbar: {
    display: "flex",
    alignItems: "center",
    justifyContent: "flex-end",
    padding: theme.spacing(0, 1),
    // Ensures proper positioning below app bar
    ...theme.mixins.toolbar,
    [theme.breakpoints.down('lg')]:{
      width:'60%',
    },
    [theme.breakpoints.up('lg')]:{
      width:'60%',
    }
  }
}));

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 method to erase data from an AutoComplete Box when clicking?

I have incorporated the Material UI AutoComplete component which can be found here. Below is my code snippet: <Autocomplete open={showUniSuggs} onOpen={this.props.getUniversityOptions} onChange={(event, value) => this.props.handleUniversi ...

The use of non-breaking space symbol ( ) does not function properly in Weblogic 14

After upgrading from Weblogic 12 to Weblogic 14 Server, I encountered a change in behavior. Previous Status Everything was functioning properly before the update. When requesting an html page with &nbsp; whitespaces, the server would include these spa ...

Problem with Loading Images in JSP

When trying to display an image on my jsp page using the code < img src="C:/NetBeanProject/images.jpg" alt="abc" width="42" height="42"/>, the image does not appear. Is there something incorrect with this code? ...

Show appreciation for Number Format by giving props to the Material UI TextField

I am looking to implement a custom TextField element for handling numerical fields dynamically. This will help me manage formats such as credit cards and phone numbers. Currently, I am using the react-number-format library similar to the Material-UI exam ...

Steps for creating a rubber compound with reduced elements

Sample Image Looking for a way to reduce the size of elements on a webpage without adapting them? I want the elements to shrink and align in a single column, and I will handle the adaptation myself. HTML: <!-- Trading --> <div class="main-co ...

Automated logout feature will be enabled if no user interaction is detected, prompting a notification dialog box

Here is my working script that I found on this site. After a period of idle time, an alert message will pop up and direct the user to a specific page. However, instead of just the alert message, I would like to implement a dialog box where the user can ch ...

Guide to navigating to a different webpage with Node.js

I am a beginner user of NodeJS and I have a specific request. I need assistance with setting up a page redirect from one page to another upon clicking on a link. Any guidance provided will be greatly appreciated. Here is the code snippet: app.js var expr ...

Storing segments of URL data for future use in React applications

Is there a way to extract information from a URL? I wish to utilize the appended details in this URL http://localhost:3000/transaction?transactionId=72U8ALPE within a fetch API. My goal is to retrieve the value 72U8ALPE and store it either in a state var ...

Is there a way to align both the horizontal and rotated lines to intersect at a common point in HTML and CSS?

As a beginner, I'm looking to create a structure resembling a thigh, leg, and ankle using thick lines. My aim is to rotate them with animation, ensuring that the joints between the lines mimic those of a knee joint and hip joint. Below is the code sn ...

Images are not displaying on mobile for the React-responsive-carousel when using nextJS/strapi

I've been developing an online store using nextJS and Strapi, and I decided to implement the react-responsive-carousel. Following the instructions on [ https://www.npmjs.com/package/react-responsive-carousel ] went smoothly. (I am currently working on ...

What could be causing the unpredictable behavior of my Material UI Tabs component, where it seems to be overriding other

I am facing an issue with the styling of tabs in my React app dashboard that uses Material UI Tabs. Specifically, I have a tab in the dashboard that opens a modal for adding new users to the system. The modal itself also contains tabs, but no matter how I ...

Learn the process of setting up a connection between Express JS and the NotionAPI using both POST and

As someone who is new to backend development, I am currently working on integrating a simple Notion form into a Typescript website. To guide me through the process, I found a helpful tutorial at . The tutorial demonstrates how to send data from localhost:3 ...

How to create list item bullets with a star icon using reactstrap/react?

As a machine learning professional looking to enhance my frontend skills, I am curious about how to create list item bullets using a custom star bullet image. Could anyone please share a complete HTML/CSS example with me? Thank you in advance! ...

Issue with MaterialUI value prop not updating after dynamic rendering of components when value state changes

As I dynamically generate Material UI form components, I encounter an issue with updating their values. The value prop is assigned to a useState values object, and when I update this object and the state, the value in the object changes correctly but the M ...

Hovering over the Star Rating component will cause all previous stars to be filled

I'm in the process of creating a Star Rating component for our website using Angular 11. I've managed to render the 5 stars based on the current rating value, but I'm having trouble getting the hover styling just right. Basically, if I have ...

What could be causing Material-UI's styles not to override?

Incorporating Material-UI into my application has been a smooth process overall. I have successfully overridden the typography and color palettes to fit my needs. However, I am facing an issue with overriding MUIButton. Below is a snippet of my style file: ...

What could be causing my display: table-cell element to not take up the entire available space?

After consideration, I have decided to avoid using a JavaScript solution for my problem. The only way it seems to work consistently is by using setInterval() at regular intervals, which I prefer not to do. I am aware that achieving the same effect with CSS ...

Generate dynamic maps that are exportable to HTML format

I've utilized Leaflet to produce engaging interactive maps in R. However, I'm encountering an issue when trying to export the maps - the background map appears grey after exporting. library(leaflet) library(htmlwidgets) m <- leaflet(data.fra ...

MUI CSS: Mastering image manipulation

My MUI React Component features a Card that contains an image and buttons <Card key={index} className="xl:w-[350px] w-[310px] max-h-[450px]"> <img src={meme.url} className="center bg-cover" alt="" /> <Box cl ...

The animation using Jquery and CSS is experiencing some glitches on Safari when viewed on an

Why is smooth animation not working on iPad Safari with jQuery animate? $('#myId').css({ 'left': '-100%' }).animate({ 'left': '0' }, 300, 'linear'); I also tried using the addClass option and in ...