How To Make A Div Fill The Remaining Screen Height?

-

Problem: Filling Remaining Screen Height with a Div

Creating a div that fills the remaining screen height can be hard in web design. This issue often occurs when trying to make a full-page layout or when working with dynamic content. The goal is to make a div element grow vertically to fill any unused space on the screen.

CSS Solution: Flexbox Approach

Flexbox for Layout Control

Flexbox is a CSS layout model that helps distribute space and align content in a container. It's useful for creating layouts that adapt to different screen sizes and content amounts.

Flexbox helps solve the remaining height problem by:

  • Centering content vertically
  • Distributing space between or around items
  • Sizing child elements flexibly
  • Changing the visual order of elements

Most modern web browsers support Flexbox, including Chrome, Firefox, Safari, and Edge. Internet Explorer 11 has partial support, while older versions don't support it. For older browsers, you may need to use other methods or include polyfills.

To use Flexbox, apply display: flex to the parent container. This enables the flex context for all its direct children. You can then use flex properties to control how these child elements behave within the flex container.

Example: Vertical Centering with Flexbox

.container {
  display: flex;
  align-items: center;
  height: 100vh;
}

.content {
  width: 100%;
}

This example shows how to center content vertically within a full-height container using Flexbox. The container is set to display: flex and align-items: center, while the content is given a width of 100% to fill the horizontal space.

Implementing the Flexbox Solution

Setting Up the HTML Structure

To create a layout where a div fills the remaining screen height, you need a simple HTML structure. Here's a basic setup:

<div class="container">
  <header class="header">Header Content</header>
  <main class="main-content">Main Content</main>
  <footer class="footer">Footer Content</footer>
</div>

This structure includes a container div that holds three main elements: a header, a main content area, and a footer. The main-content div will fill the remaining screen height.

Tip: Add Semantic Elements

Use semantic HTML5 elements like <nav>, <article>, or <section> within your main-content area to improve accessibility and SEO. For example:

<main class="main-content">
  <nav>Navigation menu</nav>
  <article>Main article content</article>
  <aside>Sidebar content</aside>
</main>

Applying Flexbox CSS Properties

To make this layout work with Flexbox, apply these CSS properties:

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

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

.header, .footer {
  flex-shrink: 0;
}

.main-content {
  flex-grow: 1;
}

Here are the key Flexbox properties:

  1. display: flex; on the container enables Flexbox layout.
  2. flex-direction: column; stacks the child elements vertically.
  3. min-height: 100vh; makes the container take up at least the full viewport height.
  4. flex-shrink: 0; on header and footer prevents them from shrinking.
  5. flex-grow: 1; on main-content allows it to grow and fill the remaining space.

This CSS structure creates a flexible layout where the main content area expands to fill any available space between the header and footer, regardless of their sizes or the amount of content.

Alternative Methods

Using CSS Grid for Full-Height Layouts

CSS Grid is a layout system that can create full-height layouts. It lets you define rows and columns, giving you control over two-dimensional layouts.

To use CSS Grid for a full-height layout:

body {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-height: 100vh;
}

header, footer {
  grid-row: span 1;
}

main {
  grid-row: 2 / 3;
}

This CSS creates a three-row layout where the middle row (main content) fills available space.

CSS Grid compared to Flexbox:

  • Gives more control over horizontal and vertical alignment
  • Works well for layouts with multiple rows and columns
  • Has less browser support than Flexbox

Tip: Responsive Grid Layout

Use CSS Grid's minmax() function to create responsive layouts that adapt to different screen sizes:

body {
  display: grid;
  grid-template-columns: minmax(200px, 1fr) minmax(300px, 3fr);
  grid-template-rows: auto 1fr auto;
  min-height: 100vh;
}

This creates a two-column layout with a flexible sidebar and main content area.

JavaScript-Based Solutions

While CSS solutions are often better, JavaScript can also create full-height layouts:

function adjustHeight() {
  const header = document.querySelector('header').offsetHeight;
  const footer = document.querySelector('footer').offsetHeight;
  const windowHeight = window.innerHeight;
  const main = document.querySelector('main');
  main.style.height = `${windowHeight - header - footer}px`;
}

window.addEventListener('resize', adjustHeight);
adjustHeight();

This script calculates the available space and sets the main content height.

Pros of using JavaScript:

  • Works in older browsers that don't support modern CSS
  • Allows changes based on content or user actions

Cons of using JavaScript:

  • Can cause layout shifts if not handled well
  • Adds complexity to the code
  • May affect performance, especially on slower devices