Creating a CSS grid layout with dual columns to dynamically adjust and accommodate two items in each row

I am attempting to create this layout using CSS grid:

https://i.sstatic.net/Ktbnm.png

The goal is to have an input and a textarea. The textarea should be of the size of 2 inputs. If there is already a textarea in the previous column, the next column should accommodate 2 inputs. The number of inputs/textareas will vary (determined by the back-end).

Can this be achieved with CSS grid alone?

This is what I currently have:

.textContainer {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-gap: .75rem 1rem;
}

.inputGroup {
  display: flex;
  flex-direction: column;
}

.input {
  border: 1px solid gainsboro;
  border-radius: 8px;
  margin-top: .5rem;
  padding: 0.5rem 1rem;
  resize: none;
}
<div class="textContainer">
  <div class="inputGroup">
    <label>Label 1</label>
    <textarea rows="5" class="input"></textarea>
  </div>

  <div class="inputGroup">
    <label>Label 2</label>
    <input type="text" class="input" />
  </div>

  <div class="inputGroup">
    <label>Label 3</label>
    <input type="text" class="input" />
  </div>

  <div class="inputGroup">
    <label>Label 4</label>
    <input type="text" class="input" />
  </div>

  <div class="inputGroup">
    <label>Label 5</label>
    <input type="text" class="input" />
  </div>

  <div class="inputGroup">
    <label>Label 6</label>
    <input type="text" class="input" />
  </div>

  <div class="inputGroup">
    <label>Label 7</label>
    <input type="text" class="input" />
  </div>

  <div class="inputGroup">
    <label>Label 8</label>
    <textarea rows="5" class="input"></textarea>
  </div>

  <div class="inputGroup">
    <label>Label 9</label>
    <input type="text" class="input" />
  </div>
</div>

Answer №1

Adjust the textarea items to span two rows in the grid layout.

This change will allow the inputs to fill the second row of the textarea.

/* new */
[textarea] {
  grid-row: span 2;
}

/* original code */
.textContainer {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-gap: .75rem 1rem;
}

.inputGroup {
  display: flex;
  flex-direction: column;
}

.input {
  border: 1px solid gainsboro;
  border-radius: 8px;
  margin-top: .5rem;
  padding: 0.5rem 1rem;
  resize: none;
}
<div class="textContainer">
  <div class="inputGroup" textarea><!-- adjust here -->
    <label>Label 1</label>
    <textarea rows="5" class="input"></textarea>
  </div>

  <div class="inputGroup">
    <label>Label 2</label>
    <input type="text" class="input" />
  </div>

  <div class="inputGroup">
    <label>Label 3</label>
    <input type="text" class="input" />
  </div>

  <div class="inputGroup">
    <label>Label 4</label>
    <input type="text" class="input" />
  </div>

  <div class="inputGroup">
    <label>Label 5</label>
    <input type="text" class="input" />
  </div>

  <div class="inputGroup">
    <label>Label 6</label>
    <input type="text" class="input" />
  </div>

  <div class="inputGroup">
    <label>Label 7</label>
    <input type="text" class="input" />
  </div>

  <div class="inputGroup" textarea><!-- adjust here -->
    <label>Label 8</label>
    <textarea rows="5" class="input"></textarea>
  </div>

  <div class="inputGroup">
    <label>Label 9</label>
    <input type="text" class="input" />
  </div>
</div>

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

A guide to implementing hover and mouse click effects on ImageList using Material UI version 5.11.11

I have successfully created a list of images using MaterialUI, but now I would like to enhance the user experience by adding a mouse hover effect to highlight the image under the cursor. Below is the code snippet for my image list. How can I accomplish t ...

Deleting a property once the page has finished loading

My issue is a bit tricky to describe, but essentially I have noticed a CSS attribute being added to my div tag that seems to come from a node module. The problem is, I can't seem to find where this attribute is coming from in my files. This attribute ...

Can the parent's :active pseudo class be overridden?

I'm struggling with changing the color of a div when clicked while preventing its parent's pseudo-class from triggering. Here is the markup I have: <div class="parent"> I should change my color to green when clicked <div class="elem ...

Modify the color of the active class in Bootstrap

I am trying to modify the color of the active class in my HTML code as I create a nav sidebar. Here is the snippet from my code: <div class="col-sm-2"> <ul class="nav nav-pills nav-stacked nav-static"> <!--stacked for vertic ...

