Expanding min-content CSS Grid cell based on neighboring cells

I am working with a CSS grid that consists of two columns and five rows, but the layout changes when the display is above 768px. The first four rows are set to "min-content", while the last row is set to auto. I have defined template grid areas for each cell, except for one area (a6) that covers the 3rd to 5th row in the second column.

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

Everything works fine when there is little or no content in the a6 area. However, when more content is added to a6, the height of a5 and a7 expand even though their content remains unchanged.

CSS:

    html,
    body {
      height: 100vh;
      padding: 0;
      margin: 0;
    }

    .maingrid {
      height: 100%;
      padding-left: 15px;
      padding-right: 15px;
      background-color: red;
      display: grid;
      grid-template-rows: min-content min-content min-content min-content min-content min-content min-content auto;
      grid-template-areas: 'a1' 'a2' 'a3' 'a4' 'a5' 'a6' 'a7' 'a8';
      grid-row-gap: .2em;
    }

    @media only screen and (min-width:768px) {
      .maingrid {
        grid-template-columns: 9fr 3fr;
        grid-template-rows: min-content min-content min-content min-content auto;
        grid-template-areas: 'a1 a2' 'a3 a4' 'a5 a6' 'a7 a6' 'a8 a6';
        background-color: darkcyan;
      }
    }

    .maingrid div {
      background-color: black;
    }

    .a1 {
      grid-area: a1;
      background-color: pink !important;
    }

    .a2 {
      grid-area: a2;
      background-color: aliceblue !important;
    }

    .a3 {
      grid-area: a3;
      background-color: aqua !important;
    }

    .a5 {
      grid-area: a4;
      background-color: blue !important;
    }

    .a4 {
      grid-area: a5;
      background-color: brown !important;
    }

    .a6 {
      grid-area: a6;
      background-color: burlywood !important;
    }

    .a7 {
      grid-area: a7;
      background-color: chartreuse !important;
    }

    .a8 {
      grid-area: a8;
      background-color: darkorange !important;
    }

HTML:

