There is a way to set max-height
in %
, but is here any way to set DIV max-height, so it would be 100px smaller than window height with only CSS?
It must not be a fixed layout, user must be able to scroll page vertically, but DIV always should be resized to window height - 100px
. Is this possible, or will I have to use JS for that?
Answer: Set the 100% height for parents too And we all know that the default value of the height property is auto , so if we also set the height of <body> and <html> elements to 100%, the resulting height of the container div becomes equal the 100% height of the browser window.
height: 100% gives the element 100% height of its parent container. height: auto means the element height will depend upon the height of its children.
A height of 100vh corresponds to the maximum possible viewport height. Since the initial viewport height is smaller, the body element with a min-height of 100vh initially exceeds the viewport height regardless of its content.
Yes:
#specificElement { height: calc(100vh - 100px); box-sizing: border-box; }
This uses the CSS calc()
function to subtract 100px
from 100vh
(1vh
being one percent of the view-port's height) and uses the result as the value of the height
property.
The box-sizing
forces the browser to include padding
, and border
s, in the calculated height
of the element.
Obviously use a relevant selector for your use-case.
References:
calc()
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With