Home > Web Front-end > CSS Tutorial > How to Make a Div Element Occupy Remaining Page Height While Maintaining a Fixed Height for Another Div?

How to Make a Div Element Occupy Remaining Page Height While Maintaining a Fixed Height for Another Div?

Linda Hamilton
Release: 2024-11-08 08:53:02
Original
838 people have browsed it

How to Make a Div Element Occupy Remaining Page Height While Maintaining a Fixed Height for Another Div?

Maintaining Consistent Height Distribution with Div Elements

Ensuring a proportional height distribution among multiple div elements can be accomplished through advanced CSS techniques. Consider the following scenario:


</p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><div>
Copy after login
Copy after login


The goal is to make div2 occupy the remaining height of the page, while allowing div1 to maintain its fixed height.

Solution: Absolute Positioning

By using the following CSS styles, you can achieve the desired behavior:


<div>

<div>
Copy after login
Copy after login

div1{

width: 100%;
height: 50px;
background-color:red;/*Development Only*/
Copy after login

}

div2{

width: 100%;
position: absolute;
top: 50px;
bottom: 0;
background-color:blue;/*Development Only*/
Copy after login

}

<div>

Copy after login


Explanation

By applying absolute positioning to div2, it is removed from the normal flow of the page and instead positioned based on the specified top and bottom values. The top property sets the distance from the top edge of div1, while the bottom property ensures that the element extends to the bottom of the page. The width of div2 is set to 100% to span the entire width of the page.

This technique allows you to dynamically adjust the height of div2 based on the height of div1 and the overall page height, ensuring a consistent and responsive layout.

The above is the detailed content of How to Make a Div Element Occupy Remaining Page Height While Maintaining a Fixed Height for Another Div?. For more information, please follow other related articles on the PHP Chinese website!

Previous article:How Can I Display Text on Image Hover Without Using Tooltips? Next article:Why does `querySelector` only select the first element in the `.weekdays` class?
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
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template