6

Possible Duplicate:
CSS - 100% height doesn’t work

I have 3 divs, the div-1 is a background and div-2 and div-3 are two containers (one for text and one for photo).

#div-1 {
  width: 100%;
  height: 100%;
  padding: 40px 0;
  margin: 0;
}

#div-2 {
  width: 500px;
  margin: 0;
  float: left;
}

#div-3 {
  width: 200px;
  margin: 0;
  float: right;
}
<div id="div-1">
  <div id="div-2"></div>
  <div id="div-3"></div>
</div>

This is what I get:

enter image description here

Why height: 100% doesn't work?

Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
Adam
  • 173
  • 1
  • 3
  • 11
  • 4
    try this: `html,body{height:100%;}` The height of these elements is automatic. – Shmiddty Oct 15 '12 at 14:41
  • This worked for me. Looking at the standard default css rules, I see no reason for these elements to have auto height. Oh, I guess because html has no parent its 100% height is zero? Strange. – David Spector Aug 29 '19 at 15:22

4 Answers4

4

This can work

<div id="div-1">
    <div id="div-2"></div>
    <div id="div-3"></div>
    <div style="clear:both"></div>
</div>
Ives.me
  • 2,359
  • 18
  • 24
4

The height of #div-1 is 100% but 100% of nothing, as it relies on the parent tags height I believe. Try setting your body to 100% height in css.

Simon Carlson
  • 1,919
  • 5
  • 24
  • 35
4

Remove height: 100%; in #div-1 and add position: absolute;. It will work just fine.

Matheus Avellar
  • 1,507
  • 1
  • 22
  • 29
3

you need to add clear flow after you applying floating properties to elements, more about it here: http://www.quirksmode.org/css/clearing.html

Roman
  • 504
  • 4
  • 10