Concerns with the adaptability of CSS grid rows

I'm struggling to find a solution for the issue I am facing. Is there a way to adjust each column's row height based on its content in a way that differs from the other column rows?

The code snippet below demonstrates the current setup, which is close to what I need but not quite right. The .recent section starts on row 2 and spans 2 rows, resulting in a total of 4 rows. This causes a large gap below the .middle and .right sections. Here is a screenshot for reference:

https://i.sstatic.net/M30mz.jpg

@dwjohnston provided a screenshot of the grid with your initial solution:

https://i.sstatic.net/sXCXh.jpg

.grid {
  display: grid;
  grid-template-columns: repeat(11, 1fr);
  grid-gap: 20px;
  align-items: start;
  background: pink;
}

.grid>.left {
  grid-column: 1 / span 3;
  grid-row: 1;
  background: green;
}

.grid>.recent {
  grid-column: 1 / span 3;
  grid-row: 2 / span 2;
  background: red;
}

.grid>.middle {
  grid-column: 4 / span 5;
  grid-row: 1 / span 2;
  background: gray;
}

.grid>.right {
  grid-column: 9 / span 3;
  grid-row: 1 / span 2;
  background: brown;
}
<div class="grid">
  <div class="left">
    Left
  </div>
  <div class="recent">
    Recent
    <br> Testing
    <br>
  </div>
  <div class="middle">
    Middle
    <br><br>
    <br><br>
    ...
    <br><br>
  </div>
  <div class="right">
    Right
  </div>
</div>

Answer №1

After reviewing the issue thoroughly, it appears that I have identified the root cause of the problem.

Here is the approach I propose:

.grid {
  display: grid;
  grid-template-columns: repeat(11, 1fr);
  grid-gap: 20px;
  align-items: start;
  background: pink;
}

.grid>.left {
  grid-column: 1 / span 3;
  grid-row: 1;
  background: green;
}

.grid>.recent {
  grid-column: 1 / span 3;
  grid-row: 2 / span 2;
  background: red;
}

.grid>.middle {
  grid-column: 4 / span 5;
  grid-row: 1 / span 4;
  background: gray;
}

.grid>.right {
  grid-column: 9 / span 3;
  grid-row: 1 / span 3;
  background: brown;
}
<div class="grid">
  <div class="left">
    Left
  </div>
  <div class="recent">
    Recent
    <br> Testing
    <br>
  </div>
  <div class="middle">
    Middle
    <br>text<br>
    <br>text<br>
    <br>text<br>
    <br>text<br>
    <br>text<br>
    ... (content continues)
  </div>
  <div class="right">
    Right
  </div>
</div>

Your initial implementation had a few issues:

  • .left occupied row 1 only
  • .recent extended to rows 2 and 3, causing an extra row at the bottom with a gap
  • .middle encompassed rows 1 & 2, expanding excessively and displacing row 2 significantly

To rectify this, I adjusted .middle to span 4 rows instead, which includes rows 1, 2, 3, and 4. This allocation allowed .recent to maintain its own compact rows while empowering .middle to occupy the fourth row dynamically.

https://i.sstatic.net/5be6s.png

Answer №2

Hopefully this meets your needs. From the feedback provided, it seems that there are no gaps in the code. I trust this is beneficial :)

.grid {
  display: grid;
  grid-gap: 20px;
  background: pink;
  height: 100vh;
  grid-template-rows: 20px 20px auto;
  grid-template-columns: 30% auto 30%;
}
.grid > .left {
  grid-column: 1;
  grid-row: 1;
  background: green;
}
.grid > .recent {
  grid-column: 1;
  grid-row: 2;
  background: red;
}
.grid > .middle {
  grid-column: 2;
  grid-row: 1 / span 2;
  background: gray;
  height: 100vh;
}
.grid > .right {
  grid-column: 3;
  grid-row: 1;
  background: brown;
}
<div class="grid">
  <div class="left">Left</div>
  <div class="recent">
    Recent Testing
  </div>
  <div class="middle">Middle
    <br><br>
    <br><br>
    <br><br>
    <br><br>
    <br><br>
    <br><br>
    <br><br>
    <br><br>
    <br><br>
    <br><br>
    <br><br>
    <br><br>
    <br><br>
    <br><br>
    <br><br>
    <br><br>
  </div>
  <div class="right">Right</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

Adjusting table dimensions according to webpage dimensions

When looking at the image below, you will notice a div that contains a table with some content. However, when resizing the web browser's page, the table goes beyond the boundaries of the div. My goal is to reduce the size of the table as it scales do ...

Adapting padding based on the height of the browser to ensure optimal layout

