6

Here's the code I'm using:

html,body{
  height: 100%;
}

div{
  min-height: 100%;
  height: auto !important;
  height: 100%;                /* this is for older IE */
}

http://jsfiddle.net/YYcLJ/

This also fails (but works in Opera):

div{
  min-height: 100%;
  height: auto;
}

So 100% height should be applied to all divs until html. But this only happens for the first DIV :(

In Opera it works correctly. First DIV gets to be 100% of body, 2nd is 100% of first div, and so on...

Is there any way to make this work in other browsers too?

Alex
  • 66,732
  • 177
  • 439
  • 641
  • may you give a desired snapshot – bugwheels94 Aug 05 '13 at 09:42
  • Page should be completely red (bg for last div from the tree)... – Alex Aug 05 '13 at 09:42
  • like this http://jsfiddle.net/YYcLJ/2/ – bugwheels94 Aug 05 '13 at 09:43
  • yes, but i need `height:auto` :( because content may have a bigger height than the viewport... – Alex Aug 05 '13 at 09:44
  • If you are saying that **content may have a bigger height than the viewport**, so why you want the red div to occupy the screen without the content? And if you want it that way, then why have you kept `height:auto;` if ultimately you want content to occupy the screen? I assume you are contradicting your own points. Kindly rectify me if that is not the case. - @Alex – Nitesh Aug 08 '13 at 04:59
  • I want the minimum height to be 100% of the screen. Actual height may be larger, depending on the content – Alex Aug 08 '13 at 07:25
  • 1
    **I want the minimum height to be 100% of the screen.** Then you should remove `height:auto`. This will take the screen to the minimum height which in your case is 100%. **Actual height may be larger, depending on the content** Then you should not remove `height:auto`, because if the content has to decide and populate height, auto height should exist. Sorry to say, but your statements are creating contradictions. Please provide a screenshot of how you want, provided the above points did not solve your issue. - @Alex – Nitesh Aug 08 '13 at 10:04
  • 1
    Alex @NathanLee is right! – NinjaOnSafari Aug 08 '13 at 12:10
  • And where's the contradiction? Like I said I want automatic height, but with a minimum height constraint of 100% of the current viewport. What's the point of having a `min-height` property in the CSS specs if you can't use it with an automatic height lol? Having a fixed height makes min-height useless – Alex Aug 08 '13 at 13:06
  • It is not necessary to use all the css properties to achieve bits and pieces of functionality lolz. I suggest using the one that serve your purpose to the goal and making it functional within the scope of appropriate semantics, makes perfect sense. - @Alex – Nitesh Aug 08 '13 at 13:16
  • Sorry I think I missed what you were trying to do in my first answer, [please see updated answer](http://stackoverflow.com/a/18130297/1947286) – apaul Aug 11 '13 at 15:10

6 Answers6

4

For that, you need to give only the div to have a height of 100%; for it to work. Plus, the way your hierarchy is, you need to change the display of your divs too.

This solution works cross browser. You can check.

Here is the WORKING SOLUTION

The HTML:

<div class="d1">
    <div class="d2">
        <div class="d3">
            hi
        </div>
    </div>
</div>    

The CSS:

html,body{
     height: 100%; /* Change 01. Div with height of 100% */
}

div{
  height: 100%;
}

.d1{
    background: #3cc;
    display:table; /* Change 02. Changing display of Divs */
    width:100%;
    height:100%;
}

.d2{
    background: #ddd;
    display:table-row; /* Change 03. Changing display of Divs */
}

.d3{
  background: #c00;
  display:table-cell; /* Change 04. Changing display of Divs */

}

I hope this is what you were looking for.

Nitesh
  • 15,425
  • 4
  • 48
  • 60
  • seems to work, but I don't think I can use `display: table` on my actual markup. Seems to brake many things.. – Alex Aug 08 '13 at 13:11
  • break?? Can you elaborate what breaks?? – Nitesh Aug 08 '13 at 13:17
  • I'm not sure this is what Alex wants. When I add content under the second `div` it splits into two separate `div` elements side by side. I thought she wanted them all to be horizontal. – Blake Plumb Aug 09 '13 at 18:00
  • I suggest you to also check what OP has asked, as she has a `min-height` which is not supported by IE7 or below. So I assume she is not looking at IE7 or below and FYI, What things OP as well as all other people who have answered does not support IE7 or below in totality. If you want something for those browsers, I would suggest to use , ,
    as they are most suitable for IE7 or below. I hope I have cleared your point. - @apaul34208
    – Nitesh Aug 11 '13 at 06:03
  • I think we may have stumbled on to an [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – apaul Aug 11 '13 at 13:31
  • What you have mentioned in your link is the HACK. Hack is not an alternative but a patch to make solutions work. As it is a workaround to make `min-height` work by adding an number of other css attributes and that is a fact for IE 7 and below. The same goes with `display:table;` and other non supporting css attributes for IE 7 and below. If you want such references, then this should look familiar to you too. http://stackoverflow.com/questions/249103/ie7-and-the-css-table-cell-property Right? - @apaul34208 – Nitesh Aug 12 '13 at 05:05
3

As specification says

If the height of the containing block is not specified explicitly (i.e., it depends on content height), and this element is not absolutely positioned, the percentage value is treated as '0' (for 'min-height') or 'none' (for 'max-height').

So Opera/Presto's behaviour is buggy in this case. Note that Opera 15+ behaves correctly.

The CSS3 draft allows using units that are relative to the viewport. So in this special case you can set

div {
    height: auto;
    min-height: 100%;  /* fallback for browsers that doesn't support "vh" */
    min-height: 100vh; /* vh == 1% of viewport height */
}
Maria
  • 548
  • 2
  • 8
  • I would say that the specification is buggy then:) When I add `min-height:100%` I expect the element to have minimum 100% height of the parent container :|. Anyway thanks, your solution with the `vw` thing seems to be the only one that works :D – Alex Aug 12 '13 at 16:28
2

try replace the:

height: auto !important;

on to:

height: inherit !important;

in this case it working but it still height property set the size of divs not min-height.

Mateusz
  • 1,222
  • 11
  • 22
1

I think we may have a bit of an XY Problem here. It looks like you've tried to implement a workaround for min-height to get it working in ancient Internet Explorer, but that workaround isn't working in other browsers.

In order to get the desired cross browser min-height try using height:auto; on .d3 and adding an IE conditional comment for IE 6 and below.

Working Example

html, body {
    height: 100%;
}
div {
    height:100%;
    min-height:100%;
}
.d1 {
    background: #3cc;
}
.d2 {
    background: #ddd;
}
.d3 {
    background: #c00;
    height:auto;
}

Add this to your HTML document to support IE6 and under :

<!--[if lte IE 6]>
<style>
.d3 { 
   height: 100%; 
}
</style>
<![endif]-->

Tested and working in Firefox, Chrome, Safari, Opera, IE10, IE9, IE8, IE7, IE6, and IE5.

All the divs are getting height:100%; and min-height:100%;. On .d3 height:100%; is being overridden by height:auto; due to the selector's specificity, but it still retains min-height:100%;.

For Internet Explorer 6 and under the conditional CSS will be applied to .d3 and override height:auto; with height:100%;

This works because (thankfully?) IE treats "height" how "min-height" is supposed to be treated.
-CSS Tricks

Community
  • 1
  • 1
apaul
  • 16,092
  • 8
  • 47
  • 82
1

The first important thing, please try to add CSS Reset scripts.

for demo i'm just reset the margin and padding to 0.

CSS :

* {
    margin: 0;
    padding: 0;
}

Then don't use height: auto !important; because the browser will calculates the height based on the child and this is default value.

Add box-sizing properties to allows you to define certain elements to fit an area in a certain way.

And for value use border-box. This value will specified the width and height (and min/max properties) on this element determine the border box of the element. The content width and height are calculated by subtracting the border and padding widths of the respective sides from the specified width and height properties

The box-sizing property is supported in IE(8+), Opera(8.5+), Chrome(*), and Safari(3).

For IE 6/7 you can use a polyfill for box-sizing: border-box by Christian Schepp Schaefer

This article about box-sizing by Chris Coyier.

And this complete solution and works cross browser. Demo

HTML :

<div class="first">
    First DIV
    <div class="second">
        Second DIV
        <div class="third">
            <div class="fourth">
                Fourth DIV
            </div>
        </div>
    </div>
</div>

CSS :

* {
    margin: 0;
    padding: 0;
}
html,body{
       height: 100%;            /* ie 6 will use this instead of min-height */
       min-height: 100%;        /* ie 6 will ignore this */
}

div{
    min-height: 100%;
    width:100%;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    -o-box-sizing: border-box;
    box-sizing: border-box;
    *behavior: url(pathto/boxsizing.htc); /* Add the behavior/HTC after every box-sizing: border-box. You must add prefix with a star so IE6/7 can see it */
}

.first{
    padding:50px;
    background: red;
}

.second{
    padding:25px;
    background: green;
}

.third{
    background: yellow;
}

.fourth{
    background: brown;
}
Yudha Setiawan
  • 376
  • 3
  • 5
  • it's because the size of the div dependent from the body and height of the body always set to 100%.. you just need to change height from the div like this **div {min-height:100%;}**. So if content of div is bigger than from the body that's div will update the height based on the content... [**see here**](http://jsfiddle.net/r9nUT/) – Yudha Setiawan Aug 12 '13 at 19:50
  • sorry, but try to read again comment from [**NathanLee**](http://stackoverflow.com/questions/18054911/100-min-height-working-in-opera-but-not-in-other-browsers/18138422#comment-26536286) Is it clear? Or if you want to get size of the viewport you can use `vw` for width and `vh` for the height.. but remember [**Can I Use this?**](http://caniuse.com/viewport-units) – Yudha Setiawan Aug 12 '13 at 21:34
1

Fiddle: http://jsfiddle.net/YYcLJ/5/

I would use position: absolute on the div elements. Then you will get the desired result.

div{
  position: absolute;
  min-height: 100%;
  height: auto !important;
  height: 100%;
  width: 100%;
}

This works because absolute positioning takes the div elements out of the flow of the document allowing their heights to be as specified. Since we do not declare the top or bottom of the div elements their top location will be the same as though their positioning was static.

http://www.w3.org/TR/CSS2/visudet.html#abs-non-replaced-height

Blake Plumb
  • 6,779
  • 3
  • 33
  • 54