Home > Web Front-end > CSS Tutorial > How Can I Make a Child Div Occupy the Remaining Height of Its Parent?

How Can I Make a Child Div Occupy the Remaining Height of Its Parent?

Susan Sarandon
Release: 2024-12-07 05:41:13
Original
785 people have browsed it

How Can I Make a Child Div Occupy the Remaining Height of Its Parent?

How to Make a Div Occupy Parent's Remaining Height

Consider the following HTML and 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, we have a container div with two child elements. The first child (#up) has a given height, while the second (#down) does not. How can we make #down occupy the remaining height of the container div?

Solution

Several methods can achieve this, depending on browser compatibility and whether #up has a fixed height.

Options:

1. Grid Layout:

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

2. Flexbox Layout:

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

3. Calculated Height:

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

4. Overflow Hidden:

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

The choice of method depends on the specific requirements and browser support. Note that CSS now includes the max-content property value for sizing, which may be suitable in certain scenarios.

The above is the detailed content of How Can I Make a Child Div Occupy the Remaining Height of Its Parent?. 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