273

Here is the HTML:

<div id="outer">
    <div id="inner"></div>
    Test
</div>

And here is the CSS:

#inner {
  float: left;
  height: 100%;
}

Upon inspection with the Chrome developer tools, the inner div is getting a height of 0px.

How can I force it to be 100% of the height of the parent div?

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
Nathan Osman
  • 71,149
  • 71
  • 256
  • 361
  • 1
    So you want the text in the outer div (not in the inner div), but the floated inner div to be the height of the outer div? – edl Jun 15 '10 at 23:48
  • What is the end result you are trying to achieve? I guess my confusion lies in that if the inner div is as high as the outer and the outer is as high as the text, isn't that the same as having the text in the inner div? – edl Jun 16 '10 at 00:09
  • @edl depends on widths. if #outer is wider than #inner, text will flow around (and possibly below), but if they are same width, text will end up below #outer. – Chadwick Jun 16 '10 at 00:22
  • 2
    @edl: Yes, it is :) The reason I want it this way is that the inner div has an image as its background. The text needs to be beside it. Both need to be inside the outer div as it has a background image too. – Nathan Osman Jun 16 '10 at 00:24
  • I have it working with tables now :) – Nathan Osman Jun 16 '10 at 00:27
  • 3
    Also see http://stackoverflow.com/questions/1122381 – Chadwick Jun 16 '10 at 01:40
  • Also http://stackoverflow.com/questions/15817019/how-to-float-an-element-left-with-full-height-of-the-wrapper – mikemaccana Jul 16 '14 at 14:52
  • ignore first 2 answers from this question – Claudiu Creanga Feb 10 '16 at 17:47
  • [Why is percentage height not working on my div?](http://stackoverflow.com/a/31728799/3597276) – Michael Benjamin Jun 19 '16 at 15:13
  • 2
    Seeing and trying the answers to this question was just depressing. – EternalHour Mar 21 '18 at 04:24

11 Answers11

136

For the parent:

display: flex;

You should add some prefixes http://css-tricks.com/using-flexbox/

Edit: Only drawback is IE as usual, IE9 does not support flex. http://caniuse.com/flexbox

Edit 2: As @toddsby noted, align items is for parent, and its default value actually is stretch. If you want a different value for child, there is align-self property.

Edit 3: jsFiddle: https://jsfiddle.net/bv71tms5/2/

Aiphee
  • 8,904
  • 3
  • 18
  • 16
  • 3
    You should point out that this works only in modern browsers, and even then requires vendor prefixes. – kontur Jul 07 '14 at 11:27
  • 12
    I'm surprised this solution hasn't received more attention, given that the original question requires the child div to be floated and all of the position:absolute solutions really throw a spanner in the works in most situations where you would be using float. This solution provides a great solution where the side-effects of position:absolute aren't an issue and if you're willing to dig a little further, it is possible to get some pretty good cross-browser compatibility. – Luke Jan 24 '15 at 08:00
  • This is the answer right here. IE9 will be going out soon enough, might as well help it along by giving it a good, solid kick. This solution is so nice, simple, and easy...I'm done hacking together alternative solutions. ;) – jrista Mar 03 '15 at 18:38
  • Thanks for this solution, works like a charm!, in my case I didn't need to use align-items: stretch, but display: flex did the trick. – Matias Jun 26 '15 at 18:27
  • 2
    According to the most recent spec for Flexbox: see https://www.w3.org/TR/css-flexbox-1/#align-items-property. `align-items: stretch;` isn't required as it is the default value. Also it should be defined on the parent not the child. – toddsby Feb 01 '17 at 14:24
  • According to caniuse.com, IE10 and IE11 have buggy enough implementations of flexbox that it's only considered "Partial Support." I'd be hesitant to use flexbox on a production site supporting those browsers: https://caniuse.com/#search=flexbox – Paul-Hebert Mar 29 '18 at 16:36
  • `display: inline-flex`can be useful too. – Flo Develop May 28 '22 at 21:41
125

For #outer height to be based on its content, and have #inner base its height on that, make both elements absolutely positioned.

More details can be found in the spec for the css height property, but essentially, #inner must ignore #outer height if #outer's height is auto, unless #outer is positioned absolutely. Then #inner height will be 0, unless #inner itself is positioned absolutely.

<style>
    #outer {
        position:absolute; 
        height:auto; width:200px; 
        border: 1px solid red; 
    }
    #inner {
        position:absolute; 
        height:100%; 
        width:20px; 
        border: 1px solid black; 
    }
