The CSS Grid extends vertically beyond its bounds, rather than activating the overflow-y property

Why does the grid blow out in the following snippet? The grid has a lime green border, each child has a dotted red border, and you can see the dotted red extends beyond the lime grid border. The .child2 should be made scrollable to prevent the grid from blowing out.

In terms of finding a solution: I cannot set a fixed height for the .grid-children, as the number of rows varies between 5 or 6. Additionally, the rows are not always the same size, with the first row being taller than the others.

It's worth mentioning that there is no grid gap or margin on the .grid-children, which differs from other proposed solutions.

.grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  height: 100px;
  border: solid lime 2px;
}

.grid-child {
  display: flex;
  flex-direction: column;
  height: 100%;
  border: dashed red 1px;
}

.child2 {
  flex-grow: 1;
  overflow-y: auto;
}
<div class="grid">
  <div class="grid-child">
    <div class="child1">
      child 1
    </div>
    <div class="child2">
      <div>foo1</div>
      <div>foo2</div>
      <div>foo3</div>
      <div>foo4</div>
      <div>foo5</div>
      <div>foo6</div>
    </div>
  </div>
  <div class="grid-child">another grid child B</div>
  <div class="grid-child">another grid child C</div>
  <div class="grid-child">another grid child D</div>
</div>

Answer №1

You have set the height of .grid to 100px, so the .grid-child should adapt to its parent's height.

Instead of using height: 100% for .grid-child, consider using min-height: 100%

Here is the updated code:

.grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  height: 100px;
  border: solid lime 2px;
}

.grid-child {
  display: flex;
  flex-direction: column;
  /*height: 100%;*/
  min-height: 100%;
  border: dashed red 1px;
}

.child2 {
  flex-grow: 1;
  overflow-y: auto;
}
<div class="grid">
  <div class="grid-child">
    <div class="child1">
      child 1
    </div>
    <div class="child2">
      <div>foo1</div>
      <div>foo2</div>
      <div>foo3</div>
      <div>foo4</div>
      <div>foo5</div>
      <div>foo6</div>
    </div>
  </div>
  <div class="grid-child">another grid child B</div>
  <div class="grid-child">another grid child C</div>
  <div class="grid-child">another grid child D</div>
</div>

Answer №2

Applying overflow-y: hidden to the grid child elements appears to be effective:

.grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  height: 100px;
  border: solid lime 2px;
}

.grid-child {
  display: flex;
  flex-flow: column;
  overflow-y: hidden;
  border: dashed red 1px;
}

.child2 {
  flex-grow: 1;
  overflow-y: auto;
}
<div class="grid">
  <div class="grid-child">
    <div class="child1">
      child 1
    </div>
    <div class="child2">
      <div>foo1</div>
      <div>foo2</div>
      <div>foo3</div>
      <div>foo4</div>
      <div>foo5</div>
      <div>foo6</div>
    </div>
  </div>
  <div class="grid-child">another grid child B</div>
  <div class="grid-child">another grid child C</div>
  <div class="grid-child">another grid child D</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

The addClass and removeClass functions in jQuery are functioning properly only on Firefox browser

Could someone help me understand why this code works in Firefox but not in Safari or Chrome? The variable newClass retrieves the value from a select box, which is then used to create a selector for adding or removing classes using addClass/removeClass. I ...

Differences in line spacing can be observed between the INPUT and LABEL elements

What could be causing this unusual behavior when setting line-height to match font-size: While the height of label is 16px, the height of input is 18px... Why is that? If I adjust line-height: 18px or higher, the heights suddenly align. But why does this ...

What are the steps to create a CSS 'shell' design?

How can I create an image displayed as a circle with border-radius:50%, along with some text on the same line that has a set width and background color? I want to achieve this without hard coding values. What is the best approach to do this? Check out the ...

Transforming a horizontal list into a vertical one

