[html] CSS - Expand float child DIV height to parent's height

I have the page structure as:

<div class="parent">
    <div class="child-left floatLeft">
    </div>

    <div class="child-right floatLeft">
    </div>
</div>

Now, the child-left DIV will have more content, so the parent DIV's height increases as per the child DIV.

But the problem is child-right height is not increasing. How can I make its height as equal to it's parent?

This question is related to html css layout

The answer is


I used this for a comment section:

_x000D_
_x000D_
.parent {_x000D_
    display: flex;_x000D_
    float: left;_x000D_
    border-top:2px solid black;_x000D_
    width:635px;_x000D_
    margin:10px 0px 0px 0px;_x000D_
    padding:0px 20px 0px 20px;_x000D_
    background-color: rgba(255,255,255,0.5);_x000D_
}_x000D_
    _x000D_
.child-left {_x000D_
 align-items: stretch;_x000D_
 float: left;_x000D_
 width:135px;_x000D_
 padding:10px 10px 10px 0px;_x000D_
 height:inherit;_x000D_
 border-right:2px solid black;_x000D_
}_x000D_
_x000D_
.child-right {_x000D_
 align-items: stretch;_x000D_
 float: left;_x000D_
 width:468px;_x000D_
 padding:10px;_x000D_
}
_x000D_
<div class="parent">_x000D_
  <div class="child-left">Short</div>_x000D_
  <div class="child-right">Tall<br>Tall</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

You could float the child-right to the right, but in this case I've calculated the widths of each div precisely.


CSS table display is ideal for this:

_x000D_
_x000D_
.parent {_x000D_
  display: table;_x000D_
  width: 100%;_x000D_
}_x000D_
.parent > div {_x000D_
  display: table-cell;_x000D_
}_x000D_
.child-left {_x000D_
  background: powderblue;_x000D_
}_x000D_
.child-right {_x000D_
  background: papayawhip;_x000D_
}
_x000D_
<div class="parent">_x000D_
  <div class="child-left">Short</div>_x000D_
  <div class="child-right">Tall<br>Tall</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

Original answer (assumed any column could be taller):

You're trying to make the parent's height dependent on the children's height and children's height dependent on parent's height. Won't compute. CSS Faux columns is the best solution. There's more than one way of doing that. I'd rather not use JavaScript.


I can see that the accepted answer uses position: absolute; instead of float: left. In case you want to use float: left with the following structure,

<div class="parent">
    <div class="child-left floatLeft"></div>
    <div class="child-right floatLeft"></div>
</div>

Give position: auto; to the parent so that it will contain its children height.

.parent {
    position: auto;
}
.floatLeft {
    float: left
}

I have recently done this on my website using jQuery. The code calculates the height of the tallest div and sets the other divs to the same height. Here's the technique:

http://www.broken-links.com/2009/01/20/very-quick-equal-height-columns-in-jquery/

I don't believe height:100% will work, so if you don't explicitly know the div heights I don't think there is a pure CSS solution.


Please set parent div to overflow: hidden
then in child divs you can set a large amount for padding-bottom. for example
padding-bottom: 5000px
then margin-bottom: -5000px
and then all child divs will be the height of the parent.
Of course this wont work if you are trying to put content in the parent div (outside of other divs that is)

_x000D_
_x000D_
.parent{_x000D_
    border: 1px solid black;_x000D_
    overflow: hidden;_x000D_
    height: auto;_x000D_
}_x000D_
.child{_x000D_
    float: left;_x000D_
    padding-bottom: 1500px;_x000D_
    margin-bottom: -1500px;_x000D_
}_x000D_
.child1{_x000D_
    background: red;_x000D_
    padding-right: 10px;    _x000D_
}_x000D_
.child2{_x000D_
    background: green;_x000D_
    padding-left: 10px;_x000D_
}
_x000D_
<div class="parent">_x000D_
    <div class="child1 child">_x000D_
        One line text in child1_x000D_
    </div>_x000D_
    <div class="child2 child">_x000D_
        Three line text in child2<br />_x000D_
        Three line text in child2<br />_x000D_
        Three line text in child2_x000D_
    </div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

Example: http://jsfiddle.net/Tareqdhk/DAFEC/


If you are aware of bootstrap you can do it easily by using 'flex' property.All you need to do is pass below css properties to parent div

.homepageSection {
  overflow: hidden;
  height: auto;
  display: flex;
  flex-flow: row;
}

where .homepageSection is my parent div. Now add child div in your html as

<div class="abc col-md-6">
<div class="abc col-md-6">

where abc is my child div.You can check equality of height in both child div irrespective of border just by giving border to child div


<div class="parent" style="height:500px;">
<div class="child-left floatLeft" style="height:100%">
</div>

<div class="child-right floatLeft" style="height:100%">
</div>
</div>

I used inline style just to give idea.


I learned of this neat trick in an internship interview. The original question is how do you ensure the height of each top component in three columns have the same height that shows all the content available. Basically create a child component that is invisible that renders the maximum possible height.

