Achieving full height for the content of the bottom element within a flexbox container

When creating a flexbox with 2 children in column flow and applying flex-grow 1 to the second child, it expands to fill the flexbox.

(Note: I avoided mentioning Safari support in this example, so please use Chrome or Firefox)

* {
  box-sizing: border-box;
}
html, body {
  margin: 0;
  width: 100%;
  height: 100%;
  color: white;
}
#outer {
  display: flex;
  flex-flow: column;
  height: 100%;
}
#top { 
  background-color: red;
}
#bottom {
  background-color: blue;
  flex: 1 0 auto;
}
<div id="outer">
  <div id="top">top</div>
  <div id="bottom">bottom (blue)</div>
</div>
  

However, when adding a child #inside inside #bottom and setting its height to 100%, it doesn't increase its height to match even though the flexbox has stretched #bottom.

Additional CSS:

#inside {
  background-color: green;
  height: 100%;
}

HTML:

<div id="outer">
  <div id="top">top</div>
  <div id="bottom">
    <div id="inside">inside</div>  <!- added ->
  </div>
</div>

* {
  box-sizing: border-box;
}
html, body {
  margin: 0;
  width: 100%;
  height: 100%;
  color: white;
}
#outer {
  display: flex;
  flex-flow: column;
  height: 100%;
}
#top { 
  background-color: red;
}
#bottom {
  background-color: blue;
  flex: 1 0 auto;
}
#inside {
  background-color: green;
  height: 100%;
}
<div id="outer">
  <div id="top">top</div>
  <div id="bottom">
    <div id="inside">inside (green)</div>
  </div>
</div>

To make #bottom stretch to fit the flexbox while also getting the child #inside to be 100% height of its container #bottom, how can this be achieved?

Answer №1

When working with Flex, a quirk to keep in mind is setting the height to 0.

To address this, modify the #bottom rule by adding height: 0;

In order for the inside element to function properly, it's necessary to set its position to "absolute" and also include a position:relative for the bottom container.

Update

If you prefer not using absolute positioning, adjust these two css rules as follows:

(Keep in mind that this may reintroduce the original issue if a new inner div is introduced)

#bottom {
  position: relative;
  background-color: blue;
  flex: 1 0 auto;
  height: 0;
  display: flex;
}
#inside {
  background-color: green;
  flex: 1 0 auto;
}

Sample utilizing "position: absolute"

* {
  box-sizing: border-box;
}
html, body {
  margin: 0;
  width: 100%;
  height: 100%;
  color: white;
}
#outer {
  display: flex;
  flex-flow: column;
  height: 100%;
}
#top { 
  background-color: red;
}
#bottom {
  position: relative;
  background-color: blue;
  flex: 1 0 auto;
  height: 0;
}
#inside {
  background-color: green;
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
}
<div id="outer">
  <div id="top">top</div>
  <div id="bottom">
    <div id="inside">inside (would not scroll if working)</div>
  </div>
</div>

Answer №2

Is there a way to ensure that the #bottom element adjusts to fit within the flexbox, while also making sure its child element #inside fills the entire height of its parent container #bottom?

You can achieve this by adding just two lines of code to your CSS.

CSS

#bottom {
    background-color: blue;
    flex: 1 0 auto;
    display: flex; /* NEW */
}

#inside {
    flex: 1; /* NEW */
    background-color: green;
}

DEMO: http://jsfiddle.net/wf2L8dse/

Explanation:

