Creating a CSS layout that expands a container to fill the entire height of its grandparent element

Currently, I am developing a web application and facing challenges with the layout.

The expected design includes 3 significant components:

  • Article Header
  • Article Content
  • Article Footer

Utilizing CSS Flexbox makes it relatively simple to implement this design.

html, body, article {
  height: 100%;
  margin: 0;
  color: white;
}

.article {
  display: flex;
  flex-direction: column;
}

  .article-header {
    background-color: orange;
  }
  
  .article-footer {
    background-color: green;
  }
  
  .article-content {
    flex-grow: 1;
    background-color: CornflowerBlue;  
  }
 
<article class="article">
  <header class="article-header">Header</header>
  <div class="article-content">Article Content</div>
  <footer class="article-footer">Footer</footer>
</article>

However, when dealing with actual HTML elements in the application, the nesting can become much more complex and at times uncontrollable (especially when using third-party components or transclusion within Angular/React).

As a result, the structure of the HTML might look like this:

html, body, article {
  height: 100%;
  margin: 0;
}

.article {
  display: flex;
  flex-direction: column;
}

  .article-header {
    background-color: orange;
  }
  
  .article-footer {
    background-color: green;
  }

  .article-wrapper {
    flex-grow: 1;
  }
 
  .article-content {
    flex-grow: 1;
    background-color: CornflowerBlue;  
  }
 
<article class="article">
  <header class="article-header">Header</header>
  <div class="article-wrapper">
    <div>
      <div>
        <div class="article-content">
           Article Content.
           Can I fit all available height?
        </div>
       </div>
      </div>
    </div>
  <footer class="article-footer">Footer</footer>
</article>

In such cases, the only way to ensure that .article-content spans the entire available height is by applying these styles to each container from .article-wrapper down to article-content:

display: flex;
flex-direction: column;
flex-grow: 1;

Nevertheless, this approach can be difficult due to deep nesting or dynamically generated container elements by third-party components.

If you have any suggestions on better methods to achieve the desired design, please share your insights.

Thank you for your assistance!

Answer №1

In the realm of flex containers, it is crucial to understand that only the immediate children become flex items and can therefore have the flex-grow: 1 property applied to them.

To achieve this functionality, you must ensure that both the article-wrapper and all its nested div elements down to article-content are set as flex properties. All three levels need to be designated as both a flex container and a flex item.

.article-wrapper,
.article-wrapper > div,
.article-wrapper > div > div {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
}

To simplify, think of .article-wrapper as the flex container for the first div, where the first div acts as a flex item in relation to .article-wrapper but also as a flex container for the subsequent div, and so forth.

Revised Instructions

Additionally, based on the image provided, the article-content should allow scrolling when its content exceeds its height. To enable this, I included overflow: auto in the wrapper-content rule.

Snippet Example

html, body, article {
  height: 100%;
  margin: 0;
}

.article {
  display: flex;
  flex-direction: column;
}

  .article-header {
    background-color: orange;
  }
  
  .article-footer {
    background-color: green;
  }

  .article-wrapper {
     overflow: auto;
  }

  .article-wrapper,
  .article-wrapper > div,
  .article-wrapper > div > div {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
  }

  .article-content {
    flex-grow: 1;
    background-color: CornflowerBlue;  
  }
<article class="article">
   <header class="article-header">Header</header>
   <div class="article-wrapper">
    <div>
      <div>
        <div class="article-content">
           Article Content.
           Can I fit all available height?
           <br><br>
           Scroll overflowed content<br><br>
           Scroll overflowed content<br><br>
           ... (content continues)
        </div>
       </div>
      </div>
    </div>
   <footer class="article-footer">Footer</footer>
</article>

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

Is there a way to conceal the scrollbar in the mobile view (on a mobile device or emulator) in a React application without disrupting the scrolling functionality?

I am looking to conceal the scrollbar on mobile devices throughout my app while still allowing users to scroll. I have attempted to hide the scrollbar using CSS properties like scrollbar, scrollbar-thumb, scrollbar-thumb:hover, and scrollbar-track. This ...

Utilizing CSS to create dynamic 3D cube transformations

Exploring the realm of CSS 3D transforms to showcase cubes and cuboids featuring cross-sections has been my current venture. In this endeavor, I'm aiming at compatibility with Chrome, Firefox, and MSIE 11. It has come to my attention that in order to ...

Updating a dropdown menu dynamically using PHP PDO according to the previous selection

Although this question has been answered previously, I am posing it again for two specific reasons. Firstly, my search for resources that utilize PDO has been fruitless. Secondly, the existing resources I have come across are devoid of comments or explanat ...

