Flex item unable to contain overflowing content despite setting overflow property

My page layout is set up using flexboxes, with a simple structure as shown:

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

The blue div should be 25% in height while the violet div should be 75%. If there are too many lines in the blue div, it should display a scrollbar to maintain its size. However, it seems that at some point the blue div overflows and grows into the violet one. As someone new to flexboxes, I'm unsure why this is happening. Should I reconsider using flexboxes for this layout? Any advice or tips would be greatly appreciated.

Here is the code snippet used (viewed in full screen):

function lines(noLines) {
  var text = "line</br>".repeat(noLines);
  document.getElementById("lower").innerHTML = text;
}
* {
  box-sizing: border-box;
}

.body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

#static1 {
  height: 30px;
  width: 100%;
  background-color: red;
}

#static2 {
  height: 40px;
  width: 100%;
  background-color: orange;
}

#content {
  display: flex;
  flex: 1;
}

#left {
  width: 40%;
  background-color: yellow;
}

#right {
  width: 60%;
  display: flex;
  flex-direction: column;
}

#upper {
  flex: 3 0;
  background-color: violet;
}

#lower {
  flex: 1;
  background-color: blue;
  overflow: auto;
}
<div class="body">
  <div id="static1">Some static div</div>
  <div id="static2">Another static div. Flexbox below fills rest of remaining screen.</div>
  <div id="content">
    <div id="left">
      Left part, fixed width in percentage.</br>
      Click to enter lines into the bottom right:</br>
      <button onclick=lines(20)>Few Lines</button>
      <button onclick=lines(200)>Many Lines</button>
    </div>
    <div id="right">
      <div id="upper">Flexbox with flex=3.</div>
      <div id="lower">Flexbox with flex=1.</div>
    </div>
  </div>
</div>

Answer №1

To ensure that the overflow property functions correctly, it is essential for the container to have a specific height or max-height. Utilizing flex heights such as flex: 1 on .content may not be sufficient.

For the overflow property to take effect, the block-level container must possess either a defined height (height or max-height) or white-space set to nowrap. ~ MDN

Given that you already have knowledge of the primary container's height (100vh) and the initial two rows (30px and 40px), calculating the remaining space using the calc() function becomes straightforward.

function lines(noLines) {
  var text = "line</br>".repeat(noLines);
  document.getElementById("lower").innerHTML = text;
}
.body {
  display: flex;
  flex-direction: column;
  height: 100vh;  /* adjustment */
}

#static1 {
  flex-shrink: 0;  /* disable shrinking */
  height: 30px;
  /* width: 100%; */
  background-color: red;
}

#static2 {
  flex-shrink: 0;  /* disable shrinking */
  height: 40px;
  /* width: 100%; */
  background-color: orange;
}

#content {
  height: calc(100vh - 70px); /* new */
  display: flex;
  /* flex: 1; */ /* may work in some browsers, but not reliable */
}

#left {
  width: 40%;
  background-color: yellow;
}

#right {
  width: 60%;
  display: flex;
  flex-direction: column;
}

#upper {
  flex: 3 0;
  background-color: violet;
}

#lower {
  flex: 1;
  background-color: aqua; /* adjusted for illustration */
  overflow: auto;
}

body {
  margin: 0; /* new; override browser default  */
}

* {
  box-sizing: border-box;
}
<div class="body">
  <div id="static1">Some static div</div>
  <div id="static2">Another static div. Flexbox below fills rest of remaining screen.</div>
  <div id="content">
    <div id="left">
      Left part, fixed width in percentage.<br> Click to enter lines into the bottom right:<br>
      <button onclick=lines(20)>Few Lines</button>
      <button onclick=lines(200)>Many Lines</button>
    </div>
    <div id="right">
      <div id="upper">Flexbox with flex=3.</div>
      <div id="lower">Flexbox with flex=1.</div>
    </div>
  </div>
</div>

Check out the jsFiddle demo here

Answer №2

Hopefully, this solution aligns with what you're looking for. If not, I apologize for the misunderstanding. The issue appears to stem from how you are utilizing flex: 1 & flex: 3 to specify the proportions of the right column without explicitly defining the height of their parent container, specifically #right. Due to the absence of a set height, the box may continually expand as content is added.

Give this adjustment a try and if there are any further questions, feel free to ask.

The modification made primarily focuses on adjusting your CSS by including max-height: calc(100vh - 70px); to the #right div. Additionally, the previous overflow: auto; was changed to overflow-y: scroll;

* {
  box-sizing: border-box;
}

.body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  
}

#static1 {
  height: 30px;
  width: 100%;
  background-color: red;
}

#static2 {
  height: 40px;
  width: 100%;
  background-color: orange;
}

#content {
  display: flex;
  flex: 1;
}

#left {
  width: 40%;
  background-color: yellow;
}

#right {
  width: 60%;
  display: flex;
  flex-direction: column;
  max-height: calc(100vh - 70px);
}

#upper {
  flex: 3;
  height: 75%;
  background-color: violet;
}

#lower {
  flex: 1;
  height: 25%;
  background-color: blue;
  overflow-y: scroll;
} 

Answer №3

