What's the best way to display two products on a mobile screen?

I'm having trouble figuring out how to make this part look right. I've tried using flexWrap, but the items are stacking vertically instead of aligning horizontally.

   <Box sx={{display: {xs: 'flex'}, 
                     gap: {xs: '0px', md:'20px', 
                     flexWrap: 'wrap', 
                     alignItems: 'center' }}}
                     >
      {products.map((product) => (
        <CardProduct key={product._id}  product={product}/>
      ))}
   </Box>

See the desired outcome here

I want the first and second products to align and have some spacing between them, with the third product wrapping automatically. I've tried setting flexWrap:'wrap' but it didn't work. Should I adjust the height or is it because of the button size?

Click here to see what I'm aiming for

This is how it looks when there are only two products:

View the images here

Answer №1

Consider utilizing the Grid feature:

<Grid container spacing={2}>
  {items.map((item) => (
    <Grid item xs={6} key={item._id}>
      <CardItem item={item} />
    </Grid>
  ))}
</Grid>;

Answer №2

Creating a layout with 2 columns can be achieved using the flex-basis property. By setting flex-basis: 50%; and ensuring to remove any padding or margin, you can easily define the width of each column and determine the number of columns.

<Box sx={{display: {xs: 'flex'}, 
     gap: {xs: '0px', md:'20px', 
     flex-basis: '50%',
     alignItems: 'center' }}}
   >
  {products.map((product) => (
    <CardProduct key={product._id}  product={product}/>
  ))}

Another CSS property that allows for creating multiple columns without relying on flexbox is column-count:

.div {
  column-count: 2;
}

Answer №3

If you're looking to implement a grid layout, Grid from Material-UI is a great option. They provide an excellent example that perfectly fits your requirements.

Check out the link here

<Grid container spacing={2}>
  <Grid item xs={12}>
    <Grid container justifyContent="center" spacing={spacing}>
      {[0, 1, 2].map((value) => (
        <Grid key={value} item>
          <Paper className={classes.paper} />
        </Grid>
      ))}
    </Grid>
  </Grid>

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

The alignment of flexbox within a list item is not positioned at the top as expected

When I place a flexbox inside a list item, the content is being pushed down by what seems to be a full line height. I have tried various CSS properties like setting margin-top: 0 for the flexbox, margin-top: 0 for its children, adding flex-wrap to the fle ...

The footer is not being properly pushed down by the DIV

I created a page and utilized toggle with jQuery. The layout of my page is great, but when clicking on the button to reveal content, the div remains fixed and my content expands and hides behind the footer. Take a look at the image here: https://i.stack.i ...

Guide to utilizing jQuery for random toggling of classes

My objective is to create a mouseover effect on a box that changes its color randomly. Currently, I have managed to achieve this by toggling one class (e.g. $("#evtTarget").toggleClass(highlighted-2);). However, I am now attempting to select a random class ...

Issue with consistent search autocomplete feature in a stationary navigation bar using bootstrap technology

My issue is with the autocomplete box - I want it to have the same width as the entire search box. Something like this using Bootstrap's col-xs-11 class: https://i.sstatic.net/npzhC.png If I set the position to "relative," it looks like this (all st ...

Navigating using passing data in ReactJs

The following data contains information about people: const people = [ { img: 11, name: "Ahmed", job: "developer", }, { img: 13, name: "Kazim", job: "Engineer", }, ...

How can images be effectively stored while utilizing MongoDB, Express, React, and Node.js?

Currently, I am developing a project that includes a Shoe model. At present, I am only displaying the shoe image using an imgUrl, but my goal is to store images. While I am aware of the option to use MongoDB and GridFS for this purpose, there are concern ...

Creating a webpage with a layout that features three full-length columns using

body { background-color: blueviolet; } .leftcolumn { width: 30%; height: 100%; float: left; background-color: brown; } .middlecolumn { float: left; background-color: yellowgreen; width: 60%; height: 100%; } <body> <div class= ...

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 ...

Encountering issues with setting state on Login as Redux state and useSelector are returning undefined

As I am working on JWT decoding for user authentication in my app, I face an issue with dispatching the roles to the Redux store and getting a undefined result when trying to log them elsewhere: The decoded roles in the JWT token are as follows: roles: [ ...

When attempting to run the command npx create-react-app myApp, an error occurred with the message "_npx10796" indicating that a package.json file

Having trouble setting up my React environment. When I attempt to execute the command npx create-react-app myApp I encounter this error message: 1) npm ERR! code ENOLOCAL 2) npm ERR! Could not install from "Guillen\AppData\Roaming\npm-cac ...

Error: null was supposed to match XXX exactly, but does not

I have been using vitest for testing my components and hooks. Recently, I encountered an issue with the useLocalTime hook that I am trying to test. When using the renderHook function from @testing-library/react, it always returns null. Any thoughts on why ...

Removing the animation from a Material-UI Select component in React

Currently, I am utilizing the React Material UI's Select Component and I am looking to either remove or quicken the animation that occurs when opening the menu. I attempted the following approach: <Select ... TransitionComponent={({child ...

How can you trigger two separate JavaScript hover effects?

I have a function in my script that creates a pop-up div when a specific trigger id is hovered over. This works perfectly fine for the first trigger, however, when I try to add another function to create a pop-up for a different div with a second trigger i ...

The resurgence of React JS in its role as a powerful tool

Although I struggle with React JS, I couldn't find a solution for this particular issue. I'm trying to incorporate this function into my tsx file and display it on the screen within a component. function displayUsers() { const UserListComponent ...

Error: Unable to use map on products as it is not a function

As a newcomer to React, I encountered the frustrating error stating that .map is not a function. Despite searching extensively for solutions, none of them seemed to work for me. My specific issue arises when the home screen loads correctly initially, but t ...

Tips on showcasing lengthy string content from JSON in a structured list format using react with typescript

Is there a way to display long string data from a JSON file in a list format when the description string is too lengthy? How can I split or format the description using React and TypeScript? The JSON data I have is as follows: "options": [ ...

Transfer multiple files by dragging and dropping them into a single file upload option

I was experimenting with adding a drag and drop feature to my website. After trying several scripts, I came across one on CodePen. However, the script allows for the upload of multiple files, while I only need to upload a single PDF or .doc file. Can someo ...

Creating a button component in React that spans the entire width

I have integrated a logging component in React and need all buttons to be the same width across the available space. You can view my implementation here. Below is the code snippet: export default function Login (props: any) { return( <Box ...

Enable scrolling exclusively for the content within a tab, without affecting the tab label in Clarity Tabs

I have a tab with lengthy content in my project (check out the StackBlitz reference here). This causes the appearance of a scroll. https://i.sstatic.net/ZcNKM.png The corresponding code snippet is provided below: <div class="content-container&qu ...

Centering an Image of Unspecified Dimensions Both Vertically and Horizontally with Hover Effect on Anchor

To achieve both horizontal and vertical centering of an image with unknown dimensions, I need to use max-width and max-height to constrain it within a container size of 101x84. Additionally, I want the image to display an icon in the middle when hovered ov ...