Ensuring equal growth of columns and rows in CSS grid when there is overflow: how can it be achieved?

While working on my project, I've noticed inconsistency in the behavior of the random grids I'm generating with JavaScript. I specify a set number of columns and rows for the grid, as well as the start and end values of the cells independently. My goal is to have cells grow when their content overflows beyond the minimum value of 50px, causing cells in the same row to also expand accordingly. You can view an example on this Fiddle.

In my current setup:

grid-template-rows: repeat(4, minmax(50px, auto));

However, you'll notice that only the 3rd row grows while the 4th row remains unchanged. To fix this, I adjusted the line to:

grid-template-rows: repeat(4, minmax(50px, 1fr));

Yet, this causes all rows to increase evenly when there's overflow, which is not the desired outcome. For reference, check out this updated Fiddle.

Interestingly, when overflowing the 6th cell instead of the 4th, the 2nd and 3rd rows grow uniformly since the 6th cell spans both rows. This aligns with my expectations, as seen in this alternative scenario. However, I'm still puzzled by why the initial issue persists.

Could this be a bug? Observing the same behavior across Chrome and Firefox leads me to believe that I might be overlooking something rather than it being a technical glitch.

Answer №1

The distinguishing factor in your initial fiddle is the absence of a single-row spanning element in the third row compared to the fourth row.

To resolve this issue, you can simply add filler elements to fill up all the rows as shown below:

When calculating the grid dimensions, items that span only one row are given priority due to their rigid restrictions within that specific row.

Subsequently, items spanning two rows are considered. If both rows are occupied and the item exceeds the available space, both rows expand. However, if only one row has been calculated with elements and the other remains uncalculated, it is best to maintain the dimension of the existing row and only extend the latter one.

body{margin:0;padding:20px;font-size:24px;font-family:sans-serif}
body > div{
  display: grid;
  max-width: 500px;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows:    repeat(4, minmax(50px, auto));
  grid-gap: 20px;
}
body > div > div{
  padding: 20px;
  background-color: #CCC;
}
body > div > div:nth-child(1){
  grid-row-start:    1;
  grid-column-start: 1;
}
body > div > div:nth-child(2){
  grid-row-start:    1;
  grid-column-start: 2;
}
body > div > div:nth-child(3){
  grid-row-start:    1;
  grid-row-end:      3;
  grid-column-start: 3;
}
body > div > div:nth-child(4){
  grid-row-start:    3;
  grid-row-end:      5;
  grid-column-start: 2;
}
body > div > div:nth-child(5){
  grid-row-start:    4;
  grid-column-start: 1;
}
body > div > div:nth-child(6){
  grid-row-start:    2;
  grid-row-end:      4;
  grid-column-start: 1;
}
<div class="test">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4<br>4<br>4<br>4<br>4<br>4<br>4</div>
  <div>5</div>
  <div>6</div>
  <filler></filler>
  <filler></filler>
  <filler></filler>
  <filler></filler>
</div>

Answer №2

In my opinion, it's not necessary to define grid-row-end individually for each element. It would be more efficient to ensure that all rows have identical grid-row-start values. I trust this achieves the intended outcome:

View the demo.

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 flexbox completely collapses all child elements that are empty