Your layout consists of a flex container (#outer) with two flex items (#top and #bottom).

The container #outer is set to align in columns.

#bottom has a flex: 1 property, allowing it to take up all available vertical space in the container.

A new element (#inside) is nested inside #bottom and needs to fill the same height as its parent.

Solution:

To address this issue, convert #bottom into a nested flexbox. This will activate default flex behavior.

One of those behaviors is align-items: stretch, which instructs flex items (like #inside) to expand to the full height of their container. Since the default flex-direction is row, it expands vertically in this case.

Next, apply flex: 1 (or flex-grow: 1) to #inside so it spans the full width of the container.


Regarding the height: 100% issue

It seems like there may not be any problem with your current code. You have correctly applied height: 100% to #inside and ensured that percentage heights are supported by specifying heights for all parent elements including body and the root element html.

If you want to prevent a vertical scrollbar from appearing on the browser window, you might consider adding overflow: hidden to the body element.

DEMO: http://jsfiddle.net/wf2L8dse/1/

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

Use Ajax to dynamically update the href attribute of an ID

I have a page with songs playing in a small audio player, and the songs change every 1-2 minutes or when I choose to (like a radio). Users can vote on each song using the following format: <span class='up'><a href="" class="vote" id="&l ...

Trouble with Jquery UI and HTML5 datepicker - date formatting issue persisting

I am creating a date picker that is compatible with browsers following the html5 specification, while also providing a jquery ui date picker for those that do not support it. Currently, I have: <input type="date" id="TargetDate1" name="TargetDate1" cl ...

What is the best way to add an image using a div in React?

I need assistance getting an image to display as the background in a div element, but it's not working. Below is my code: <div style='background: url("https://images.unsplash.com/photo-1426604966848-d7adac402bff?ixid=M ...

Is it possible to retrieve the value of this input field within a function call in HTML?

When working with an HTML input field, I encountered the need to limit manual input to 100. Although I already had minimum and maximum values set up for spinner functionality (up and down arrows), I wanted to create a reusable function instead of using inl ...

Tips for creating multiple row tabs in Material UI Tabs

Currently, I have close to 30 tabs housed within Material UI Tabs. In order for the user to view all tabs, they must scroll two times. I am seeking a solution to display the tabs in two rows with a scroll, as opposed to one row with scroll. This would al ...

"Can anyone provide guidance on how to use Twig to read from a JSON file

I currently have a basic PHP file set up as follows: <?php // Loading our autoloader require_once __DIR__.'/vendor/autoload.php'; // Setting the location of our Twig templates $loader = new Twig_Loader_Filesystem(__DIR__.'/views& ...

Blurry Background CSS Effect

Is there a way to create a background image with the same blurry effect as seen in the picture provided? It appears to involve multiple filters rather than just a basic blur. I attempted using the blur Effect, but it didn't achieve the desired result ...

Struggling to display the default value on a <select> element using the defaultValue prop in React

My objective is to create a dropdown menu that lists all the US state names. If there is a stateName in this.props (obtained from the redux store with values like NY, KY, or an empty string), I want the dropdown to display it by default. Otherwise, I want ...

Tips on saving Firebase Storage image url in Firebase database?

How do I store the URL of an image uploaded to Firebase Storage in Firebase Database? When executing the code below, I encounter the following error: Uncaught (in promise) FirebaseError: Function DocumentReference.set() called with invalid data. Unsuppor ...

Arranging an array of objects from the Fetch API into rows and columns in Bootstrap 4 by utilizing a foreach loop to iterate through the array

I am currently retrieving data from an external API, which returns an array of objects. Each object contains details of characters from the Harry Potter movies, and there are a total of 50 objects in the array. I am using a foreach loop to iterate through ...

Shifting the position of an HTML page to one direction

I'm currently working on adding a sidebar to my Twitter Bootstrap 3 project. The goal is to have a fixed positioned nav nav-pills nav-stacked show up on the left side of the page when a button is clicked. I've set its z-index to 1000 so it appear ...

Cannot get the input padding override to work in MUI autocomplete

I've been attempting to modify the padding within the MUI autocomplete component, but unfortunately, it doesn't appear to be functioning correctly const icon = <CheckBoxOutlineBlankIcon fontSize="small" />; const checkedIcon = ...

The Jquery ajax request fails to function properly once the webpage has been published live

We've implemented a basic jQuery ajax call. A "more" button triggers a call to another PHP file when clicked. While it worked fine on my local server and initially on the live server, it suddenly stopped working live. The code functions perfectly o ...

Is it advisable to blend Angular Material with Bootstrap?

I'm brand new to Angular Material and it seems to have its own intricate API. Coming from using Bootstrap, I'm accustomed to utilizing its grid system with classes like row, containers, cols, etc. I'm curious if it's considered a good o ...

Creating 3D spheres that appear on a webpage with CSS3 and HTML5

Currently working on a web app and planning to enhance the login page by incorporating a sequence of filled spheres in the order 1 -> 2 -> 3 -> 4 -> 5, as illustrated below. My experience so far has been mainly with plain HTML and minimal CSS, ...

React is producing a collection of <td>'s

My React code is very straightforward and it runs smoothly: function Columns(){ return ( <React.Fragment> <li>Hello</li> <li>World</li> </React.Fragment> ); } function Example(){ ...

Adjusting the size of a Checkbox with CSS

Adjust the width of the checkbox for the call field to 25 pixels, ensuring it is displayed only when the left margin is clear. I'm having trouble figuring this out, even though it's probably something simple again. I double-checked that my style ...

Is there a way to dynamically change the title of a tab when new content is added to a minimized page?

Is anyone familiar with how websites like Facebook and Buzzfeed update their tab titles when there are multiple tabs open? I have noticed that sometimes they add a "(1)" or "(2)" to indicate changes on the page. Do they use JavaScript to achieve this eff ...

Leveraging XPATH to pinpoint objects based on their RGB background color

I have a table with multiple divs inside it, many of which are identical except for their background colors! Is there a way to select a specific div based solely on its background color? Here is an example of the HTML structure: <div class="fc-event fc ...

Tips for making a separate countdown clock for every element in a list?

I am facing an issue in my React application where I need to display a list of orders to the user for only 30 seconds each. Each order has a duration property set to 30 seconds: [ { ..., ..., duration: 30 }, { ..., ..., durati ...