</style>

<div id='outer'>
    <div id='inner'>
    </div>
    text
</div>

However... By positioning #inner absolutely, a float setting will be ignored, so you will need to choose a width for #inner explicitly, and add padding in #outer to fake the text wrapping I suspect you want. For example, below, the padding of #outer is the width of #inner +3. Conveniently (as the whole point was to get #inner height to 100%) there's no need to wrap text beneath #inner, so this will look just like #inner is floated.

<style>
    #outer2{
        padding-left: 23px;
        position:absolute; 
        height:auto; 
        width:200px; 
        border: 1px solid red; 
    }
    #inner2{
        left:0;
        position:absolute; 
        height:100%; 
        width:20px; 
        border: 1px solid black; 
   }
</style>

<div id='outer2'>
    <div id='inner2'>
    </div>
    text
</div>

I deleted my previous answer, as it was based on too many wrong assumptions about your goal.

ChainFuse
  • 797
  • 2
  • 10
  • 26
Chadwick
  • 12,555
  • 7
  • 49
  • 66
  • 126
    Hmm.. the question was about Floated div's. You answered about absolutely position divs – Mihkel L. Jul 18 '14 at 10:38
  • 59
    I never stop surprising how complex it is to achieve insanely simple things in `HTML/CSS` :) – Yuriy Nakonechnyy Oct 07 '14 at 16:09
  • 15
    Any way to do this without hardcoding widths? Not great for responsive containers. – Jake Wilson Jul 13 '15 at 04:01
  • 24
    I don't know why this was accepted and upvoted so much. The question is about floated divs, which is what brought me here from my search, and instead this answers absolutely positioned divs. – Matt K Jan 29 '16 at 18:47
  • 2
    How to make a floated container vertically fit it's container? By not making it float anymore! Haha. Nice try, and close enough to be considered useful, actually. – Rolf Nov 10 '16 at 10:39
  • @Yura In CSS, "height:100%" actually means set the height to 100% of container if this condition and that condition (etc.) are fulfilled. To get a full answer, spend a day or so reading the relevant specs. Or try your luck on SO! – Rolf Nov 10 '16 at 10:41
  • 2
    This answer is so esoteric, and look at the other answers! "treat it like a table cell" or "use flex". These are quality answers, and they amount to using tools that were designed for the task OP wants to accomplish. Absolute positioning is error prone and waay overkill. – 16807 Apr 21 '17 at 18:35
  • 1
    Today makes sense using `flex`, but if you don't understand this properties, you will have a hard time using `flexbox`. This answer made me understand `position` property influence on other properties and this is the foundation on which flexbox is built. For sure now we will use 100% of the time flexbox. – Fabrizio Bertoglio Jan 06 '18 at 14:39
77

Actually, as long as the parent element is positioned, you can set the child's height to 100%. Namely, in case you don't want the parent to be absolutely positioned. Let me explain further:

<style>
    #outer2 {
        padding-left: 23px;
        position: relative; 
        height:auto; 
        width:200px; 
        border: 1px solid red; 
    }
    #inner2 {
        left:0;
        position:absolute; 
        height:100%; 
        width:20px; 
        border: 1px solid black; 
    }
</style>

<div id='outer2'>
    <div id='inner2'>
    </div>
</div>
ChainFuse
  • 797
  • 2
  • 10
  • 26
  • 14
    Cool, this is less limiting than Chadwick's requirement of `position:absolute` (which could interfere with a site's styles) – henry May 16 '13 at 05:01
  • 3
    It could be just me, but I have problems if the sidebar is larger than the content. A background color will stop at the parent container but the sidebar content will overflow outside the parent. – Howdy_McGee Apr 29 '14 at 16:17
  • This is a much more flexible solution, and even if you have several floated child elements, it seems an acceptable drawback to style their offset absolutely positioned. +1 – kontur Jul 07 '14 at 11:26
  • Thank you **very** much. I've posted my specific solution as an answer to another question on SO: http://stackoverflow.com/a/31749164/84661. – Brian Cannard Jul 31 '15 at 14:53
25

As long as you don't need to support versions of Internet Explorer earlier than IE8, you can use display: table-cell to accomplish this:

HTML:

<div class="outer">
    <div class="inner">
        <p>Menu or Whatever</p>
    </div>
    <div class="inner">
        <p>Page contents...</p>
    </div>
