Is there a method to ensure that flex children adhere to a minimum height, while still permitting them to exceed the height of their container?

This code snippet sets up a CSS layout where child elements within a container have dynamic heights. The issue arises when the contents of #child-2 spill over into #child-3, even though min-height: 100% is set on #child-2. It seems that #child-2 won't grow past 100% height, regardless of its content size. The challenge here is to handle cases where the content takes up less or more than 100% height without causing overflow. Removing min-height resolves the overflow issue but doesn't cater to scenarios when the content is shorter than 100% height.

Answer №1

If you have a .container that is set as a flexbox, it's recommended to utilize flex properties to control the dimensions of its children. A good approach would be to set the children with flex: 0 0 100%.

To break it down:

/* do not take up extra space */
flex-grow: 0; 

 /* do not shrink when there's insufficient space */
flex-shrink: 0;

 /* designated size for the primary axis of the flexbox (in this scenario, vertical), which is 100% of their parent's height */
flex-basis: 100%;

body {
  height: 100vh;
  margin: 0;
}

.container {
  height: 100%;
  overflow: auto;
  display: flex;
  flex-direction: column;
}

#child-1 {
  flex: 0 0 100%;
  background: rgb(255, 179, 179);
}

#child-2 {
  flex: 0 0 100%;
  background: rgb(135, 135, 232);
  z-index: 2;
}

#child-3 {
  flex: 0 0 100%;
  background: rgb(208, 219, 125);
}

.block {
  height: 200px;
  width: 50px;
  background: rgb(135, 214, 135);
  margin-bottom: 10px;
}
<div class='container'>
  <div id='child-1'>
    <div class='block'></div>
  </div>
  <div id='child-2'>
    <div class='block'></div>
    <div class='block'></div>
    <div class='block'></div>
    <div class='block'></div>
    <div class='block'></div>
    <div class='block'></div>
    <div class='block'></div>
  </div>
  <div id='child-3'></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

Display tabular information using a Bootstrap modal popup

I am facing formatting challenges while working with bootstrap. My goal is to display data in tabular form within a modal-body. Is this feasible? From what I have observed, modals can only be implemented using div tags. http://getbootstrap.com/javascrip ...

Is there a way to locate an element within innerHTML using getElementById?

Is it possible to achieve the following code snippet? <div id="parent"> <iframe id="myFrame" title="HEY!" srcdoc="<div id='inner'>Hello World!</div>"></iframe> </div> v ...

Creating a wide mega dropdown menu using Bootstrap 5.2 for a full-width page

I am attempting to create a Bootstrap 5.2 Mega Menu with a full page-wide background, but I am encountering some challenges. Here is my basic navigation setup: <nav class="navbar navbar-expand"> <div class="container-fluid" ...

Center an image vertically in an AdminLte content-wrapper division

I am currently utilizing an AdminLte template as the framework for my design. In the designated area where I need to insert the main content, it is structured in the following manner: <div class="content-wrapper"> <se ...

Strategies for successfully passing and showcasing the result of a calculation on a fresh view or component within Angular

I have developed a basic calculator application with two main components: Calculator and Result. The Angular router is used to navigate between these components. Now, I am looking for a way to dynamically display the calculation result from the Calculator ...

Functioning on JSFIDDLE, though facing issues on .html files

I'm baffled. The code snippet below functions perfectly on JSFIDDLE, but it's not working properly on my own webpage. The code is supposed to duplicate everything inside the DIV element. To see it in action, check out this link to the working JSF ...

Having difficulty upgrading from BootStrap 3 to BootStrap 4

Need assistance updating from BootStrap 3 to BootStrap 4 on my local server. After trying to update, I ended up with a result that still reflects BootStrap 3. https://i.sstatic.net/Bs9H0.jpg Here's the code snippet I used for linking: <link rel=& ...

What is the best way to prompt users to submit comments with a popup textarea box?

Within my project, I have incorporated a dropdown list: <div class="dropdown"> <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Select subject <span class="caret"></span> </but ...

Top method for expanding a SASS class using an inner selector (sub-class)

When it comes to styling HTML to display messages, here's what I have: <div className="message"> <div className="message_label"> A message </div> </div> To style these messages, I am using the SCSS class bel ...

Input field with a two-row text box

Encountering Issue with IE Every time I use <textarea style="overflow :auto" rows="2" cols="10"/> I notice a scroll bar appearing. Is it possible to achieve the same appearance without the overflow style (which removes the scrollbar) and mimic a ...

Guide to ensuring uniform card height in Bootstrap 4

Struggling with creating cards of equal size. I have been trying different solutions like using h-xxx, align-items-stretch, and Height:100% but still unable to make my cards have the same size or height in this case. This is my current code: <div c ...

Issues with a Java applet, surprisingly straightforward

Having recently embarked on learning Java, I've hit a snag with the tag in HTML. I suspect it may have something to do with the classid attribute and my incorrect usage of it. While I dabbled in HTML, CSS, JavaScript, and PHP in the past, tackling th ...

CSS techniques for aligning content

I am currently working on designing a website template, and I have encountered an issue with positioning three individual columns that contain blocks of content. My objective is to have the first block of content in each column start at the top of their re ...

Seeking particular section of online content in an asynchronous manner

Is there a method for asynchronously requesting a specific portion of a web resource (like the initial 100 bytes) using JavaScript? I believed this could be accomplished through XmlHttpRequest by adjusting its Range header. However, if the server utilizes ...

A limited-width div located on the right side

When I tried to align the div on the right with a max-width of 1200px on a page with an available width of 1600px, it did not work as expected. .csystem { display: flex; max-width: 1200px; flex-direction: column; justify-content: flex-end } < ...

If a checkbox is checked, then the value in the textbox should be multiplied by 0

I'm faced with a challenge involving a non-clickable textbox that is meant to hold a running total. Each time a checkbox is clicked, its value should be added to the total in the textbox. However, I am now struggling to figure out how to perform mult ...

Display scroll bars in the table only when necessary

I am attempting to have scroll bars only appear in a table when necessary, while also showing colors upon hovering over rows and columns. The row and column highlighting on hover is functioning properly, but the scroll bar is persistently visible. What st ...

What is the best way to incorporate a counter into my JavaScript game?

I've created this JavaScript game: window.onload = function() { canv = document.getElementById("gc"); ctx = canv.getContext("2d"); document.addEventListener("keydown", keyPush); setInterval(game, 1000 / 7); } px = py = 10; gs = tc ...

What could be causing the utf8 to not function properly on my php webpage?

Despite searching for numerous solutions, I am still facing issues with UTF8. The "special" characters are not displaying correctly in my browser. Here is the setup I am using: - PHP Designer 8 - Wampserver 3.1.4_x86 - MySQL 5.6 - Navicat10 ...

Mobile Device Experiencing Constant Resizing Issues on Website

I have been working on my website for almost a year now, and I am facing an issue with its responsiveness on mobile devices. Despite using Bootstrap 4 and Ajax to load pages and modals, I can't seem to figure out what's causing the problem. Befor ...