HTML PART:
<div class="parent-one">
<div class="child-one">
<p>Some Text...</p>
</div>
</div>
CSS PART:
.parent-one {
min-height: 100vh;
width: 100%
}
.child-one {
// min-height: inherit // This works and sets child height to 100vh
// min-height: 30% // This does not work
// height: 30% // This does not work
}
I have set the parent’s min-height to 100vh and need to set the child’s min-height (or height) to a percentage of the parent’s min-height. From the code example above, I tried 2 ways to achieve this but have been unsuccessful. Can you please tell me how I should go about setting the child’s min-height (and also what if I need to set the child’s height?)
2
Answers
To set the child’s height as a percentage of the parent’s height, you need to ensure that the parent has a specific height value, not just
min-height
. By default, the height of an element is determined by the height of its content unless otherwise specified.Here’s an updated code example that sets the child’s height as a percentage of the parent’s height:
HTML:
CSS:
In this code, the parent element
.parent-one
has its height explicitly set to100vh
. Then, the child element.child-one
usesheight: 30%;
to specify that its height should be 30% of the parent’s height.Note that if you want to use
min-height
instead ofheight
for the child element, you should ensure that the parent element has a specific height value (not justmin-height
) for the percentage calculation to work correctly.or you can try this
Set
.parent-one { display: grid; }
: