Chrome Flexbox Hacks: Controlling the Size of Nested Items

How can I ensure that an element nested within a flexbox member in Google Chrome is limited to the size of its container? For example, consider the following scenario:

<div style="display:flex; flex-direction:column; height:100vh;">
  <div style="height:60px;">Static header</div>
  <div style="flex: 1 1 0; overflow:hidden;">
     <div style="overflow:auto; height:100%;" id="problem-is-here">
       Lots and lots of content, way too much to fit in the viewport
     </div>
  </div>
</div>

In Firefox and IE11, the innermost element with id="problem-is-here" adjusts its height to match that of its parent due to height:100%, resulting in a vertical scrollbar when content overflows. This behavior is what I desire.

However, in Chrome, the innermost element expands to accommodate all content regardless of its parent's height, causing the excess content to be inaccessible without a visible scrollbar. How can I make Chrome render this element with a maximum height equal to its parent, especially when the parent's height is determined by a flexbox rather than a specific CSS height value?

Various suggestions like setting min-height:0 or ensuring flex-basis is set to

0</code have been attempted but did not yield the desired outcome. View the issue demonstrated here: </p>

<p><a href="http://plnkr.co/edit/UBRcrNmR5iDbn3EXu6FI?p=preview" rel="nofollow">http://plnkr.co/edit/UBRcrNmR5iDbn3EXu6FI?p=preview</a></p>

<p>This problem revolves around limiting the height of the <code>div
with ID heres-the-problem to that of its parent.

Answer №1

Let's start by delving into the terminology:

...how can I ensure that an element nested within a flexbox member (by "member of a flexbox," I am referring to a child of an element styled with display:flex) limits its size to match the size of the flexbox member it resides under?

A flex container, technically known as a flex container, or colloquially referred to as a flex parent, is an element with the display: flex property.

The children of a flex container are identified as flex items. It is important to note the term children in this context regarding first-level elements. Descendants beyond the initial children do not fall under the category of flex items, and most flex-related properties do not apply to them.


Now, to address your query:

The issue arises due to Firefox (and possibly IE11) interpreting the percentage height rule differently than Chrome.

In essence, the vertical scrollbar you desire isn't appearing in Chrome because the utilization of percentage heights deviates from the conventional implementation specified here.

CSS height property

percentage
Indicates a percentage height value calculated relative to the height of the generated box's containing block. If the height of the containing block is not explicitly defined and the element isn't absolutely positioned, the value computes as "auto".

auto
The height adjusts based on other property values.

To clarify, when assigning a percentage height to an element, it is imperative to specify a height for the containing block (i.e., the parent).

In your code structure, body at level 1 has a height: 100vh declaration.

One level below, .max-flex at level 2 features height: 100%.

Four levels deep, .large-nested-div at level 4 also utilizes height: 100%.

However, at .variable-flex-content at level 3, there is no explicit height property set. Essentially, defining the height using flex: 1 1 0 leaves a missing link in the hierarchy according to Chrome, leading to a violation of the specification. Chrome expects to encounter the height property, and its absence results in an automatic computation of the height as auto.


Chrome vs. Firefox (IE11 not included in this comparison)

In traditional scenarios involving calculating percentage heights, browsers have typically adhered to interpreting the term "height" in the spec as representative of the height property value specifically. Although it could theoretically represent a generic height parameter, the requirement of the height property has become the prevalent interpretation standard. Instances of min-height or max-height affecting a parent element with regards to percentage heights were uncommon in practice.

Recently, as exemplified in this inquiry and in responses to other threads and related discussions, Firefox has broadened its perception to accommodate flex heights as well.

The degree to which each browser complies with standards remains ambiguous.

This ambiguity is exacerbated by the fact that the definition of the height property hasn't been revised since 1998 (CSS2).


The Proposed Solution

Instead of employing flex: 1 1 0% to define the height of .variable-flex-content, consider utilizing height: 100%, height: calc(100% - 60px), or exploring absolute positioning as alternatives.

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

Using jQuery to handle multiple buttons with the same ID

