In web development, it’s common to want a div element to fill the remaining space on the screen after other elements have been rendered. This can be particularly useful when creating layouts with headers and footers where you want the main content area to take up all available vertical space.
To achieve this effect without using tables for layout (as tables are meant for tabular data, not layout), we can use CSS Flexbox or CSS Grids, both of which provide powerful ways to control the layout of elements on a web page. In older browsers where these modern layout methods might not be supported, other techniques such as absolute positioning or using the calc()
function in CSS can also be employed.
Using Flexbox
Flexbox is a layout mode that allows you to design complex layouts with less code and more flexibility. To make a div fill the remaining screen space using flexbox:
-
Set up your HTML structure: Ensure your HTML has a container element (e.g.,
div
) wrapping around your header, content, and footer elements.<body> <div class="container"> <header>Header</header> <div class="content">Content here</div> <footer>Footer</footer> </div> </body>
-
Apply Flexbox CSS: Make the container a flex container, set its direction to column (since we’re dealing with vertical space), and ensure it takes up 100% of the viewport height.
html, body { height: 100%; margin: 0; } .container { display: flex; flex-direction: column; height: 100vh; /* Use viewport height */ } header { height: 75px; /* Example fixed height for the header */ } footer { height: 50px; /* Example fixed height for the footer */ } .content { flex-grow: 1; /* Makes the content div take up the remaining space */ }
Using Absolute Positioning
For cases where you might not need or want to use Flexbox (e.g., when dealing with very specific or older browsers), absolute positioning can be a fallback method. However, it requires more manual calculation and adjustment.
-
Set up your HTML structure: Similar to the flexbox example.
<body> <header>Header</header> <div class="content">Content here</div> <footer>Footer</footer> </body>
-
Apply Absolute Positioning CSS:
html, body { height: 100%; margin: 0; } header { position: absolute; top: 0; left: 0; width: 100%; height: 75px; /* Example fixed height for the header */ } footer { position: absolute; bottom: 0; left: 0; width: 100%; height: 50px; /* Example fixed height for the footer */ } .content { position: absolute; top: 75px; /* Header height */ left: 0; right: 0; bottom: 50px; /* Footer height */ overflow-y: auto; /* In case content exceeds available space */ }
Using Calc()
If your header and footer have fixed heights, you can use the calc()
function to set the height of your content div.
-
HTML structure remains similar.
-
Apply Calc() CSS:
html, body { height: 100%; margin: 0; } header { height: 75px; /* Example fixed height for the header */ } footer { height: 50px; /* Example fixed height for the footer */ } .content { height: calc(100vh - 75px - 50px); /* Subtract header and footer heights from viewport height */ }
Each of these methods has its use cases, depending on your project’s requirements and the level of browser support you need to achieve. Flexbox provides a flexible (no pun intended) solution for modern layouts, while absolute positioning and calc()
offer alternatives that can be useful in specific scenarios.