views:

267

answers:

5

Hi,

I have an issue with CSS I can't solve.

I've made a little diagram.

Let's say that the pink and green box's height are determined by there content. The pink box could sometimes be the smaller one.

What I am trying to do is have the smaller box fix it's height to the outer containing div, so that it would have the same height as the pink box (or vice-versa).

Anyone have any solutions?

Min-height on the pink and green boxes won't work because they may exceed that height (also no IE6 support).

100% height on the pink and green boxes won't work because the outer div does not have a fixed height.

A table would work, but come on, a table?

I could fake backgrounds and left-right-borders on the pink and green boxes by putting them in the outer div's background. But that seems messy.

At the moment I have a js solution, but there must be a simpler one.

Cheers.

+4  A: 

Apart from Javascript/Jquery or tables, the only thing I can think of is a display: table-cell based solution (explanation here) - but that won't work in IE either, and there isn't much difference to using a table straight away, is there?

I'd say this is one of the rare cases where, due to sucky CSS specifications and/or implementations, there is no way around a table.

Edit: As other answerers have pointed out, there are CSS workarounds ("Faux columns") that will work in most cases. Using "CSS tables" (using display: table properties) I do not deem valid solutions yet, as they are not supported by IE6, a browser that still has a considerable market share.

Pekka
OHMYWORDTABLES :) Actually, I still think Faux Columns is the right answer. Tables *really* are not a layout mechanism. With Faux Columns, you can always turn off the background image and change the layout in the CSS. With tables, you need to edit the table to change the layout. Just my two cents.
Doug Neiner
CSS tables **are** a layout mechanism and do not require HTML table elements. There is no more need to edit the HTML when CSS tables are used for layout then there is when any other kind of CSS layout is used (i.e. you'll probably have to tweak something for any significant change).
David Dorward
@Doug true, but doesn't always work (when you need the columns to *physically* have the same height, e.g. when positioning something inside the column). Faux columns are an o.k. solution if they work for your situation (and I'm perfectly content if they are the solutions to the OP's problem), but still, this is a massive shortcoming of CSS of which I find horrible that it is not addressed in some form.
Pekka
@David, can you show me a CSS only solution that achieves what the OP wants, with the elements *physically* stretching to the same height, that works in IE and without JavaScript?
Pekka
I really cannot understand why horrible CSS hacks are good while tables are unacceptable.
kemp
@Pekka Why are you asking that of me specifically?
David Dorward
@kemp HTML tables describe semantics. If those semantics don't really exist, they are a lie. Lying is bad. Neither Faux Columns nor CSS tables are "horrible hacks".
David Dorward
@David semantics or not, tables work, CSS don't. Sometime you just have to pick the lesser evil, i.e. what gets your job done. Faux columns are a hack as they only give you the *impression* of same height columns, they don't actually stretch as you wish. Not sure what you mean with CSS tables, hope you don't mean `display: table-cell` for obvious reasons.
kemp
@kemp CSS does. Since the impression is all that matters when it comes to layout, so what? The answer suggested `display: table-cell`. It works perfectly well if you don't mind things gracefully degrading for users of IE7 and lower. IE8 supports it fine.
David Dorward
If the impression is all that matters, then you might as well use a table :) I do mind degrading for IE6/7 users. I wish I could ditch them but those who pay disagree.
kemp
Given good markup laid out with various CSS methods, the impression given to a screen reader user (for example) is pretty constant. Start using HTML tables and it isn't.
David Dorward
That's interesting, I'v never read anything concrete about screen readers not understanding tables. Do you have any link?
kemp
@David, I was asking you specifically because you were saying it can be done in pure CSS. The solutions you propose are fine, but as long as IE 6 is still around and has a market share no sane end-user focused web site operator can ignore, they are not an option just yet. Until that is the case, I will merrily use tables in these edge cases.
Pekka
@kemp They understand tables. They also understand that tables describe relationships between bits of data and can express that relationship in a fashion suitable for navigating via audio.
David Dorward
@Mathias, read the full discussion. Faux columns are fine but not a solution in every situation.
Pekka
@Pekka: Yes, but obviously, “what the OP wants” `!==` “the elements physically stretching to the same height” in this case. In the diagram the OP provided, he doesn’t mention he wants to position an element inside one of the boxes.
Mathias Bynens
@Mathias Yes, and that's why I said above that if Faux Columns solve it for the OP, that's perfectly all right. In a question like this there should be mention of the alternatives, though, in case somebody comes across it who needs physical stretching.
Pekka
@Pekka Dude... sorry if I caused this amazing comment storm. You bring up a very valid point as to positioning items within the column, however, even using Faux columns, if you set the container to `position:relative` but not the individual columns, you can still position items at the bottom of a faux column. However there does come a point where the most *pragmatic* thing to do is use a table. I don't know if it is CSS's fault though, only IE6+IE7's adoption of it :)
Doug Neiner
@Doug, no problem, I think this was a great discussion! `position: relative` won't work in all cases because you can position things relative to the *container* (= the table if you will) but not the individual *columns*. This gives you trouble if the columns are of unknown width. These are very, very rare edge cases though. Well, we just have to wait until IE6 dies out, and the problem will solve itself and we'll all use CSS tables :)
Pekka
@Pekka My thinking was that if you are using Faux Columns, you know the column width (Either pixel width or percentage) so it would still be relatively (no pun intended) easy to set the left or right value as well.
Doug Neiner
@Doug good point. Or differently put, you're *absolutely* right ;)
Pekka
@Pekka ROFL....
Doug Neiner
@Doug, well, my views are not *static*, I have no problem with being corrected. As long as the correction is *inline* with my core beliefs on clean code, of course. I tend to *block* complicated approaches when there is an easier solution available. Sometimes, a *table-row* (i.e. a row over tables) just can't be prevented. After all, we are all looking for simple solutions that get food on our *table*. All right, enough with the bad puns :)
Pekka
+3  A: 

Use the Faux Columns technique for that. Basically, you’ll have to use a repeating background image on the parent element containing both boxes.

Mathias Bynens
+1 For the answer. Faux columns is valid solution and if saved as a 8bit PNG, the file is normally around 1 or 2K.
Doug Neiner
…or even less. In most cases where I used this technique, I was able to use repeating background images with a height of 1px. Depending on the situation, the file size could be just a few hundred bytes.
Mathias Bynens
+2  A: 

This is the same as the same height columns problem, see this blog post for a solution.

Lazarus
Well, reading the article that "no hacks" in the title is rather questionable.
kemp
A: 

Hi,

what about this:

.green {
  display:block;
  float: left;
  background: green;
  width: 200px;
  height: 200px;
  position: relative;
}

.outer {
  position: relative;
  background: yellow;
  overflow: auto;
}

.pink {
  left: 250px;
  background: red;
  height: 100%;
  width: 100px;
  position: absolute;
}

Should be working.. maybe I changed the colors, the pink box is the with-growing one! ;)

edit ah ok, I missed the floating on the second box. If this is a must-have then, as mentioned above, faux columns is a good technique I think.

Joe
A: 

How about a compromise?

If faux columns can´t help you, you don't want to use tables and you already have a javascript solution, I would simply use css (display:table, display:table-cell) and put the javascript in conditional comments for IE6 and IE7.

You can then easily take out the javascript when the time is right and in the meantime, modern browsers won´t have to execute it.

jeroen