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:

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

CSS styles are combined and included at the beginning of the index.html file

During the creation of a production version of my Angular 9.0.4 application, I noticed that the CSS is bundled and placed at the top of the dist/index.html file as shown below: <link rel="stylesheet" href="styles.6ea28d52542acb20a4c6.css"><!docty ...

Ways to prevent image toggling when the mouse moves out of it or when the hover effect stops

I'm looking to toggle between two images on mouseover/hover and stop the toggling when hovering stops. Here is the HTML code I have: <div class="fader"> <img src="image1" /> <img src="image2" /> </div> This is the JQuery ...

The never-ending loading problem at the bottom of the Firefox screen seems

I am facing an issue while trying to open something in Firefox using window.open(). The loading screen seems to never stop, and I cannot pinpoint the reason behind it. Any advice on how to resolve this would be greatly appreciated. View Screenshot: This ...

The appearance of all input forms in MaterializeCSS when used in an electron environment appears to

After following the instructions here on how to get started with Materialize CSS, I am having issues with my input form appearing strange: The contents of my current index.html file are as follows: <!DOCTYPE html> <html> <head> & ...

What is the best way to align 3 divs next to each other with spacing and borders?

I'm having trouble aligning three divs side by side. When I try to add borders and a small gap between each div, the third div ends up on a new line. Is there a way to automatically resize the divs so they fit together? HTML: <div class="trendi ...

Customize your webpage's style to reflect your unique identity with user

Is there a way to allow users to customize the style of a webpage? I know I need multiple CSS files, but how can I implement the code that enables this customization based on user preferences? ...

Unable to set the correct title view for mobile devices

We have successfully styled the products for desktop view and larger phones, but we are facing challenges in adjusting them properly for smaller phones. Below you will find a photo and corresponding code snippets: /* Products list - view list */ .product ...

The right column in a relatively positioned layout descends as an element in the left column is revealed

Currently in the process of constructing a registration form for our website, everything is going smoothly with one minor hiccup. The form consists of two columns where users enter their information. Initially, when designing elements for the first (left) ...

Trouble with implementing a stationary header for a table on IE9?

I have successfully created a table with a fixed header and scrollable content. I utilized CSS classes "fixedHeader" for thead and "scrollContent" for tbody: thead.fixedHeader tr { position: relative; } html > body thead.fixedHeader tr { displa ...

Tips for creating multiple diagonal lines with CSS and HTML

Is there a way to create multiple diagonal lines within a rectangle using CSS and HTML from the start? I am interested in incorporating diagonal lines at the onset of the rectangle. Here is an example code that displays the rectangle: <div className={ ...

Adding negative values to the Tailwind CSS utility plugin is a simple process that can greatly enhance

Adding Negative Values to Tailwind CSS Utility Plugin Quick Summary: I've developed a custom Tailwind utility plugin that includes numeric values. I'm looking for a way to introduce negative numbers by adding a - at the start of the class, simi ...

Issue arises when attempting to compile .less file with variables sourced from a separate file

I encountered an issue with my .less file where it contains definitions that rely on variables from another file. For example: body { font-family: @baseFontFamily; font-size: @baseFontSize; color: @textColor; } At first, IntelliJ displayed t ...

Issues with webpage responsiveness, width not adjusting correctly

Just completed coding my website and now I'm facing a new challenge. I am focusing on making it responsive for tablet screens (768px) starting with the header section, but for some reason, the modifications I make in the responsive.css file are not ta ...

Problem with selecting odd and even div elements

I have a div populated with a list of rows, and I want to alternate the row colors. To achieve this, I am using the following code: $('#PlatformErrorsTableData').html(table1Html); $('#PlatformErrorsTableData div:nth-child(even)').css(" ...

Challenges of aligning a modal overlay in the middle of mobile screens

Currently, I am developing a website and encountering a specific issue with the modal structure. When viewing it on Codepen using Chrome devtools and toggling the device toolbar to simulate mobile screens, everything appears fine. However, when opening the ...

Choosing descendant elements in CSS styling

Is there a way to select child elements in sequence? For instance: <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li> ...

The toggle button requires two clicks to activate

I created a toggle button to display some navigation links on mobile screens, but it requires two clicks upon initial page load. After the first click, however, it functions correctly. How can I ensure that it operates properly from the start? Below is t ...

The background of an HTML button does not appear in IE8

My buttons are not showing up in IE8. I tried adding background: transparent url('..'), but it didn't help. Here is the code: .wpcf7 input.wpcf7-submit { background: url("/wp-content/themes/ASC/images/<?=ICL_LANGUAGE_CODE;?>_submi ...

Gradually bringing a tag into view, gently fading it away, and then altering the text before beginning the cycle anew

I have a situation where an tag's content is being dynamically changed with jQuery and then faded in and out using the Velocity JS library along with the setInterval function. Initially, everything works smoothly for about 30 seconds. However, after ...

I am looking to implement user authentication using firebase, however, the sign-in page is currently allowing invalid inputs

<script> function save_user() { const uid = document.getElementById('user_id'); const userpwd = document.getElementById('user_pwd'); const btnregister=document.getElementById('btn-acti ...