Find more detailed results with CSS examples here and more information about equal height columns here.
Your CSS results in undefined behavior - the parent's height depends on the childrens height, but the children have a percentage height defined in terms of the parent: this is invalid, and will likely not work in a cross-browser way.
I see you're not floating everything - so that's defined at the cost of not scaling if ever the right column grows longer than the left (which indeed is not the case in the OP's question).
Yes, you need to add this: padding-bottom: 32768px; margin-bottom: -32768px; to the children.
This works as long as the right column is guaranteed to have fewer content then the left or else it will be clipped. Also I edited the answer to omit the useless declarations.
@MatthewBlackford: it's a "hack" in that it prevent margin collapsing. You could prevent margin collapsing in other ways, such as using a 1px transparent border, but in essence, but the important thing here is that you avoid margin collapse. I wouldn't use this solution except as a last resort since it causes weird layout errors and unexpected cropping (or superimposed content) when your assumptions turn out to be faulty. In short: you don't want to do this unless you really need to.
E
Eamon Nerbonne
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:
display: flex: by far the simplest & best solution and very flexible - but unsupported by IE9 and older. table or display: table: very simple, very compatible (pretty much every browser ever), quite flexible. 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).
Flexbox has support for a lot more options, but to simply have any number of columns the above suffices!
2.
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).
.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.
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.
Good one, but it's worth to note that you're unable to use margins between these cells (and padding won't help if you want them styled).
border-spacing:10px; on the table element will introduce margin-like spacing. Alternatively, you could add extra (empty) columns where you want extra space. And you could also add padding inside the table-cells; that padding will be included in the background however, so that may not be what you want. But you're right that it's not quite as flexible as a normal margin. And you can't position anything relative to the columns, which may be a problem.
So it's better a table-less layout or not ?
I have to say: I agree to you that in this case a simple table is more than enough. The problem is that there is a common view to disregard every layout with table
The problem with table is that when the screen is resized to a smaller width (responsive design) the table cells get squished instead of tiling one over the top of the next one. The second method with padding-bottom: 32768px; margin-bottom: -32768px; is quite amazing and does almost exactly what I needed...thanks for that one...border bottom is an issue though...
a
andreas
For the parent:
display: flex;
For children:
align-items: stretch;
You should add some prefixes, check caniuse.
Wouldn't recommend. IE9 is still a thing.
Thank you for your answer; it works well! "You should add some prefixes, check caniuse." what do you mean by that line? Can you add little more information for beginners?
@MdSifatulIslam Go here: caniuse.com/#feat=flexbox You can see if some browser youu need to support needs feature prefix.
D
Dobromir Minchev
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;
}
I have no idea why this work, but it solves my problem like a charm. Thank you
Works for me too. Even though it's not the clean way. Thanks!
hacky as hell. I do not recommend this for a production applications
this is amazing haha. I didn't think this would actually work
T
Tareq
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)
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.
Good point. How about if the children's height is dependent on the height of the tallest sibling (whether that be determined by the height of the parent or the not), and not the parent? I guess that can't be done in CSS.
No, siblings cannot affect the height of each other. However, display: table + display: table-cell layout will produce the desired result.
Great update! However, I ended up trying this and I wanted to have whitespace separation between the divs/cells and ended up having to make inner divs within the table-cell divs to accomplish that and then of course lost the ability to make them use the full height because the inner divs weren't table-cell divs - exact same problem. Any ideas for that? Kind of missing cell-spacing now.
Ah, there is border-spacing in CSS! stackoverflow.com/questions/339923/… I was able to get my table-like divs working well with whitespace separation by using border-spacing. I now have the ability to make table cells (colored background blocks) use the full height of the parent or not by clever use of the table-cell divs.
You could float the child-right to the right, but in this case I've calculated the widths of each div precisely.
first thing that came to my mind was height:inherit not sure why nobody upvoted
a
ajcw
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:
Give position: auto; to the parent so that it will contain its children height.
.parent {
position: auto;
}
.floatLeft {
float: left
}
J
Joel Lim
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.