3

I want a 100% height div with absolutely positioned divs inside it. For whatever reason when I add the child divs, the height of the parent div shrinks to the static content size.

To clarify, I do not want the absolutely positioned elements to be full-height. Just the containing div.

I have (simplified) markup as follows:

<div id="content">
   <div id="dashboard">
      Some text
      <div class="widget"></div>
      <div class="widget"></div>
      ...
   </div>
</div>

And the styling:

#content {
  min-height: 100%;
}
#dashboard {
  min-height: 100%;
  height: 100%;
  position: relative;
}
.widget {
  height: 50px; /* arbitrary */
  width: 50px;
  position: absolute;
}

I have put #content and #dashboard as min-height 100% and height 100% and that works. If I comment out the absolutely positioned divs, both are the full height of the screen.

But when I add the .widgets, #content is still full height (good) but #dashboard becomes only the height of 'some text'. For whatever reason, adding absolutely positioned content to #dashboard changes its height.

Note (edit)

I don't want #content to be 100% height because it needs to grow with content on other pages. I only want #dashboard to be exactly 100% height.

jQuery works, but I'd like to do it with css only

$("#dashboard").height( $("#content").height() );

Also I tried changing the type of display to block or inline for #content and setting -moz-box-sizing to default because I read it can break min-height for Firefox.

Any idea how to fix this?


Similar but not the same: How to set 100% height of a parent div with absolutely positioned children (not a duplicate)?

Community
  • 1
  • 1
AJcodez
  • 31,780
  • 20
  • 84
  • 118

4 Answers4

4

From the MDN:

A percentage value for min-height property is calculated with respect to the height of the generated box's containing block. 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.

Hence, you'll need to specify the height of the #content element explicitly to get min-height property of #dashboard element to work.

Thus, try using height property for #content instead of min-height:

#content {
  height: 100%;
}

Here is a jsDiddle Demo.

html, body {
    height: 100%;
}
#content {
  height: 100%;
}
#dashboard {
  min-height: 100%;
  position: relative;
}
.widget {
  height: 50px; /* arbitrary */
  width: 50px;
  position: absolute;
}​
<div id="content">
   <div id="dashboard">
      Some text
      <div class="widget"></div>
      <div class="widget"></div>
      ...
   </div>
</div>​

UPDATE

I don't want #content to be full-size at all times.

Then, you'll need to use a fixed height for the #content:

#content {
  height: 200px;  /* or whatever you want */
}

#dashboard {
  height: 100%;   /* or inherit */;
}

Else, you should use JavaScript to calculate the needed height and apply that to the #dashboard element.

Hashem Qolami
  • 97,268
  • 26
  • 150
  • 164
  • I don't want #content to be full-size at all times. I want it to extend indefinitely on other pages... – AJcodez Oct 17 '12 at 21:30
  • So why did you use `min-height` ?? however, you can use any percentages or pixels you want. – Hashem Qolami Oct 17 '12 at 21:33
  • I want it to take up the whole screen or extend. Thats what min-height is for, no? – AJcodez Oct 17 '12 at 21:35
  • 1
    Let me know what exactly you mean. – Hashem Qolami Oct 17 '12 at 21:40
  • Edited question. Your solution would work, but #content is a centered content div with a background in the middle of the page, so I can't have overflowing content, and I can't have it scroll or I'd have a scrollbar in the middle of the page! – AJcodez Oct 17 '12 at 21:45
2

Absolutely-positioned elements are no longer part of the layout. The parent element has no idea how big child items are.

You need to set the height of the parent manually, or use JS to calculate the size of the child elements and apply accordingly.

Also, 100% height elements need their parent elements to be 100% height as well:

body, html { height:100% }
Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
  • Parent (#dashboard) height is specified at 100% – Kevin Boucher Oct 17 '12 at 21:30
  • +1 the height at 100% only takes up 100% of its parent element which in this case is the body, html :) - http://jsfiddle.net/mcAXP/ – Aaron Oct 17 '12 at 21:32
  • `#content` is set to min-height: 100%, but according to @Hashem, setting that to `height: 100%` works. -- Apparently this is not a solution for OP though. – Kevin Boucher Oct 17 '12 at 21:33
1

When you want to have a min-height of 100% (min 100%, expand when there is more content) for #content, use min-height together with height: auto:

body, html {
  height: 100%;
}
#content {
  min-height: 100%;
  height: auto;
}

Furthermore, if i understand your question correctly, you could simply use position: absolute together with top: 0; right: 0; bottom: 0; left: 0; to have exactly a height of 100% for #dashboard. Add position: relative to #content if the height of #dashboard should be relative to #content.

Felix Ebert
  • 1,103
  • 11
  • 30
0

Absolute elements are taken out of the flow. If they are out of the flow, they do not add height to their parent.

JimmyRare
  • 3,958
  • 2
  • 20
  • 23
  • Parent (`#dashboard`) height is specified at 100% – Kevin Boucher Oct 17 '12 at 21:29
  • Because the OP does not seem unaware that absolute positioning takes elements out of the flow. (I.e. That is not the issue here.) – Kevin Boucher Oct 17 '12 at 21:40
  • Since OP did not know how to use the height property I assumed a clarification of the absolute positioning was in place. Since I cannot comment, because of my low reputation, I answered. My bad. Should I delete it? – JimmyRare Oct 17 '12 at 21:46
  • _Since OP did not know how to use the height property_ I didn't get that impression. He said that `height: 100%` worked as long as the child DIVs were not `position: absolute`. Which I verified in my fiddle: http://jsfiddle.net/kboucher/4zwyW/. (Change `#content` `min-height` to `height` and see it work.) – Kevin Boucher Oct 17 '12 at 21:50
  • If you'll edit your answer, though I will remove the downvote. (Tried already, but is locked in till you edit) – Kevin Boucher Oct 17 '12 at 21:51
  • "He said that height: 100% worked as long as the child DIVs were not position: absolute." And this was why I explained how absolute positioning works. – JimmyRare Oct 17 '12 at 21:55
  • `height: 100%` should make the element 100% regardless of the positioning of the child elements. (Assuming parent elements allow for it) – Kevin Boucher Oct 17 '12 at 21:58