To update the header section of CSS, use the following code:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

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

Struggling to create HTML divs with CSS styling

Currently working on creating a 768x240 window design. An example of what I have so far: <div class="global-tab"> <div class="image" src="link"></div> <div class="class1">{{details.name}}</div> <div class="class2" ...

Is there a way to properly close all AudioContexts generated within an iframe?

I've been working on a web application that allows users to write Web Audio code and see a preview of it within an iframe. The issue I'm facing is that every time a user creates an AudioContext, a new context is added on top of the existing one, ...

Converting numerical indexes in an array to an associative array: A step-by-step guide

Struggling with posting dynamic forms currently. Using simple post, looking for a method to retrieve data from entire table. This is the HTML table structure: <form id="importForm" method="post" action="/modules/import/test.php"> <table class="t ...

Looking to refresh the dropdown menu utilized in HTML with Django

I created a Django web application and included a drop down field using the following code: <select id="countrynamew" name="countrynamew" value="{{countrynamew}}"> <option value="Abkhazia">Abkhazia</ ...

Utilizing Jquery tabs for consistent height display

Currently, I am utilizing jquery tabs for showcasing various content. This is what my functions look like: $(function() { $( "#tabs" ).tabs(); }); I attempted to ensure all tabs have the same height by using this approach: var heightStyle = ...

Text within Twitter Bootstrap colliding with each other

I am having trouble creating a large text header for my website using Twitter Bootstrap. The span12 column is not adjusting to the size of the text as I would like it to. When I resize the browser window, instead of the text wrapping onto a new line, the ...

The correct rendering of background image paths is not functioning properly

Currently, I am utilizing Django Compress to streamline the process of using fewer files in their original format, rather than converting them to CSS files. This method has been successful except for a minor issue with translating the paths for background ...

Guide to manually creating and accessing an array of objects in Firebase using Angular

I have successfully generated a dynamic list of cards from an array of objects stored in the user.component.ts file. user.component.ts import { Component, OnInit } from '@angular/core'; import { AuthService } from '../service/auth.service& ...

Using (javascript:) within Href attributes

Recently, I've noticed some people including "javascript:" in the href attribute of an a tag. My question is: what is the purpose of this? Does it guarantee that clicking on the a tag directs the function of the click to JavaScript for handling, rathe ...

What is the best way to transfer a PHP string to JavaScript/JQuery for use in a function?

Within my PHP code, I have the following: $welcome = "Welcome!"; echo '<script type="text/javascript">addName();</script>'; Additionally, in my HTML/script portion: <a id="franBTN"></a> <script type="text/javascript ...

Error: Trying to access the parent node of an undefined property

After incorporating this script into my webpage for the newsletter sign-up process, I aimed to reveal customized content post user subscription. <script> function insertAfter(referenceNode, newNode) { referenceNode.parentNode.insertBefore(newNode ...

The output of VueJs hooks shows a blank refs object first, followed by the referenced elements

Below is the HTML VueJS code sample that I am working with: <div v-for="site in topSites" ref="ts"><a :href="site.url"> ... </div> Update: Here is the full div code: <div v-for="site in topSites& ...

Leverage jquery to adjust the height or width of an element depending on which dimension is larger

I'm a beginner in jquery and html and am currently developing a gallery webpage. The functionality involves setting the selected image as the page background and then scrolling vertically based on the mouse pointer's position. This works well for ...

Need for Jekyll to Incorporate "excerpt" Liquid Tag in YAML Front Matter

My blog is running on Jekyll 3.0.1, using the Kasper theme with some custom modifications. I've cleared all the posts except one to simplify the issue I'm facing. The site builds correctly when I add an "excerpt" tag with a value in the post&apos ...

Screen remains blank as the React webpage fails to load

Hello, I am having issues with my React page not showing up. Can someone please review my code to see if there are any errors? Here is the edited program: index.html <!doctype html> <html lang="en"> <head> <meta charset ...

Guide to creating an intricate formula using the keyup event

Just starting out with coding and I need help making this formula work. GPro = 31 * ((Cr / 8.4)-1.5) * (kA-0.2) * kG I have an online calculator set up to update automatically when the user inputs data (on keyup event). The site has three formulas - Crum ...

What changes should I make to my code in order to incorporate an additional level of submenu to my CSS-based dropdown menu?

Hello and thank you for your help, I currently have some CSS code that is working well for 3 levels of dropdown menus, but I am now in need of it to also support a 4th level. When I try to add the extra level by copying the 3rd level code and making adjus ...

Modify the Link's Color

How can I change the color of a link inside a DIV? The current code isn't working for me. <style type="text/css"> .someDiv { font-size:14px; color:#c62e16; } </style> <div id="someDiv"> <a href="http://www.s ...

How can I align an image in relation to text?

I'm attempting to create an interesting layout where I have text centered with an image on each side, like this | | | text[image] | | [image]text | | | My goal is to have the text centered within t ...

URL to section of website without the navigation bar portion

I am trying to solve a problem with creating a link that will take me to a specific part of the page while taking into account the height of the navbar. The issue is that since the navbar is fixed to the top with a solid color, it ends up covering part of ...