</div>

CSS:

.inner {
    display: table-cell;
}

This will force each element with the .inner class to occupy the full height of its parent element.

Nathan Osman
  • 71,149
  • 71
  • 256
  • 361
  • Although this will work, it won't show a margin, even if you specify one. So this solution will fail if you want to assign a margin. You can assign padding as a work-around, but how would you solve this if you needed a margin (& not padding)? – Devner Sep 02 '13 at 12:10
  • 14
    Am I missing something here? Your question asks for a floated element to have height 100%. When floated, a `display: table-cell;` element doesn't fill the height of its container any more? I must be missing something, because it's your own question...? – user56reinstatemonica8 Apr 25 '16 at 00:26
17

I made an example resolving your problem.

You have to make a wrapper, float it, then position absolute your div and give to it 100% height.

HTML

<div class="container">
    <div class="left">"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." </div>
  <div class="right-wrapper">
    <div class="right">"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." </div>
  </div>
  <div class="clear">&nbsp;</div>
</div>

CSS:

.container {
    width: 100%;
    position:relative;
}
.left {
    width: 50%;
    background-color: rgba(0, 0, 255, 0.6);
    float: left;
}
.right-wrapper {
    width: 48%;
    float: left;
}
.right {
    height: 100%;
    position: absolute;
}

Explanation: The .right div is absolutely positioned. That means that its width and height, and top and left positiones will be calculed based on the first parent div absolutely or relative positioned ONLY if width or height properties are explicitly declared in CSS; if they aren't explicty declared, those properties will be calculed based on the parent container (.right-wrapper).

So, the 100% height of the DIV will be calculed based on .container final height, and the final position of .right position will be calculed based on the parent container.

Mandarinazul
  • 171
  • 1
  • 3
  • 1
    This is a solid solution for situations in which you can _guarantee_ that the `.right` column's content is shorter than the `.left` column's content. For example, if you are using this on a component in which the `.left` column has some variably sized content and the right column simply displays an arrow to a detail page then this solution works well. +1 for that – Zachary Kniebel Mar 24 '15 at 16:44
16

Here it is a simpler way to achieve that:

  1. Set the three elements' container (#outer) display: table
  2. And set the elements themselves (#inner) display: table-cell
  3. Remove the floating.
  4. Success.
#outer{
    display: table;
}
#inner {
    display: table-cell;
    float: none;
}

Thanks to @Itay in Floated div, 100% height

Community
  • 1
  • 1
leopinzon
  • 707
  • 6
  • 13
6

If you're prepared to use a little jQuery, the answer is simple!

$(function() {
    $('.parent').find('.child').css('height', $('.parent').innerHeight());
});

This works well for floating a single element to a side with 100% height of it's parent while other floated elements which would normally wrap around are kept to one side.

Hope this helps fellow jQuery fans.

Mikael Engver
  • 4,634
  • 4
  • 46
  • 53
Obi-Dan
  • 187
  • 3
  • 13
3

This is a code sample for grid system with equal height.

#outer{
width: 100%;
margin-top: 1rem;
display: flex;
height:auto;
}

Above is the CSS for outer div

#inner{
width: 20%;
float: left;
border: 1px solid;
}

Above is the inner div

Hope this help you

Kashif Nazar
  • 20,775
  • 5
  • 29
  • 46
Biswajit
  • 978
  • 3
  • 11
  • 30
2

This helped me.

#outer {
    position:relative;
}
#inner {
    position:absolute;
    top:0;
    left:0px;
    right:0px;
    height:100%;
}

Change right: and left: to set preferable #inner width.

sevenkey
  • 21
  • 2
1

A similar case when you need several child elements have the same height can be solved with flexbox:

https://css-tricks.com/using-flexbox/

Set display: flex; for parent and flex: 1; for child elements, they all will have the same height.

romaroma
  • 658
  • 8
  • 13
-1

try

#outer{overflow: auto;}

show more options in: How do you keep parents of floated elements from collapsing?

Community
  • 1
  • 1
  • the "overflow:hidden" option seems to work better because there's no chance of rolling... – Rodrigo Barreiro Jan 16 '17 at 17:49
  • If you came up with a better solution, you can [edit](http://stackoverflow.com/posts/41682029/edit) your answer. Also, it would be good if you added more information to your answer, like for example the information provided in the link. – Donald Duck Jan 16 '17 at 18:04