What might be causing the message list to display horizontally in the admin dashboard, but vertically on other pages? .user-list{ display:inline; } .users header, .users-list a{ display: flex; align-items: center; padding-bottom: 20px; border-bottom: 1px s ...

Adjusting the outline color of a Material UI Select component in outlined mode to a different color when the dropdown is activated (currently shown as blue)

Is there a way to change the outline color of Material-UI select component outlined variant correctly? I need help changing the default blue color to red, any assistance would be greatly appreciated Click here for an image reference Reference code snippe ...

Employing jQuery to add an element as a sibling rather than a child node

I'm having trouble finding the specific functionality I need. My goal is to add sibling DOM elements to a disconnected node. From what I gather, it should be possible with either .after() or .add(), but for some reason both methods are not working as ...

Indent the text within a span element that is displayed as an inline block

I am in search of a solution using only CSS for the following issue. Take into account the HTML below: <p> <span>Some text</span> <span>Some text</span> </p> Both <span> elements are set as inline-block. ...

Utilize jQuery to style the background of the webpage, while excluding the Modal element when using Bootstrap

I'm currently working on implementing foggy.js () to create a blurred background effect for Bootstrap's Modal. While I've successfully blurred all elements in the background, the Modal Popup itself is also appearing blurry. So my question is ...

The hover effect flickers in Firefox, but remains stable in Google Chrome

Here is an example link to check out: I am facing a dilemma on how to solve this issue and unsure if it is related to jQuery or the DOM structure. If you have any suggestions, they would be greatly appreciated. Thank you! ...

I'm missing out on that gray background

input your code hereI am working with the following html/php code snippet: <div class="footerbottom"> <span class="footermenuitem"> <span class="footermenutitle">PRODUCTS</span> <?php $menu = menu_navigation_links('menu-pro ...

Hide the border on a table with CSS

I recently installed a Wordpress Template and customized the table's background and text colors. However, I am struggling to remove the border around the table. Despite searching for solutions, my limited CSS knowledge has not led me to a fix that wor ...

Javascript code for scrolling to the top isn't functioning as expected

I found a helpful code snippet for adding a scroll to top button on my webpage at the following link: http://jsfiddle.net/neeklamy/RpPEe/ Although I implemented the code provided, unfortunately, the scroll to top button is not displaying correctly. Here ...

There seems to be an error with JVectorMap: the <g> attribute transform is expecting a number but is instead receiving "scale(NaN) translate(N..."

Hey there! I'm currently working on creating a form with a map to select the country of birth using JVectorMap. However, when checking the Google Chrome developer console, I encountered the following error message: jquery-jvectormap-2.0.5.min.js:1 Err ...

div is consistently correct across all web browsers

After creating the following CSS code for a div to always be positioned on the right: #button > .right { background-position: -268px 1415px; height: 180px; position: fixed; right: -90px; top: 40%; width: 263px; -webkit-transform ...

React Native tutorial: Changing button color on press

When a user clicks on the TouchableOpacity element, I want to change the color of the button from the default gray to blue. Initially, the 'checked={this.state.newProjects}' newProjects variable is not present in the state, so when clicked it sho ...

The HTML and CSS code I wrote functions perfectly on Codepen, but for some reason, it appears different when I view it in my Chrome environment

My codepen displays perfectly, but when I run the same code on my local environment it looks off. I can't seem to figure out what is causing the difference, especially since I copied and pasted the exact code. Both the codepen and my environment are u ...

Creating movement in three distinct divisions

I am seeking a way to have three divs flying in upon click. The first DIV is positioned at the top, followed by one on the left and one on the right (both being below the top one). I wish for them to fly in from their respective directions - the top div fr ...

What is the process for incorporating Bootstrap 5 animation into a website?

I'm looking to incorporate animations similar to this example into the background of the page below. However, I'm unsure how to achieve this using Bootstrap 5. The image provided above is what I envision as the background for my HTML page. Any gu ...

Manipulating div position interactively with vanilla javascript during scroll

I'm trying to create an effect where an image inside a div moves vertically downwards as the user scrolls, stopping only at the end of the page. I've attempted a solution using a script from another post, but it doesn't seem to be working. ...

Do not apply styling to a particular page in CSS and HTML

Utilize the teachable.com platform and take advantage of their code Snippets feature (refer to attached photo #1) https://i.stack.imgur.com/dW1lB.png I inserted the following code: div { direction: rtl; } To modify the website's direction to be ...