In CSS, percentage-based sizes are relative to the parent of the element. The parent must also have a fixed (read: non-percentage) size.
In this case, body has no defined height, and so you get this:
body {
background: beige;
}
section {
display: inline;
background: lightgray;
}
p {
width: 50%;
height: 100%;
background: cyan;
}
<body>
<section>
<p></p>
</section>
</body>
You can test this by giving body
or section
a fixed size and seeing the result:
body {
background: beige;
height: 250px;
}
section {
display: inline;
background: lightgray;
}
p {
width: 50%;
height: 100%;
background: cyan;
}
<body>
<section>
<p></p>
</section>
</body>
The cyan box now takes up 100% of the value of the height
of body
.
One of the workarounds that you will often see used for this scenario is that people will give the top-level container a size relative to the viewport. If we give body
, say, height: 50vh, and then give p
a height
of 100%, even though viewport height
is a scalar/percentage based unit, it will resolve as you would desire.
body {
background: beige;
height: 50vh;
}
section {
display: inline;
background: lightgray;
}
p {
width: 50%;
height: 100%;
background: cyan;
}
<body>
<section>
<p></p>
</section>
</body>
However, because you have content inside of your <p>
element, a percentage-based height
would not resolve to zero. I believe the comment in the original example was made to illustrate the fact that a percentage-based height
would not increase the height beyond the native size of the <p>
and its contents, but could have been phrased slightly better to say something along these lines.