I'm interested in adjusting the padding value of a button element based on the height of the browser window, not its width. I want to make sure that the padding adjusts proportionally to the browser window's height. Is this achievable using CSS? ...

The HTML code displays the changes in output after resizing the screen

Currently, I am utilizing canvas(graph) within my Angular JS UI5 application. My main goal is to increase the width of the graph. However, whenever I attempt to adjust the size of the Graph, it remains unchanged. Interestingly, when I shrink the screen, th ...

A guide on updating the appearance of a list of comma-separated tags using HTML, CSS, and jQuery

Is there a way to replicate the functionality of the tag section on this website for creating question entries? How can this be achieved? ...

Adjusting the box sizing of a contentEditable DIV to conceal the first and last characters

I am having trouble with a ContentEditable div that requires the hiding of characters, specifically the first and last character. I need to overlay a layer on top of the "#" character to make it appear as if the character and the border line beneath it are ...

Tips for shifting an image upwards and extending it beyond one section while keeping the other section unaffected

Struggling all day while designing a CSS site, trying to hide one part of an image when it overflows and keep another part visible, in addition to moving the image up and down. Here's the HTML code snippet: <section class="next-gen"&g ...

Utilize JQuery variables for toggling the visibility of various DIV elements

On my webpage's splash page, there are 4 divs but only the home div is initially visible. The other three are hidden. Each of these divs has a button associated with it that triggers a jquery click event to swap out the currently visible div for the ...

Getting rid of all CSS properties currently set for the Mobile view

Utilizing third-party library components in my project. The library includes components that come with predefined styles for mobile devices. I am seeking to completely UPDATE these properties within my own code. When I say "update," I mean removing all th ...

Provide alternative styling through css in instances where the Google Books API does not provide an image

How can I modify the CSS code to display the author and title of a book when there is no image available from the Google Books API? Currently, users see a custom image linked here, but it's not clear what it represents without the name and author inf ...

The CSS background-image fade transition is optimized for Chrome browsers

HTML: <a class="navbar-brand page-scroll" href="#intro"></a> CSS: .navbar-custom .nav li a.navbar-brand { width: 70px; height: 62px; background: url(myimg.png) no-repeat; background-size: 70px 62px; display: block; po ...

Trying to display logo using 'content' property in CSS

I've been trying to display a logo on the header of my website, but I'm having trouble getting it to show up. I attempted to set the logo using HTML syntax as well as the following CSS code: .div { content: url (...jpg); } However, both metho ...

How to Blend Pseudo-classes in CSS?

If I want the selected text in a link to turn red when hovered over, can I achieve that using the following CSS code? .abc:hover::selection {color: red;} Also, if I have the following link: <a href="123" class="abc">4567890</a ...

Is there a way to adjust the font size of a select option?

Looking to customize the appearance of a select option dropdown list. Wondering if it's possible to change the font sizes of individual options rather than keeping them all the same? For instance, having the default: -- Select Country -- displayed a ...

Encountering undefined JavaScript functions when called from HTML

I had most of these functions working perfectly, but after restarting my program, I'm now encountering issues with undefined functions in Chrome. I can't pinpoint the exact problem, and even though I've checked through Eclipse, I still can&a ...

Make sure to consistently show the rating bubble within the md-slider

I am currently exploring the functionality of md-slider in its md-discrete mode. However, I would like to have the slider bubble always visible on the screen, similar to this: I do not want the slider bubble to disappear when clicking elsewhere. Is there ...

Internet Explorer 11 encountering a flaw with absolute positioning

In my web page, everything looks good in Google Chrome, Firefox, and Opera, but I'm facing an issue with Internet Explorer 11. Here is the simplified HTML: <div class="container"> <div class="page-content"> <div id="corner ...

Selecting JavaScript file on change as default option

Currently, I have an HTML file that prompts the user to select an XML file when the file selection is changed. Is there a way to make it so that by default, only one file is chosen? <form id='fileSelection' action=""> <center> <in ...

"Redirecting to an HTML page from the POST method is not supported in the Flask backend and Vanilla JS frontend setup

Utilizing the selected dropdown value from the frontend to perform calculations on the backend, resulting in an HTML page being returned. It's worth noting that no response is needed from the POST method, such as using return jsonify. Currently, I am ...

Discover the exact location of an HTML element within an iframe

I am currently attempting to determine the position of an element that is within an iframe. I have written the following code for this purpose: // Custom function to calculate the position of an element on the page function getElementPosition(elem){ var ...

What is causing my images to fail loading on IE but display properly on other browsers?

I am facing an issue where the images for my css header class load correctly in Chrome and FF, but not in IE8 or 7. Can anyone help me figure out what I may be missing? Below is the css class code that I am using: .TBox { color:#333333; font-size ...