Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make DIV max-height equal to `window height - 100px`

Tags:

css

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?

like image 546
Silver Light Avatar asked Mar 23 '15 13:03

Silver Light


People also ask

How Div height is equal to body height?

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.

What does CSS height 100% do?

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.

What is Max height 100vh?

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.


1 Answers

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 borders, in the calculated height of the element.

Obviously use a relevant selector for your use-case.

References:

  • calc().
  • CSS lengths.
like image 107
David Thomas Avatar answered Sep 18 '22 16:09

David Thomas