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
Susan SarandonOriginal
2024-12-07 05:41:13729browse

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>
#container { width: 300px; height: 300px; border:1px solid red;}
#up { background: green; }
#down { background:pink;}

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;
}

2. Flexbox Layout:

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

3. Calculated Height:

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

4. Overflow Hidden:

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

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!

Statement:
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