Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto adjust div height with CSS

Tags:

html

css

Let's say there is six divs one on the other.

1st, 2nd and 3rd div has a fixed height e.g. 25px, 100px and 25px. 4th div is content area and should be an auto adjusting div. 5th div has some content and the min-height is 100px (height is NOT fixed). 6th div is a footer has a fixed height e.g. 25px.

5th and 6th divs should be always on the bottom of the page (NOT sticky)

There is no problem when the 4th div (div_auto_height) has a lot of content and the page is just as long or longer than the screen.

The problem occurs when the page is shorter than the screen and the empty space comes after the 6th div. Then the 5th and 6th div are not where they supposed to be.

This is the normal case

The problem would be solved if one can get the height of the 4th div (div_auto_height) automatically adjusted to fill the empty space.

This is what we need to have

I have been trying to solve this problem in many ways without a decent solution.

Not working solutions:

  • There are different screen resolutions, so min-height doesn't work with large screens without making the page a very long for small or wide screens.
  • I haven't been able to get top and bottom positioning properties work properly because it makes divs 5 and 6 to come on top of 4th div (div_auto_height)

Here is a template for your modification:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head><title>No title</title>
<style type="text/css">

html {
    height: 100%;
}

body {
    height: 100%;
    margin: 0;
}

#div1 {
    height: 25px;
    background-color: #123456;
}

#div2 {
    height: 100px;
    background-color: #345678;
}

#div3 {
    height: 25px;
    background-color: #567890;
}

#div_auto_height {
    height: auto ;
    background-color: #789012;
}

#div5 {
    min-height: 100px;
    background-color: #901234;
}

#div6 {
    height: 25px;
    background-color: #123456;
}

</style>
</head>
<body>
    <div id="div1">Div 1</div>
    <div id="div2">Div 2</div>
    <div id="div3">Div 3</div>
    <div id="div_auto_height">This div should adjust automatically</div>
    <div id="div5">Div 5</div>
    <div id="div6">Div 6</div>
</body>
</html>
</body>
</html>
like image 766
Johnny Avatar asked Jan 09 '12 22:01

Johnny


People also ask

How do I Auto adjust height in CSS?

If height: auto; the element will automatically adjust its height to allow its content to be displayed correctly. If height is set to a numeric value (like pixels, (r)em, percentages) then if the content does not fit within the specified height, it will overflow.

How auto adjust the div height according to content?

Syntax: height: length|percentage|auto|initial|inherit; Property Values: height: auto; It is used to set height property to its default value.

How dynamically change the height of a div?

The content height of a div can dynamically set or change using height(), innerHeight(), and outerHeight() methods depending upon the user requirement.

How do you give a height 100% to a div?

Syntax: To set a div element height to 100% of the browser window, it can simply use the following property of CSS: height:100vh; Example: HTML.


2 Answers

I believe that you should use javascript to solve this problem. There are a lot of easy-to-use javascript frameworks out there like jquery.

What you should do:

  • give an id to your divs.
  • using those ids find your divs - except for the fourth - and calculate their height
  • get the height of the window
  • set the 4th div's height by substracting the window height from the sum of your other divs height

And that's all! Tell me if you need some code.

edit: I didn't find the code but I remember that I used jquery dimensions. You can find example codes there!

like image 162
Adam Arold Avatar answered Oct 12 '22 08:10

Adam Arold


There is no CSS way to give a property the value "total screen height - some fixed pixels". Use Javascript.

like image 2
Mr Lister Avatar answered Oct 12 '22 07:10

Mr Lister