Home > Web Front-end > CSS Tutorial > How to Make a Div Occupy Remaining Parent Height Without Absolute Positioning?

How to Make a Div Occupy Remaining Parent Height Without Absolute Positioning?

Linda Hamilton
Release: 2024-12-06 15:12:18
Original
896 people have browsed it

How to Make a Div Occupy Remaining Parent Height Without Absolute Positioning?

Make Div Occupy Remaining Parent Height Without Absolute Positioning

In HTML/CSS, you often encounter a scenario where a child div needs to occupy the remaining height of its parent container without explicitly setting a specific height. Let's delve into the problem and its possible solutions.

Consider the following HTML/CSS code:

<div>
Copy after login
#container { width: 300px; height: 300px; border:1px solid red;}
#up { background: green; }
#down { background:pink;}
Copy after login

Here, the "down" div should occupy the whitespace below the "up" div.

Solution

There are several methods to achieve this:

Grid:

.container {
  display: grid;
  grid-template-rows: 100px;
}
Copy after login

Flexbox:

.container {
  display: flex;
  flex-direction: column;
}
.container .down {
  flex-grow: 1;
}
Copy after login

Calculation:

.container .up {
  height: 100px;
}
.container .down {
  height: calc(100% - 100px);
}
Copy after login

Overflow:

.container {
  overflow: hidden;
}
.container .down {
  height: 100%;
}
Copy after login

Note:

  • For browsers that support CSS Grid and Flexbox, these solutions provide a more elegant approach.
  • The "Overflow" method can be useful when the parent container has a fixed height. However, it may cause unexpected scrolling behavior.

The above is the detailed content of How to Make a Div Occupy Remaining Parent Height Without Absolute Positioning?. 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