How can I make my Grid items in React and Material-UI occupy the entire horizontal space without wrapping to the next row?

I am currently utilizing Material-UI within a React 16.10 project. My goal is to create a table where the left column consists of an icon, while the right column displays a label and address stacked on top of each other. I want the items in the right column to occupy 100% of the available space. To achieve this, I have defined the following class:

fullWidth: {
    backgroundColor: "red",
    width: "100%",
},

(the red background color was added for clarity). I then proceeded to create the table like so...


          <Grid container direction="row" alignItems="top" spacing={1} className={classes.fullWidth}>
            <Grid item>
              <LocationOnIcon />
            </Grid>
            <Grid item>
              <Grid container direction="column" alignItems="center">
                <Grid item className={classes.fullWidth} style={{backgroundColor: "yellow"}}>
                  <TextField
                    className={`${classes.rootInput} ${classes.input}`}
                    id="pickupLocationLabel"
                    value={values.pickUpLocationLabel}
                    placeholder="Label"
                    variant="outlined"
                    disabled={false}
                    onChange={handleChangePickUpLocationLabel}
                    fullWidth
                  />
                </Grid>
                <Grid item className={classes.fullWidth}>
                  <AddressInput
                    className={classes.textField}
                    placeholder={values.pickUpLocation?.address}
                    stage={values.pickUpLocation}
                    setStage={handleChangeLocation.bind(null, "pickUpLocation")}
                    setLocation={handleChangeLocation}
                  />
                </Grid>
              </Grid>
            </Grid>
          </Grid>

However, the elements did not fully expand to 100% of the available width...

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

To rectify this, I assigned the fullWidth class to the parent container:


          <Grid container direction="row" alignItems="top" spacing={1} className={classes.fullWidth}>
            <Grid item>
              <LocationOnIcon />
            </Grid>
            <Grid item className={classes.fullWidth}>
              <Grid container direction="column" alignItems="center">
                <Grid item className={classes.fullWidth} style={{backgroundColor: "yellow"}}>
                  <TextField
                    className={`${classes.rootInput} ${classes.input}`}
                    id="pickupLocationLabel"
                    value={values.pickUpLocationLabel}
                    placeholder="Label"
                    variant="outlined"
                    disabled={false}
                    onChange={handleChangePickUpLocationLabel}
                    fullWidth
                  />
                </Grid>
                <Grid item className={classes.fullWidth}>
                  <AddressInput
                    className={classes.textField}
                    placeholder={values.pickUpLocation?.address}
                    stage={values.pickUpLocation}
                    setStage={handleChangeLocation.bind(null, "pickUpLocation")}
                    setLocation={handleChangeLocation}
                  />
                </Grid>
              </Grid>
            </Grid>
          </Grid>

After adding the fullWidth class, however, the alignment with the icon was disrupted.

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

Please advise on how I can adjust the layout so that the elements take up 100% of the available width without wrapping to a new row?

Answer №1

Why are you utilizing the classes.fullWidth for controlling the size of the Grid items? Material-UI provides a Bootstrap inspired system that allows for sizing items in the Grid based on the screen size. Take a look at the Grid API documentation for more information.

Consider replacing classes.fullWidth with xs=12 to see if the element fills the entire width as intended.

You can also utilize the wrap/nowrap prop in the Grid item to control wrapping behavior.

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

Tips for addressing Navbar collision with body content or other pages using CSS

I have successfully created a navigation bar using CSS and JS. The image below illustrates the example of my navigation bar: https://i.sstatic.net/2l4gj.png The issue I am facing is that when I select "MY ACCOUNT," it displays some content. However, upon ...

Measuring the space between components

I am looking for a way to dynamically calculate distance on a canvas, similar to the example shown in the figure. I want to be able to drag the highlighted joints in order to adjust the span for calculating distances. Are there any plugins available for th ...

Transform your current website layout from a fixed 960-grid system with 12 columns into a fluid and

As a newcomer to web development, I have successfully created my portfolio website. I initially set the body width to 1600px because that matched the size of my banner background in Photoshop. I'm using a 960gs 12-column grid system, but when I view t ...

Making Material-UI Autocomplete a mandatory field: a guide

Currently working on a form that involves Material UI autocomplete with multiple selections. I have made the field required, but upon submitting the form with selected values, it keeps showing an error message that the field is required. This issue seems t ...

