Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify max-height css property to Screen size

I am trying with the following style:

.scrollDiv {     height:auto;     max-height:100%;     overflow:auto;    } 

My Requirement is:

  1. max-height of div is equal to screen height
  2. If the content in the div exceeds screen size, then scroll bars should come in div.
like image 334
Butchi Reddy Velagala Avatar asked Dec 10 '13 07:12

Butchi Reddy Velagala


People also ask

How do I limit the size of my screen in CSS?

Using width, max-width and margin: auto; Then, you can set the margins to auto, to horizontally center the element within its container. The element will take up the specified width, and the remaining space will be split equally between the two margins: This <div> element has a width of 500px, and margin set to auto.

How do I limit page height in HTML?

Make the div a "cover", and make the image position: absolute . Then position it. Also, if you're making a gallery, then make the image's max-height: 90% or something like that. You can set the image height of height: 90vh , so it still has a 10% view height marginal room for spacing.

Is there max-height in CSS?

The max-height property in CSS is used to set the maximum height of a specified element. Authors may use any of the length values as long as they are a positive value. max-height overrides height, but min-height always overrides max-height .


2 Answers

Use CSS Viewport units for this.

Example:

.scrollDiv {     max-height: 100vh;     overflow: auto; } 

More info: https://www.w3schools.com/cssref/css_units.asp

like image 195
John Rix Avatar answered Sep 18 '22 13:09

John Rix


You can use $(window).height() to set the max-height to screen height:

$('.scrollDiv').css('max-height', $(window).height()); 

UPDATE

As mentioned in the John's answer. With the latest CSS3 API you can use vh's(View port unit for height)

.scrollDiv {     max-height: 100vh;     overflow: auto; } 
like image 31
James Avatar answered Sep 20 '22 13:09

James