What is the best way to ensure an element fills the entire height of its container?

Is there a way to handle the overflow of a div when it exceeds its container's size without specifying the height?

Requirements:

  • .Wrap must adjust in height and should not have a fixed height.
  • .Head needs to have a fixed height.
  • .Body should become scrollable if its height surpasses that of its container (.Wrap)

The goal is to make .Body expand to fit .Wrap, triggering an overflow when necessary.

CURRENT CSS

.Wrap{
    position:fixed;
    top:10%;
    left:10%;
    display:flex wrap;
    align-items:stretch;
    max-height:50%;
    max-width:50%;
    overflow:hidden;
}
.Head{
    height:50px;
    width:100%;
}
.Body{
    overflow:auto;
}

http://jsfiddle.net/x6TaR/2

Answer №1

To prevent flexbox from forcibly collapsing, try setting a specific min-height for the .Head element. Also, adjust the flex-flow property of the parent container .Wrap to column nowrap:

.Wrap{
    position:fixed;
    top:10%;
    left:10%;
    display:flex;
    flex-flow: column nowrap;
    align-items:stretch;
    background:#ddd;
    max-height:50%;
    max-width:50%;
    padding:10px;
    overflow:hidden;
}
.Head{
    background:#e00;
    height:50px;
    min-height: 50px;
    width:100%;
}

View Example Here

Answer №2

It is important for the body to have a specified height. Take a look at this example.

.Body{
    background:#555;
    overflow:auto;
    height: 150px;
}

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

Designing rows and columns using Angular CSS within an ngFor loop

Is there a way to align items in a row or column based on conditions within an *ngFor loop? I want the input type to display on the next line if it is textarea, and on the same line otherwise. Check out this Stackblitz Demo for dynamically generated HTML ...

Creating a Custom Alert Box in Javascript with Image Alignment

I have just finished creating a custom alert box using Javascript, complete with text and images. However, I am facing an issue with alignment. The output is not as expected. I am trying to display the correct mark and text on the same line. Can someone ...

Utilize the 'Save and add another' feature within a bootstrap modal

Hello everyone, this is my first time seeking assistance on this platform. If any additional information is required, please do not hesitate to ask. Currently, I am working with JavaScript in combination with the handlebars framework. Within a bootstrap ...

When updating the HTML content, JavaScript is outputting the code as text instead of running it

I am encountering an issue with adding the body content of a JSON file, which contains HTML code, to my existing HTML. Instead of executing the code, it appears as text within paragraph or header elements. I have searched through various discussions but ha ...

How can I implement a hover effect without triggering a click event?

My structure is pretty basic, here it is: .container { height: 100vh; width: 100%; pointer-events: none; } .click-layer { background-color: #ccc; position: absolute; top: 0; left: 0; right: 0; bottom: 0; pointer-events: auto; } .box { ...

trouble centering elements in bootstrap

It appears that when I try to center this image, it is slightly off-center to the right on the page. What could be causing this? It seems like there might be a margin added to the left of the image. <head> <meta charset="utf-8"> ...

Completing two tasks with a single form submission and a single button press

I'm currently working on a script that inserts data into a database using a form. I'm trying to figure out how to trigger another action in a different section after the insertion. Any insights or tips would be greatly appreciated! ...

The content div in CSS is causing the menu div to be displaced

I have encountered a difficulty in describing the issue at hand as I am still in the learning phase and consider myself a beginner. Within my container div, there are two other divs - one for the menu and another for the content. Both of these divs float ...

How can I enable two-finger scrolling on mobile web browsers?

I am currently tackling a project that involves dragging operations on a canvas, making scrolling with a single finger impractical. My goal is to enable scrolling using two fingers instead. After testing different touch-action values, I discovered that pi ...

Struggling to make AngularJS routes function properly

After starting from scratch, I rebuilt it with freshly downloaded angularJS (version 1.5.8). I simply placed both angular.js and angular-route.js in the library folder following this setup: https://gyazo.com/311679bf07249109671d621bc89d2d52 Here is how in ...

Ensuring Mobile Compatibility: Maintaining Image Ratio Responsively on HTML with Minimum Height

I'm having trouble with inserting a full-width hero image. The issue arises on mobile devices where the height of the image appears too short based on its original proportions. On large screens: https://i.sstatic.net/U1tXC.jpg On mobile screens: h ...

Is CDATA insertion automatic in JavaScript within Yii framework?

Currently I am working with Yii and have noticed that whenever I insert any JavaScript code, it is automatically encapsulated in CDATA. I am curious as to why this is happening. Will there be any issues if I were to remove the CDATA tags, considering that ...

Enhance your Zara shopping experience with the dynamic image zoom-in

I am currently exploring ways to replicate the image zoom-in feature seen on Zara's product pages. To see this effect in action, visit their site: Upon clicking on the image, it opens up in a popup window where it is enlarged to fit the entire screen ...

button click event blocked due to unforeseen circumstances

Recently, I've been attempting to change the CSS properties of a div by triggering a click event. However, no matter what I do, it doesn't seem to be working and it's starting to frustrate me. Can anyone shed some light on why this might be ...

I'm having trouble extracting text from the HTML code for an unknown reason

My code seems to be working well until I attempt to use the .text function, resulting in an error message stating "list' object has no attribute 'text". Interestingly, when I apply .text to a single element within the same list, it works without ...

Ways to retrieve the href attribute value from an element in Python/Selenium even when there is no explicit href attribute present

I am facing an issue with retrieving the URL link from an element using the get_attribute('href') method. It returns a null value, as if the element does not have a href attribute. webdriver.find_element_by_xpath('//*[@id="__next"] ...

I came across a wlwmanifest.xml file while browsing through the source directory of my website

<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="../preload/wlwmanifest.xml"> I am curious about the purpose of including this code in my website. If I were to delete this file, how would it impact the f ...

What is the best way to add a CSS class to every <TD> element on an HTML page with just two clicks?

I am in the process of developing a scheduler web tool. Essentially, when a user clicks on two dates, all dates between those two selections will be automatically chosen. Here is an example to illustrate this functionality: If the user clicks on day 5: h ...

How can you enable fullscreen for a featherlight iFrame?

I have implemented Featherlight to display an iframe within a popup modal on my website. If you click the iframe button, you can see a demo of how it works. One issue I am facing is that the generated iframe tag by Featherlight does not include allowfulls ...

What is the role of the CSS URL property in web design?

Occasionally, I enjoy the challenge of "reverse engineering" websites, especially those with a complex design. By dissecting these sites, I aim to gain insight into the techniques employed by other developers. Recently, my curiosity led me to analyze the ...