Home > Web Front-end > CSS Tutorial > How to Make a Div Occupy the Remaining Height of a Page in HTML and CSS?

How to Make a Div Occupy the Remaining Height of a Page in HTML and CSS?

Barbara Streisand
Release: 2024-11-09 06:47:02
Original
281 people have browsed it

How to Make a Div Occupy the Remaining Height of a Page in HTML and CSS?

How to Make Div Occupy Remaining Height in HTML and CSS

In HTML, it can be a common challenge to achieve a specific layout where one div needs to occupy the remaining height of the page. Consider a situation with two divs:

<div>
Copy after login

The goal is to make #div2 fill the remaining height of the page. Here's a solution using absolute positioning:

#div1 {
    width: 100%;
    height: 50px;
    background-color: red; /* Development Only */
}
#div2 {
    width: 100%;
    position: absolute;
    top: 50px;
    bottom: 0;
    background-color: blue; /* Development Only */
}
Copy after login

In this code, #div1 is given a fixed height of 50px, while #div2 is positioned absolutely and made to occupy the remaining space. The top property is set to 50px, which is the height of #div1. The bottom property is set to 0, indicating that #div2 should be anchored to the bottom of its parent, which is the page itself in this case. By doing so, #div2 will dynamically stretch to take up the remaining height of the page.

The above is the detailed content of How to Make a Div Occupy the Remaining Height of a Page in HTML and CSS?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template