Show equally sized elements using a grid system

I need assistance with displaying fields in a row using CSS grid. I want to display 3 fields in a row with equal width, and if there are only 2 fields, they should also have equal widths. Additionally, if there is only one field, it should take up the full width.

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

Here's the CSS code I tried:

root: {
        width: '100%',
        display: 'grid',
        gridTemplateColumns: 'repeat(3, 1fr)',
        gridColumnGap: theme.spacing(),
        overflow: 'hidden',
    },

However, this is the result I got:

https://i.stack.imgur.com/1CZNO.png

How can I achieve the expected layout shown in the first image using CSS grid?

Answer №1

There are two innovative solutions to this challenge that can be explored. The first solution involves dividing the grid into 6 columns instead of the traditional 3 columns. By utilizing the nth-child selector, grid items can span across multiple columns with precision. The second solution proposes avoiding the use of grids altogether and opting for a flex-container approach. Within this setup, three flex-containers serve as child nodes, with flex-basis playing a crucial role in achieving the desired layout.

Solution 1: Using a 6 column grid, The key lies in identifying specific grid items and leveraging the span property to extend them across a designated number of columns. For instance, the initial three grid items could be configured to span two columns each using grid-column: span 2;. Below is an example:

.grid-container {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: repeat(3, 1fr);
}

.grid-item:nth-child(1),
.grid-item:nth-child(2),
.grid-item:nth-child(3) {
  grid-column: span 2;
}

.grid-item:nth-child(4),
.grid-item:nth-child(5) {
  grid-column: span 3;
}

.grid-item:last-child {
  grid-column: span 6;
}

To view a demo, check out the codesandbox link.

Solution 2:

Given your interest in CSS-grid solutions, I have refrained from presenting the code snippet based on flex-box methodology. However, you might find the codesandbox demonstration showcasing the flex-box solution quite enlightening.

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

Which React Native framework is the superior choice: NativeBase or Shoutem?

When it comes to React Native, I've noticed that there are two UI components that are particularly popular. I'm interested in using one of them - but which one is easier to use and more customizable? ...

Cross-browser compatibility issues with animated SVG line drawing glow effect

Attempting to create a dazzling animation utilizing SVG filters and the stroke-dasharray technique to produce a gradually drawn "glowing" line effect has posed its challenges. After extensive research, a partially functional solution was crafted: JSFiddle ...

Using array.map with Promise.all allows for concurrent execution

Currently, I am working with an array of images and attempting to apply image processing techniques using an asynchronous function. The code is functioning as expected since each image in the array contains an index field representing its position. // Res ...

Utilizing Jquery for toggling HTML elements and styling with CSS

I'm trying to implement a jQuery toggle button that switches between two states below; When I click the Toggle Button/link, everything in picture A (above the red line) should be hidden. Clicking the toggle button again would make it reappear. I&apo ...

Building a dynamic form in React: Fetching select data from an API, posting to another API, and automatically clearing fields upon submission

I am currently working on a form that utilizes a GET request to retrieve data from an API endpoint and then proceeds to make a POST request to another endpoint. Although I have been successful in achieving this function, I am facing challenges with reset ...

What impact does switching from HTML input to form_for in Rails have on the structure of the code?

Recently, I made some adjustments to the website I've been working on. Initially, it was created using HTML, CSS, and Javascript. There was a section for email subscriptions which originally only had an HTML input field. However, as we started develop ...

Is there a way to hide the blinking cursor at the conclusion of the animation after 3 seconds?

I've been working on this HTML snippet <div class=typewriter> <h1>Lorem Ipsum</h1> </div> and included some CSS styles as well .typewriter { display: inline-block; } .typewriter h1 { overflow: hidd ...

Material-UI organizes its Grid Items vertically, creating a column layout rather than a horizontal row

I'm struggling to create a grid with 3 items in each row, but my current grid layout only displays one item per row. Each grid item also contains an image. How can I modify the code to achieve a grid with 3 items in a row? Here's the code snippet ...

What is the best way to automatically resize a React Material Slider component?

Is there a way to set the slider to automatically adjust its width? Currently, I can only specify a fixed width in the css or leave it blank, resulting in a zero-width slider. What I need is to have the label on the left (with variable size) and then the ...

Mastering the art of horizontal alignment in forms

So, within my render function, the following code is used: render() { <div> <form id="one"> <TextField id="t1" ... /> <Button id="b1" /> </form> <div id="empty"><div ...

Guide on displaying Adsense ads specifically for mobile devices

Can anyone provide me with a proper method to show/hide my AdSense ads on both mobile and desktop? I am currently using a method that results in 2 console errors. Here is the current approach: We are utilizing two classes, "mobileShow" and "mobileno," t ...

Trigger a React Redux action while changing routes

I have successfully implemented "react-router": "^4.1.1" in my project and now I need to trigger an action when the routing changes. Can anyone guide me on how to achieve this? /index.js/ render(( <Provider store={store}> <BrowserRouter& ...

Display an Asterisk Icon for md-input fields with lengthy labels

Documentation states that md-inputs add an asterisk to the label if it is a required type. However, when input containers have width constraints and long labels, the label gets truncated and the asterisk becomes invisible. From a user experience perspectiv ...

Display a persistent bar on the page until the user reaches a specified <div> element through scrolling

Looking to implement a sticky bar at the bottom of my page that fades out once the user scrolls to a specific div and then fades back in when scrolling up and the div is out of view. This bar should only appear if the user's screen size is not large ...

Implementing a Toggle Feature in React Components for Looping Elements

Hey everyone, I'm encountering a problem on my development site and could use some assistance. The issue I'm facing involves looping through a data file to create project cards with show more/show less functionality for displaying/hiding descript ...

Images in Chrome are shivering when animated at unconventional positions

I am attempting to create a masking animation that reveals an image from its center. The animation is working well, but I have encountered a problem in Chrome where the revealed content shakes. Here is an example on jsfiddle: http://jsfiddle.net/BaKTN/ I ...

I must create a dropdown and lift up feature similar to the one provided in the example

I am looking to implement a drop-down navigation bar on the top right and a pop-up button/links on the top left. The drop-down menu should appear when the screen size reaches a certain pixel width. If you can't see the drop-down navigation (NAV), adju ...

Issue with IE11: Flexbox with absolute positioning not sizing correctly, fills entire width instead of individual content

Here's a simple case to reproduce the issue: <div style="display: inline-block; position: relative; border: 1px solid black;"> short <div style="display: flex; position: absolute; border: 1px solid black; top: 100%; lef ...

Encountering an issue with the history module when utilizing the webpack dev server

I am encountering an issue while trying to run webpack dev server. The history functionality was working fine until I started using the webpack module. A warning message appeared in my console: WARNING in ./src/history.js 2:15-35 export 'createBrows ...

`Can a creation of this nature be accomplished?`

In my text input field, users can type messages to send to me. I'd like to add buttons on the page with symbols like "!", "XD", and "#". When someone clicks on a button, such as the "!" button, that corresponding symbol should be inserted into the tex ...