<div class="parent">
    <div class="assert-height invisible">
        <!-- content -->
    </div>
    <div class="shown">
        <!-- content -->
    </div>
</div>

I found a lot of answers, but probably the best solution for me is

.parent { 
  overflow: hidden; 
}
.parent .floatLeft {
  # your other styles
  float: left;
  margin-bottom: -99999px;
  padding-bottom: 99999px;
}

You can check other solutions here http://css-tricks.com/fluid-width-equal-height-columns/


A common solution to this problem uses absolute positioning or cropped floats, but these are tricky in that they require extensive tuning if your columns change in number+size, and that you need to make sure your "main" column is always the longest. Instead, I'd suggest you use one of three more robust solutions:

  1. display: flex: by far the simplest & best solution and very flexible - but unsupported by IE9 and older.
  2. table or display: table: very simple, very compatible (pretty much every browser ever), quite flexible.
  3. display: inline-block; width:50% with a negative margin hack: quite simple, but column-bottom borders are a little tricky.

1. display:flex

This is really simple, and it's easy to adapt to more complex or more detailed layouts - but flexbox is only supported by IE10 or later (in addition to other modern browsers).

Example: http://output.jsbin.com/hetunujuma/1

Relevant html:

<div class="parent"><div>column 1</div><div>column 2</div></div>

Relevant css:

.parent { display: -ms-flex; display: -webkit-flex; display: flex; }
.parent>div { flex:1; }

Flexbox has support for a lot more options, but to simply have any number of columns the above suffices!

2.<table> or display: table

A simple & extremely compatible way to do this is to use a table - I'd recommend you try that first if you need old-IE support. You're dealing with columns; divs + floats simply aren't the best way to do that (not to mention the fact that multiple levels of nested divs just to hack around css limitations is hardly more "semantic" than just using a simple table). If you do not wish to use the table element, consider css display: table (unsupported by IE7 and older).

Example: http://jsfiddle.net/emn13/7FFp3/

Relevant html: (but consider using a plain <table> instead)

<div class="parent"><div>column 1</div><div>column 2</div></div>

Relevant css:

.parent { display: table; }
.parent > div {display: table-cell; width:50%; }
/*omit width:50% for auto-scaled column widths*/

This approach is far more robust than using overflow:hidden with floats. You can add pretty much any number of columns; you can have them auto-scale if you want; and you retain compatibility with ancient browsers. Unlike the float solution requires, you also don't need to know beforehand which column is longest; the height scales just fine.

KISS: don't use float hacks unless you specifically need to. If IE7 is an issue, I'd still pick a plain table with semantic columns over a hard-to-maintain, less flexible trick-CSS solution any day.

By the way, if you need your layout to be responsive (e.g. no columns on small mobile phones) you can use a @media query to fall back to plain block layout for small screen widths - this works whether you use <table> or any other display: table element.

3. display:inline block with a negative margin hack.

Another alternative is to use display:inline block.

Example: http://jsbin.com/ovuqes/2/edit

Relevant html: (the absence of spaces between the div tags is significant!)

<div class="parent"><div><div>column 1</div></div><div><div>column 2</div></div></div>

Relevant css:

.parent { 
    position: relative; width: 100%; white-space: nowrap; overflow: hidden; 
}

.parent>div { 
    display:inline-block; width:50%; white-space:normal; vertical-align:top; 
}

.parent>div>div {
    padding-bottom: 32768px; margin-bottom: -32768px; 
}

This is slightly tricky, and the negative margin means that the "true" bottom of the columns is obscured. This in turn means you can't position anything relative to the bottom of those columns because that's cut off by overflow: hidden. Note that in addition to inline-blocks, you can achieve a similar effect with floats.


TL;DR: use flexbox if you can ignore IE9 and older; otherwise try a (css) table. If neither of those options work for you, there are negative margin hacks, but these can cause weird display issues that are easy to miss during development, and there are layout limitations you need to be aware of.


Does the parent have a height? If you set the parents height like so.

div.parent { height: 300px };

Then you can make the child stretch to the full height like this.

div.child-right { height: 100% };

EDIT

Here is how you would do it using JavaScript.


For the parent:

display: flex;

For children:

align-items: stretch;

You should add some prefixes, check caniuse.


Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to css

need to add a class to an element Using Lato fonts in my css (@font-face) Please help me convert this script to a simple image slider Why there is this "clear" class before footer? How to set width of mat-table column in angular? Center content vertically on Vuetify bootstrap 4 file input doesn't show the file name Bootstrap 4: responsive sidebar menu to top navbar Stylesheet not loaded because of MIME-type Force flex item to span full row width

Examples related to layout

This view is not constrained What's the difference between align-content and align-items? CSS Flex Box Layout: full-width row and columns Fill remaining vertical space with CSS using display:flex What is setContentView(R.layout.main)? How to change line color in EditText Scrolling a flexbox with overflowing content UICollectionView - Horizontal scroll, horizontal layout? How to style a div to be a responsive square? 100% width Twitter Bootstrap 3 template