How might I force a floating DIV to match the height of another floating DIV?
Wrap them in a containing div with the background color applied to it, and have a clearing div after the 'columns'.
<div style="background-color: yellow;">
<div style="float: left;width: 65%;">column a</div>
<div style="float: right;width: 35%;">column b</div>
<div style="clear: both;"></div>
</div>
Updated to address some comments and my own thoughts:
This method works because its essentially a simplification of your problem, in this somewhat 'oldskool' method I put two columns in followed by an empty clearing element, the job of the clearing element is to tell the parent (with the background) this is where floating behaviour ends, this allows the parent to essentially render 0 pixels of height at the position of the clear, which will be whatever the highest priorly floating element is.
The reason for this is to ensure the parent element is as tall as the tallest column, the background is then set on the parent to give the appearance that both columns have the same height.
It should be noted that this technique is 'oldskool' because the better choice is to trigger this height calculation behaviour with something like clearfix or by simply having overflow: hidden on the parent element.
Whilst this works in this limited scenario, if you wish for each column to look visually different, or have a gap between them, then setting a background on the parent element won't work, there is however a trick to get this effect.
The trick is to add bottom padding to all columns, to the max amount of size you expect that could be the difference between the shortest and tallest column, if you can't work this out then pick a large figure, you then need to add a negative bottom margin of the same number.
You'll need overflow hidden on the parent object, but the result will be that each column will request to render this additional height suggested by the margin, but not actually request layout of that size (because the negative margin counters the calculation).
This will render the parent at the size of the tallest column, whilst allowing all the columns to render at their height + the size of bottom padding used, if this height is larger than the parent then the rest will simply clip off.
<div style="overflow: hidden;">
<div style="background: blue;float: left;width: 65%;padding-bottom: 500px;margin-bottom: -500px;">column a<br />column a</div>
<div style="background: red;float: right;width: 35%;padding-bottom: 500px;margin-bottom: -500px;">column b</div>
</div>
You can see an example of this technique on the bowers and wilkins website (see the four horizontal spotlight images the bottom of the page).
Make floating divs the same height
You could try instead of using float, use display: table-cell
. You might find some older browsers don't understand this rule however. See below:
#wrapper {
display: table; // See FelipeAls comment below
width: 300px;
}
#left {
display: table-cell;
width: 50px;
background: blue;
}
#right {
display: table-cell;
width: 250px;
background: red;
}
HTML/CSS: Making two floating divs the same height
You can get equal height columns in CSS by applying bottom padding of a large amount, bottom negative margin of the same amount and surrounding the columns with a div that has overflow hidden. Vertically centering the text is a little trickier but this should help you on the way.
#container {
overflow: hidden;
width: 100%;
}
#left-col {
float: left;
width: 50%;
background-color: orange;
padding-bottom: 500em;
margin-bottom: -500em;
}
#right-col {
float: left;
width: 50%;
margin-right: -1px; /* Thank you IE */
border-left: 1px solid black;
background-color: red;
padding-bottom: 500em;
margin-bottom: -500em;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head></head>
<body>
<div id="container">
<div id="left-col">
<p>Test content</p>
<p>longer</p>
</div>
<div id="right-col">
<p>Test content</p>
</div>
</div>
</body>
Force all floating DIVs to match the height of their container
If you're ok with javascript/jQuery, here's the solution I would use.
Floating divs, equal height - fill space with additional div without overflow
Pure CSS solution
Here is a DEMO of that solution.
In this DEMO, you see multipple Row
s,
each Row
can have a variable number of columns without stating anything in the markup, and without fixing any width. (the width is always divided evenly between the columns).
Each column is called ElementsHolder
, and can have any number of Elements
you want.
all the column in a row will always have the same height, and the last arrow in the row will fill that space.
In the DEMO you can see 3 Row
s.
The First Row
has the starting point, so no stretch needed there.
The Second Row
has 3 ElementsHolder
, without stating anything special in the markup, 2 of them will stretch to fill the gap.
The Third Row
has 2 ElementsHolder
, behave as expected.
notice that the stretching works regardless of the Element
s height. (some of them have 2 or 3 lines of text, and it works perfectly)
If you want to use that technique, you only have to implement the other kind of boxes and arrows (Curve etc..)
The solution is done by using the new CSS flex model.
the direction is set via flex-direction: row;
,
Each row has ElementsHolder
s that gets equal width.
each one of those ElementsHolder
is also a flex box, but this time his direction is opposite (flex-direction: column;
).
the child's of ElementsHolder
are Element
s & Arrow
s, I dont want them to have equal height, but to span excatly the natural height. except the last arrow, that should span the rest of the container.
all of that is achieved using the flex
property with the appropriate values.
More about the flex-model can be found HERE
How to fit a div's height to wrap around its floated children
This is a common issue when working with floats. There are several common solutions, which I have ordered by personal preference (best approach first):
Use the ::after CSS pseudo element. This is know as the 'clearfix', and works IE8 and up. If you need compatibility with earlier versions of IE, this answer should help. Example.
.parentelement::after {
content: "";
display: table;
clear: both;
}Add the two floats into a container with the CSS attribute
overflow: auto
oroverflow: hidden
. However, this approach can cause issues (e.g. when a tooltip overlaps the edges of the parent element a scrollbar will appear). Example.<div style="overflow: auto">
<div style="float: left"></div>
<div style="float: left"></div>
</div>Add a set height to the parent element. Example.
<div style="height: 200px">
<div style="float: left"></div>
<div style="float: left"></div>
</div>Make the parent element a float. Example.
<div style="float: left">
<div style="float: left"></div>
<div style="float: left"></div>
</div>Add a div after the floats with
clear: both
. Example.<div style="float: left"></div>
<div style="float: left"></div>
<div style="clear: both"></div>
How can you force a floating div to be the height of its parent?
This is essentially an “equal column height” question. You can't directly set the child height to 100% because the height of the parent is indeterminate. There are workarounds. of various sorts.
But in your case: use a table. It's clearly tabular data, so there's nothing wrong with using a table for it.
Floating elements within a div, floats outside of div. Why?
The easiest is to put overflow:hidden
on the parent div and don't specify a height:
#parent { overflow: hidden }
Another way is to also float the parent div:
#parent { float: left; width: 100% }
Another way uses a clear element:
<div class="parent">
<img class="floated_child" src="..." />
<span class="clear"></span>
</div>
CSS
span.clear { clear: left; display: block; }
Related Topics
How to Prevent Browser from Caching Form Fields
How Does Justify-Items Work on Display:Block Elements
Subpixel Anti-Aliased Text on HTML5's Canvas Element
How to Correctly Use "Section" Tag in HTML5
Why Is Chrome Showing a "Please Fill Out This Field" Tooltip on Empty Fields
How to Transform Each Side of a Shape Separately
How to Detect HTML5 Audio Mp3 Support
How to Make a Youtube Embedded Video a Full Page Width One
Three Column Layout: Fixed Width Center with Fluid Side Columns
Transition for Background-Image in Firefox
Text-Decoration Not Working for Visited State Link
Do Custom CSS Properties Use One Leading Dash or Two
How to Get Placeholder Text in Firefox and Other Browsers That Don't Support The HTML5 Tag Option
Progress Bar with HTML and CSS
Emulate Ie7 for Ie8 But Not for Ie9 Using "X-Ua-Compatible"