<main class="maingrid">
  <div class="a1">BLAH</div>
  <div class="a2">BLAH</div>
  <div class="a3">BLAH</div>
  <div class="a4">BLAH</div>
  <div class="a5">BLAH</div>
  <div class="a6">
    at<br />at<br />
  </div>
  <div class="a7">BLAH</div>
  <div class="a8">
    <button type="button" onclick="BreakTheGrid();">click me :(</button>
  </div>
</main>

JS (just to get the toggle button to work):

var isBroken = false;

function BreakTheGrid() {
  if (!isBroken) {
    $('.a6').html("the<br/>left<br />columns<br />have<br />expanded<br />boo!<br />");
  } else {
    $('.a6').html("no<br/>issue");
  }
  isBroken = isBroken == false;
}

If you want to see the issue in action, check out this JSFiddle: https://jsfiddle.net/up6afdj4/. Feel free to click the button in a8 to toggle the content of a6 and observe the issue.

This is my first time working with CSS grid, so I may have made some mistakes. Any help would be appreciated!

Answer №1

When you set auto for the fifth row, containing the grid area a6, it activates the Grid's auto stretch algorithm. This algorithm reallocates any extra space among the rows covered by that grid area (refer to sections §11.5, §11.5.1, and §11.8).

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

If you change from auto to 1fr, the last row will now consume all the available free space, pushing the rows above towards the top.

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

See the updated demonstration here

To better understand how auto distributes space, check out my responses in these threads:

  • Eliminating wide gaps in CSS Grid layouts
  • How to collapse unused rows in a CSS grid?

(Visual aids courtesy of Firefox DevTools Grid Inspector.)

Answer №2

By replacing auto with 1fr in the grid-template-rows, it effectively resolves the issue at hand.

Although I'm uncertain of the exact reasoning behind this solution, if anyone could provide a clearer explanation, they will receive the accepted answer from me :)

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

The footer on my webpage seems to be completely incorrect, but I'm struggling to identify what is causing the conflict

I'm puzzled by the issue with the footer on my webpage, especially the strange line of black opacity that shouldn't be there. I'm unsure why this is occurring. You can view the problematic page here. I've also created a codepen with the ...

Ways to ensure a div is positioned beneath another div

I am facing an issue with two divs; one on the right and the other on the left. When I try to add a third div below the right div, it ends up appearing under the left div or at the left side of the left div. Here is my current setup: https://i.sstatic.n ...

What is the largest image dimension allowed for website use?

What are the dimensions in pixels and file size in MB? I've been curious about this for a while, appreciate any insights! ...

Web site designed with the aesthetics of Office 2007 aesthetic

Can anyone guide me to a website template that resembles the design of Office 2007? I'm hoping to find one that has similar colors, hover effects, and overall aesthetic. ...

Link causing chaos in CSS animation

I've created a CSS animated banner. You can view the result here. However, I'm facing an issue where adding a hyperlink to some of the pictures is causing the animation to mess up. The first picture has a href attribute, making it smaller and not ...

Ways to incorporate a smooth transition between the opened and closed fab functionality

I am currently utilizing VueJS and Vuetify in my project. I have successfully implemented Vuetify's FAB component, which displays a list of fab buttons as demonstrated in the documentation: <div id="fab-button"> <v-speed-dial v-mo ...

Images that adjust to different screen sizes within a grid layout

I have four images that need to be aligned in the following layout: ____________ |1 |4 | |_____| | |2 |3| | |__|__|______| They must be flush against each other, occupy 100% of the viewport's width, and most importantly, be respon ...

Styling the Container Component in ReactJS

I need to apply some styling to the "Forms_formline__3DRaL" div (shown below). This div is generated by a component, but it seems that the style I want to add is being applied to the input instead of its parent div. How can I use ReactJS to set the style " ...

Two hyperlinks are not visible

I've added 2 links in my header component, but for some reason, they are not displaying. It's strange because everything appears to be correct. These 2 links are part of a list within a ul element. <nav class="navbar navbar-expand-lg navbar-d ...

Challenges with custom select boxes in Firefox

Is anyone else experiencing issues with a custom select box in Firefox? It seems like the problem might be related to the code snippet below: $(".custom-select-trigger").on("click", function() { $('html').one('click',function() { ...

Press on a circle to adjust its size to fit the section or division

Is it possible to make a circle expand and fill the section/div when clicked? I want the circle to fill the screen upon clicking, and then have some text appear in its place. Before posting this, I searched on the web and came across this jsfiddle link. ...

When an element is focused using jQuery, allow another element to become clickable

I've been working on implementing an input field along with a dropdown list right next to it. The concept is that users can either type in the input field or choose a value from the dropdown list. I attempted to use styles to show the dropdown list wh ...

The Bootstrap flex-grow-0 class did not work as intended in my navigation bar

My webpage has a navbar that currently looks like this Click here for an image example of the code output This is the code I have written for the navbar: <nav class="navbar navbar-expand-sm fixed-top bg-white"> <div class="container my-2" ...

Launching the Bootstrap 5 modal will cause the header and background to shift towards the right side

Currently, I am working with Angular 2 and Bootstrap 5 for the front-end of my project. I noticed that when I open a modal, the header and background shift to the right, covering the scrollbar. Upon closer inspection, I discovered that the HTML code trans ...

Using the bootstrap container-fluid to create additional space on the right side

I've been trying to remove the right side padding in my container-fluid without success. There seems to be extra space where the red lines are marked. I have attempted various methods to fix this issue but I can't seem to identify where this ex ...

Top tips for utilizing CSS in a web component library to support various themes

Our team is currently in the process of developing a comprehensive design system that will be utilized across multiple projects for two distinct products. Each product operates with its own unique brand styleguide which utilizes design tokens, such as: Th ...

What is the best way to divide widgets in a Wordpress Sidebar?

I am looking to customize the spacing between widgets in my Wordpress sidebar, similar to what is shown on this example site. Currently, my sidebar does not have the desired spacing. I have tried various CSS codes found online, but none of them seem to be ...

Split the flex column into two separate rows

These are the columns I have (Fiddle: https://jsfiddle.net/DTcHh/15420/embedded/result/) https://i.sstatic.net/Uvi0g.png Using flex, I was able to achieve equal height for the columns: @media (min-width: 992px) { .columns-equal { display: -we ...

Struggling to figure out how to make this Vue function work

I am working with a list of tasks, where each task is contained within a div element. I am looking to create a function that changes the border color of a task to red when clicked, and reverts the previous task's border to normal. I have two files: &a ...

Interactive Radial Menu Using Raphael JS

Greetings and thank you for taking the time to consider my predicament. I am currently working on creating an SVG menu using raphael, but unfortunately, geometry has never been my strong suit. Below is a visual representation of what I have managed to cre ...