Is there a way to address the issue of multiple buttons sharing the same id? Currently, when clicking on any button, it only updates the record with id=1. How can this problem be resolved? div id="ShowPrace"> <?php try { $stmt = $pdo->prepare(" ...

What is the method for changing the color of text within a button when hovering using CSS?

In my OceanWP WordPress theme, I customized the top-bar menu by adding a button labeled "Post a Job" using CSS. Screenshot Link I have two top-bar menus - one for logged-in users and another for logged-out users. The "post a job" button has a 2px purple ...

How can I wrap text in Angular for better readability?

I've created a calendar in my code that displays events for each day. However, some event descriptions are too long and get cut off on the display. Even after attempting to use Word Wrap, I still can't see the full text of these events unless I c ...

Why do confirm or alert boxes in Safari on Windows require a double click?

I'm currently working on a simple JavaScript example where I want to display an alert box when an HTML button is clicked in SAFARI. However, I've noticed that it requires a double click to make the alert disappear from the screen. Does anyone ha ...

Attitude: Defiant and Ignoring Authority

So far, no suggestions have been made, indicating that maybe I haven't properly summarized the issue; The problem arises when I absolutely position the section with the class="container" using an additional class or id specific to that <div>. I ...

prevent the page from scrolling to the top when clicking on an empty <a> tag

Is it possible to prevent the page from scrolling back to the top when a user clicks on an empty link tag using only HTML and without JavaScript? For example, if there is an empty link at the bottom of the page and a user clicks on it, the page jumps bac ...

What could be the reason for the sudden malfunction of the .tabs li:hover property on my

I'm having trouble with the :hover effect not working on my .tabs li element. I've tried commenting out my jQuery script, but it still won't apply. Can anyone help me figure out why? Here's a JSFiddle link for reference: https://jsfidd ...

What is the secret to the lightning speed at which this tag is being appended to the DOM?

Have a look at this concise sandbox that mirrors the code provided below: import React, { useState, useEffect } from "react"; import "./styles.css"; export default function App() { let [tag, setTag] = useState(null); function chan ...

The mouseover and mouseleave functions remain active even during window resizing

I wish to create a dynamic menu that appears when hovering over a user, but only if the window size is above 977 pixels. Take a look at my code below: $(document).ready(function() { $(window).on("load resize", function(event){ var windowS ...

Conceal column exclusively on smaller displays using Bootstrap 4

I want to design a grid that covers the whole screen with 3 columns (left, middle, right) Left: This column should only be displayed on large views and occupy 16.6% of the screen (2/12) Middle: This column should always be visible. It should take up 75% ...

Disappear scrollbar when overlay is activated

How can I hide the scroll bar when an overlay is displayed on my page? .overlay{ display: none; opacity:0.8; background-color:#ccc; position:fixed; width:100%; height:10 ...

I seem to be having an issue with the decimal point on my calculator - I only want to see one dot

Only need one dot for the calculator to function properly. The current situation with multiple dots is causing confusion. I intend to address other aspects of the code later, but I'm currently struggling with this issue. function dec(d) { let ...

Focusing on a specific div element

I need help selecting the initial div block that contains the word "Target". <div class="box-content"> <div> <div>Target</div> <div>xxxx</div> <div>xxxx</div> <div>xxxx</div> ...

Pressing the tab key makes all placeholders vanish

case 'input': echo '<div class="col-md-3"> <div class="placeholder"> <img src="images/person.png" /> &l ...

The 'Required' attribute in HTML is malfunctioning

The 'required' attribute is not functioning properly when I try to submit the form. I've searched online for a solution, but none of them have resolved my problem. Take a look at the code snippet below - I've used the required attribute ...

"Is there a way to target an element within another element that has inline styles using

What is the best CSS strategy for targeting elements with inline styles? How can I focus on the first span but exclude the second using only CSS? If that's not achievable, is it possible to accomplish this using jQuery? http://jsfiddle.net/TYCNE/ & ...

Hover activates a CSS animation where elements pop over other elements

I want to design a menu that resembles those "apps panels" we often see. My menu consists of 6 elements, arranged side-by-side with no space between them. When I hover over an element, it slightly pops as shown in the code snippet below: #account-bub ...

How can I write the code to enable dragging the button for navigating to the next page?

<a draggable="true" class="user" id="leonardo" ondragstart="dragUser(this, event)" aria-selected="undefined"> IPD</a> If I want the button to navigate to the next page when dragged, what code should I write? ...

Unable to Confirm the Verification of ITR1

During the verification of my ITR1, I encountered an error message that read: [#/ITR/ITR1/TaxPayments/TaxPayment/0/DateDep: string [NaN-NaN-NaN] does not match pattern ([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))];#; Please reach out to the ...

Tips for showing a public image in-text when pasted on Skype or various social media platforms

As an avid user of Skype, I have discovered that when pasting a link into Skype (using the desktop app) such as a YouTube link, a thumbnail is displayed in the chat. This got me thinking about whether there is a specific class or area in my code, be it CS ...