Is there a way to collapse the empty "col" divs in this html structure while keeping the content divs equally sized (two columns in this case)? * { box-sizing: border-box; } .container { border: 1px dashed rgba(255, 0, 0, .5); background-color: b ...

What is the best method for applying a style specifically to controls located on the lower part of the

I am currently working on designing a table in a webpage and need to set styles for the <td> tags. Is it possible to apply different styles to the upper and lower cells? Can I use a <style> that specifically targets the bottom <td> tags? ...

Fully conceal a DIV while the webpage is loading

Following the header, I've implemented a hidden div with a height transition that can be activated by a button. However, there seems to be an issue where the browser displays the content of the div while the page is loading and then hides it afterward ...

Developing a dynamic scheduling system with PHP

I have just started learning php and I'm completely lost. My goal is to create a calendar layout that is 4 rows by 4 columns. Each row and column should represent one month, for example January, February, March, and April in the first row. I'm no ...

What are the best ways to ensure that my side menu, bottom buttons, and content are all responsive

I have been struggling with creating a responsive side menu in my HTML code. Despite my efforts, the side menu contents, buttons, and layout do not adjust properly when resizing the browser window. When I drag the browser size from bottom to top, the butto ...

Adjust the loading bar component in Material UI to allow for customizable color changes

I am currently learning how to use material ui. My goal is to customize the loading bar's CSS. I checked the documentation and utilized colorPrimary classes. However, the changes are not appearing as expected. Could you please guide me on how to resol ...

Executing a TypeScript function directly in HTML without the need for a click event

I understand how to trigger a TypeScript function when clicking a button, but how can I initiate a function without relying on a specific event? My goal is to call a function once an array named chartData has been populated. Here is the code snippet I have ...

Implementing checkboxes for each API data in JavaScript

I am fairly new to this and I am currently working on a to-do list. I want to include a checkbox next to each item from the API to indicate whether it has been completed or not. <script type="text/javascript"> fetch('http://localhos ...

JavaScript tip: How to disregard symbols that affect the length of text in a textbox?

I am working on a task which involves counting only the text, excluding symbols, entered in a textbox. For instance, if a user types email_ex_3/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="05374568646c692b666a68">[email ...

Tips on making a progress bar with a reverse text color for the "completed" section

I've been tackling this issue for hours, but haven't found a working solution yet. The challenge is to create a progress bar with a dynamic width and a centered label that changes its text color between the progressed and unprogressed parts. Here ...

What is the best way to incorporate a <c:forEach> loop into JSP operations?

I am attempting to calculate the product of two variables that are associated with an item in a loop. Below is the code snippet from the JSP file: <table> <thead> <tr> <th>PRICE</th> <th ...

Get the nearest offspring of the guardian

I have a main 'container' div with multiple subsections. Each subsection contains 1 or 2 sub-subsections. Let's say I have a variable that holds one of the subsections, specifically the 4th subsection. This is the structure:container > s ...

Develop a chart featuring sub-categories and column headings

I'm in the process of creating a table with subcategories and headers that resembles the image provided below: Here's what I've come up with so far: <table> <thead> <tr> <th>Item</th> & ...

Unable to implement the bootstrap panel class as expected

It seems like the bootstrap panel class is not being applied in this code snippet: <div class="container main-content"> <div class="row"> <div class="col-sm-3"> <div class="panel panel-primary"> ...

Stop the replication of HTML/CSS styles during the extraction of content from a div

Is there a way to prevent the copying of CSS properties, such as font styles and sizes, when content is copied from a div? I want only the plain text to be copied to the clipboard, without any formatting applied. ...

Obtain the image format from an HTML page using the Jsoup library on Android

<div class="_jjzlb" style="padding-bottom: 55.2778%;"><img alt="AT Dam party.. #nashik #big #dam" class="_icyx7" id="pImage_11" src="https://instagram.fbom1-2.fna.fbcdn.net/t51.2885-15/e35/17438694_254543168340407_6435023364997251072_n.jpg" style ...

Creating a CSS-only carousel - Ensuring its correctness (especially in terms of height)

Just stumbled upon this amazing CSS-only carousel. Any hints on how I could make it responsive and display two carousels side by side? Click here to view the carousel. /* ***************************************************** */ /* SLIDER 1 */ /* ****** ...

Review and add alt attributes to images that are missing them

I have a plan to create a Java tool that can evaluate HTML pages on an existing website. The goal is to add the alt="" attribute to any image that currently lacks it. One idea is to use an HTML parser, such as HtmlCleaner, to generate a DOM structure of th ...

Area of Interest - Underline

Check out this navigation menu: https://jsfiddle.net/LauraStoian/5kmo0v7e/. body { border: solid 3px black; } #UL_1 { align-items: stretch; block-size: 103px; box-sizing: border-box; /* Additional CSS properties */ } /* Styling for UL ...

Unable to establish the relative placement of Div elements

Currently, I am working on an Analog clock project using React, Typescript, and SCSS. My main goal is to keep the CSS code primarily in the SCSS file and minimize its use in inline HTML (or eliminate it completely). Here is an excerpt from my SCSS file: ...