Facing difficulty transferring an array from React to Django

Trying to transfer an array from the React frontend (stored in local storage) to my view class in Django is resulting in the following error: Console Output: GET http://127.0.0.1:8000/api/quiz/multiple/ 500 (Internal Server Error) Django Logs: for qu ...

Trouble with Bootstrap 3's nav-justified feature not displaying correctly upon window expansion

Looking at this Bootstrap example page, I noticed a small issue with the nav-justified navigation. When the window is minimized, it transitions correctly to a mobile version. However, when the window is maximized again, the buttons remain in the mobile for ...

Functionality of the button disabled in Firefox, despite working perfectly in Chrome

I have been developing a ReactJS application that is now live. Take a look at the deployed version to understand the issue I am facing. In Firefox, the Login button in the Inventory Login section doesn't seem to be working as expected. Despite having ...

Utilizing Reactjs and Php to Send and Retrieve Form Data

I am currently working with Reactjs (Nextjs) and PHP. I am trying to send form data using Axios, but I am facing an issue where I am unable to retrieve any parameter on the API side. How can I resolve this problem? Below is my current code: const handleSu ...

Gulp fails to generate a CSS file after compiling Sass to CSS

Recently, I've been having trouble compiling my sass file to css. Even though it was working fine before and I haven't made any changes, now my CSS file is empty. Below is the task I am trying to accomplish: gulp.task('sass', function ...

Ways to show buttons as inline or block elements when a single button is enclosed within a form

Need help figuring out how to make two buttons display as block elements on small screens and inline elements on larger screens, especially when one button is wrapped inside a form? I am struggling with displaying two buttons on my page. One of the button ...

Issues with local storage accessing a state that is not defined

TL;DR: My state updates correctly in Redux DevTools but does not persist in Local Storage, showing as "undefined" (screenshots provided). Explanation: I am a beginner with Redux and trying to save my state to Local Storage. While the state updat ...

What is the process for incorporating a new task into my HTML/CSS/JavaScript to-do list application when the Enter key is pressed?

Currently, I am working on developing a to-do list application using HTML, CSS, and JavaScript. The application includes a text input field for users to enter their tasks, along with an "Add Task" button. However, I want to enhance the user experience by a ...

React Checkbox Sum Component - Effortlessly Improve User Experience

Looking for assistance with passing checkbox values to a React component in my app. I'm currently implementing a food list display using JSON data, but I'm unsure how to transfer the checkbox values to another component. For reference, here&apos ...

Calculate the percentage of a specific side for the border-radius using a single radius

I am looking to adjust the border-radius of a div based on its dimensions without creating an elliptical or pill shape effect. For instance, if a div has a width and height of 250px and 35px respectively, the border-radius should be set to 7px. Similarly, ...

New feature in Bootstrap 4 beta allows carousel arrows to be positioned outside of the

I created a text-based carousel using Bootstrap 4 beta. Is it possible to move the arrows off of the slider area and position them outside of it? I have tried searching on Google and in other forums, but I couldn't find a solution. The issue is that s ...

Guide to developing a React package incorporating customized environment variables

I am currently in the process of developing an NPM package with React. This package is designed to make API queries and display the responses on a web page. Since I anticipate that multiple applications will be utilizing this package, each requiring a dif ...

Is there a way to use a Google Chrome command line switch to simulate the dimensions of a

Seeking information on the available Google Chrome command line switches for emulating device sizes. Specifically, I need to test a component that utilizes CSS @media queries for min-device-width/min-device-height. Previous attempts with --window-size an ...

Converting canvas illustrations into HTML files

I am in the process of creating a drawing application that will be able to export into HTML/CSS/JavaScript. I plan to use this tutorial as a foundation for the drawing functionality. My goal is to take all the rectangles within the this.shapes = [] array a ...

Can client-side rendering and session authentication work together?

After careful consideration, it appears that implementing a client-side rendered single-page-application with secure authentication using session information via cookies presents significant security risks. While I initially planned on developing a React a ...

What are the best practices for utilizing an array of routes?

I'm new to working with react but I noticed something strange. My routes are currently set up like this: <Main> <Route exact path="/home" component={Home} /> <Route exact path="/home1" com ...