Displaying grid elements in a single row - Using ReactJS with Material-UI

I am attempting to design a material ui carousel that shows 3 items in one row, even when the browser window is resized. I want the excess items to be hidden instead of being stacked below.

Here is how it looks on full screen:

https://i.stack.imgur.com/ruYPj.png

This is my desired layout:

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

However, this is what I currently have:

https://i.stack.imgur.com/h0gq5.png

Can you help me identify any issues with my code? Here is the snippet:

  <Paper variant="outlined" className={classes.paper}>
    <Grid
      container
      spacing={2}
      className={classes.grid}
      alignItems="center"
      justify="center"
    >
      ...
      <Box display="flex" alignItems="center" className={classes.box}>
        <Grid item xs="auto" className={classes.arrow}>
          ...Left Arrow...
        </Grid>
        <Grid item>
          <Grid
            container
            spacing={2}
            className={classes.grid}
            alignItems="center"
            justify="center"
          >
            <Grid item xs="auto">
              <UserCard content={users[index]} />
            </Grid>
            <Hidden xsDown>
              <Grid item xs="auto">
                <UserCard content={users[index + 1]} />
              </Grid>
            </Hidden>
            <Hidden smDown>
              <Grid item xs="auto">
                <UserCard content={users[index + 2]} />
              </Grid>
            </Hidden>
          </Grid>
        </Grid>
        <Grid item xs="auto" className={classes.arrow}>
          ...Right Arrow...
        </Grid>
      </Box>
    </Grid>
  </Paper>

Styles

const useStyles = makeStyles((theme) => ({
  paper: {
    display: "flex",
    width: "auto",
  },
  grid: {
    width: "auto",
  },
  arrow: {
    padding: theme.spacing(3),
  },
  box: {
    padding: theme.spacing(3),
  },
}));

Answer №1

To make sure your items stay on the same row in your Grid container, you can add the wrap='nowrap' prop, which will override the default behavior of wrapping to the next row.

<Grid
    container
    wrap='nowrap'
    // Your other props
>

Explanation:
Typically, when items don't fit in a container, they wrap onto the next row. However, with the wrap='nowrap' prop, Grid items will remain on the same row.

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

When you try to install @mui/material, it may cause a conflict with

I'm encountering an issue while trying to set up @mui/material npm install @mui/material However, during the installation process, I am getting this error message: npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! ...

Quick guide on prepending text to an IconButton

Is there a way to include custom text before the IconButton in Material UI? Here is the code for the button: <div className={classes.toolbar}> <IconButton onClick={handleDrawerClose}> {theme.direction === 'rtl' ? <Chevron ...

Problem encountered when re-rendering in a Next.js application leveraging Material-ui, React Redux, and Server-Side Rendering (SSR) causing

Hey there, I'm experiencing an issue and could really use some help figuring it out. I've been using Next.js v10+ with Material-UI and React Redux / Redux for server-side rendering. One of the problems I've encountered involves a Redux Prop ...

The symbol `'.'` is giving a error message of not being recognized as an internal or external command, operable program, or batch file when using React

Attempting to initiate a project from gitlab, I encountered an error when running npm start: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="add9dec880cbdfc2c3d9ed9d839c839d">[email protected]</a> start npm run re ...

Unable to retrieve JSON element in ReactJS

Here is the code snippet that I am currently working with: const [questions, setQuestions] = useState([]) useEffect(() => { let idVar = localStorage.getItem('idVarianta'); idVar = JSON.parse(idVar) axios({ ...

When the drawer is opened, there is a strange phenomenon of all buttons being mysteriously clicked

Currently, I am working on a single-page web application utilizing React and Material UI, along with React-Mini-Router for routing. The app features a side drawer that is activated by clicking a hamburger icon located in the top app bar. Each item in the d ...

Despite being used within useEffect with await, asynchronous function fails to wait for results

In my component, I am utilizing a cookie value to determine which component or div block to display. The functionality works correctly in the end, but there is a brief moment where it seems like the cookie value is not being checked yet. During this short ...

The function window.alert() has not been implemented in jest

Recently, I crafted a test for my API using jest. In the test file, I included a function that invokes my API: import AuthManager from "../Client/Modules/Auth/AuthManager"; Then, I utilized it in the following manner: test("login api resolv ...

Undefined React custom hooks can be a tricky issue to troub

Just delving into the world of React and trying to wrap my head around custom hooks, but I keep hitting roadblocks. Here's the latest hurdle that I'm facing, hoping for some guidance. I'm taking a step-by-step approach to creating a functio ...

Implement type declarations for a React JS form validation schema

I encountered the following scenario: interface FORM<P> { onSubmit: (d: P) => void; schema?: yup.SchemaOf<P>; } This is an example of my onSubmit function: const onSubmit = (d: { firstName: string; lastName: string }) => { conso ...

What is the best way to sift through slug data?

Struggling to display related posts from the same category in my project using Sanity for slug data. Attempted fetching all data and storing it in the state but unsure how to filter based on the current post's category. I'm thinking about leverag ...

The proper way to handle redirection in Apollo and React after encountering authentication failure

I'm currently developing a React application that relies on apollo-client and incorporates apollo-link-error to effectively manage authentication errors across the app. Additionally, I am utilizing createBrowserHistory for seamless browser history man ...

Error message: "React Component not identified"

I am following [this React tutorial][1] and facing an issue right from the start as my React components are not being identified. Here is a snippet of my code: import React from 'react'; import {BrowserRouter as Router, Route, Routes} from "react ...

"Implementing responsive design with Material-UI components in React using

It has become quite annoying to constantly see all these warnings related to DOMNesting. No matter what I do, I can't seem to get rid of them completely. Here is a typical example: Warning: validateDOMNesting(...): <table> cannot appear as a des ...

Is it possible to display different components based on language selection using i18n?

I'm currently developing an application using React and incorporating i18next for internationalization. I have a requirement to dynamically change the banner based on the language selected by the user. Can this be achieved within the i18next framework ...

"Ensuring Your SVG Icon is Perfectly Centered: A Step

My SVG icon is currently aligned to the left, but I want to center it. I tried using the following code, but it doesn't seem to be working: .parent{ display: flex; align-items: center; font-size: 13px; } span{ margin-right:10px } When I look at the S ...

Is it possible to intercept tRPC requests and modify the response data on the client side, either before or after the request is sent?

I'm currently working on creating a sample e-commerce website using Next.js 13, utilizing the app directory and tRPC. So far, I've successfully developed the majority of the admin dashboard and established the core functionality of the site incl ...

Is there a way to include e.preventDefault() within an ajax call?

After the user clicks the submit button on my form, the handleSubmit function is triggered. However, I am having trouble calling e.preventDefault() inside my AJAX call due to its asynchronous nature. How can this issue be resolved? class FormRegister ex ...

Issue: React-Firebase-Hooks failing to retrieve dataHaving trouble with React-F

I've been utilizing the React-Firebase-Hooks package in my project, but I'm encountering some difficulties with its usage. In the code snippet below, the user.data().username is displayed correctly. However, when I try to use it within useState, ...

Stylesheet for a React component

Why is the CSS code in my React component file not working even though I have imported it? import React from 'react'; import { Carousel } from 'react-bootstrap'; import './Banner'; ...