The div element is finally loading properly after multiple clicks

I need some assistance with loading dynamic divs in Angular. I have created a button that adds new divs each time it is clicked in a specific area. However, once these new divs are added, they appear incorrectly: https://i.sstatic.net/sAE6q.png After add ...

What is the best way to reveal CSS files downloaded via npm?

I'm currently working on a Sass/CSS library that I have hosted on npm. I am exploring ways to make it easier for clients to access it, such as using: @import 'library_name' Instead of the less appealing option: @import 'node-modules/ ...

Navigation problems resolved: The fixed-top class causes issues with navbar-right and obscures the logo

I have implemented a bootstrap Nav on this page To achieve a sticky top navigation, I used the fixed-top class. However, this caused the navbar to align left instead of right as intended. It also ended up covering my logo, which was originally aligned to ...

Expanding and collapsing accordion using jQuery to handle dynamic data

I am trying to implement functionality where I have dynamic data and when I click on a button, an accordion opens and closes. The challenge is that only one accordion should be open at a time, closing any previously opened accordions. Currently, my scrip ...

Activate a JavaScript mouse event outside of an element

https://jsfiddle.net/f8L300ug/3/ Attempting to create a drag-to-resize feature, inspired by the provided example. One challenge encountered is that when the mouse is released outside of the <body>, the mouseup event does not trigger as expected. Th ...

Remove the background from the image to make it transparent

Is it possible to upload an image with a white background and then use HTML Canvas/CSS to turn that white background transparent? I'm looking for a way to automatically remove the white background from any image, whether it's a pair of shoes or a ...

What is the reason for elements such as "if" and "else" not being visually

I am currently developing a browser-based code editor with a unique feature. Task: The objective is to highlight specific keywords like if, else, for... when they are entered into the editor. textarea.addEventListener("input", function() { ...

Opacity effect on input text when it exceeds the input boundaries

I'm having an issue: I need to create inputs with different states, including when the text is longer than the input itself. In this case, the extra text should fade out with a gradient transparency effect: https://i.sstatic.net/r5qQu.jpg Here is my ...

What is the effect of SEO on the use of image width and height attributes for a responsive website

Is it true that setting inline width and height for all images is beneficial for SEO and can improve site loading speed? Here's an example: <img src="http://www.example.com/images/free-size.jpg" width="200" height="400" alt="random image" /> E ...

How to use HTML and CSS to resize images from a tileset

I need to resize an image that I am pulling from a tile set, originally sized at 50px x 50px. Is there a way to enlarge this image without adjusting the background size property? Perhaps by placing the image into a div and resizing the div instead? img.ti ...

Tips for adjusting image size to take up only half of the screen in NextJS

Struggling to resize an image to fit only 50% of the screen in NextJS? The Image component provided by NextJS comes with its own inline styling, making it tricky to customize. Currently, I attempt to style the image by wrapping the Image component in a spa ...

Instructions for compiling node-sass within the present directory for specific files

In my project, the directory structure looks like this: - folder1 - styles1.scss - folder2 - styles2.scss I want to utilize node-sass through the command line to generate the following output: - folder1 - styles1.scss - styles1.css - folder2 ...

Implementing AngularJS with the use of the "bind(this)"

Lately, I have made the switch to using "this" in the controllers and controllerAs in ngRoute and Directives instead of accessing $scope directly. While I do appreciate the aesthetic appeal of the code, I find myself needing to manually bind "this" to each ...

Scraping content using xpath in a complex html layout

I have a large amount of HTML code that I need to process using XPath. There are two different ways in which the text "The Text" can appear: <div> The Text </div> <!-- OR --> <div> <span>The Text</span> </div> ...

What is the best way to ensure that the question text will wrap onto a new line if it becomes too lengthy

I am currently working on developing ASP.NET MVC applications. However, I am facing a design issue with the HTML, CSS, and Bootstrap elements on one of my pages. The problem arises when Question 8 exceeds the border and does not wrap to a new line as inte ...

The values in the Jquery drop-down menu are not updating when selected, causing the button link to display the

Struggling to decode my code here. Every time a user chooses a corporation from the dropdown, it displays the name, phone number, email, and cc correctly but the "mailto:" link button is missing. I want to add a button that generates a mailto link using th ...

Htaccess rules not translating directory-style URLs into query-style URLs

I am currently working on a basic htaccess file to convert directory-style URLs to query-style. For example: a. /public_html/main/folder1/folder2 ... etc b. /public_html/main.php?1=folder1&1=folder2 Despite my efforts with various regex and rewrite r ...