Maximizing Bootstrap card size in Angular4: A step-by-step guide

How can I make Bootstrap cards resizable on top of other elements when clicked on an icon to make it full screen and overlay on other cards? detail.component.html <div class="card card-outline-info" > <div class="card-header bg-info"><h5 ...

What is the best way to align HTML inputs within a grid, whether it be done automatically or manually using JavaScript?

Arrange 20 HTML inputs with various data types into grid-columns as the user inputs data. Ensure that the grid container has div elements correctly positioned for output. ...

The submit button is getting disrupted by the background image being set

Implement the following CSS code to set a background image on a single-page app (Angular). .page::before { content: ''; position: absolute; background-size: cover; width: 100%; height: 100%; background-image: url("../../assets/weird. ...

Designing php/mysql data in a container

After successfully converting an ordered list output into a div output, I now face the challenge of stacking arrays on top of each other (and side by side with the two divs) like they would in an ordered list. Here is the original code snippet: <?php ...

Get your columns in order with Bootstrap4

I need to rearrange my 3 columns on desktop and mobile devices in different ways. Currently, my grid structure is set up like this: <div class="row"> <div class="col-xs-3 col-md-6"> 1 </div> <div class="col-xs-3 col-md- ...

Displaying items in the shopping cart across two separate columns

I have a website located at Within that page, I have included the Cart table using a woocommerce shortcode. Currently, the cart table is positioned below the billing details and payment options section. My goal is to move this cart table to be situated ...

Discover the ultimate guide to harmonize IE 9 with the ingenious Bootstrap Multiselect plugin developed by davidstutz

I've come across an amazing plug-in developed by David Stutz that allows for a Bootstrap and jQuery multi-select options list. Here are some resources: Check out the source code on Github Find documentation and examples here This plug-in works fla ...

When the browser size shrinks, turn off the drop-down navigation menu

I'm currently working on a responsive website, and I have encountered an issue with a dropdown menu. When the browser size is reduced to 900px, the menu shifts all the way to the left side. To address this, I added some left padding to keep it off the ...

Hover events in the browser are currently operating at a sluggish pace

When I hover over a photo, an overlay is supposed to appear. However, if I move the mouse quickly (from the top to the bottom of the page, for example), the overlay remains visible even after I stop hovering over the photo. I have tried different event ha ...

Need a jQuery function that updates the 'id' after clicking on a specific div? Here's how to do it!

I need help simplifying my current situation. Step 1: I want to increment the variable "count" by 1 every time a specific div is clicked. Step 2: After updating "count", I want to utilize it in another function. This function involves copying the value f ...

Difficulty with spacing in HTML/CSS styling

Here is what I'm currently working on: link to jsfiddle code * { margin: 0; padding: 0; } html { height: 100%; } header, nav, section, article, aside, footer { display: block; } body { font: 12px/18px Arial, Tahoma, Verdana, sans- ...

How can an accordion icon be added and list toggling be accomplished when HTML data is sourced from an API instead of a JSON response?

I have an API that sends HTML data as a response. The task at hand is to add accordion icons to the items in the list and enable list toggling using HTML, CSS, JavaScript, React, and MaterialUI. Unfortunately, I am limited in my options and cannot use JQ ...

I am experiencing an issue wherein after using jquery's hover() function, the CSS :hover state is not

Following the jquery hover function, the css hover feature ceases to work. In my html, there are multiple svg elements. A jquery script is applied where hovering over a button at the bottom triggers all svg elements to change color to yellow (#b8aa85). S ...

Update the background image to be smoother, with no text overlay

I am working on a website where I need to smoothly change between background images. Here is the JavaScript code I currently have: var bg=[ 'images/best.jpg', 'images/61182.jpg', 'images/bg.jpg' ...

Tips for handling transparent areas during a hover event

Is there a way to make only the rhombus image respond to hover events? I want to exclude the transparent area, as shown in this picture. <img src='http://s30.postimg.org/xpd6gwla9/1_Copy.jpg' id="first"> #first:hover { -moz-box-shadow ...

Having difficulty resolving issues with the chat box (div) scroll bar staying fixed at the bottom

I am currently working on a chat application and I am facing an issue with fixing the scroll bar at the bottom of a div when loading my PHP file. I have already written a script to achieve this, but despite accessing it using CSS Id, I am having trouble ge ...