Have been searching the net for the past hours to find a solution to this, but couldn't find it, so here I am.
I need the width of the div
to be 100%
minus the width of the left div
.
So that the div
to the left of it stays the same width (390px) but the other div
adjusts its size depending on the resolution . I have found the solution where it has a fixed width div
on both sides, but just cant figure this out.
This type of calculation isn't currently supported in CSS (certainly not in Chromium 12/Ubuntu 11.04), but there is a
calc()
function defined in CSS 3, which would allow for this kind of behaviour, using simple mathematical functions:(Above example taken directly from the W3.org.)
My own (in-exhaustive) tests show:
The above results were obtained using the named browsers and a css
calc()
demo, the code of which is below:HTML:
CSS:
(If anyone would like to run the above test in the browsers on their platform, and supply the results, or edit them in to this answer, that would be much appreciated.)
As pointed out, by clairesuzy, in comments:
And, indeed, in Firefox 5 (Ubuntu 11.04) it does work (the other vendor prefixes don't appear to do anything for Opera or Webkit, though; sadly): Revised vendor-prefixed demo.
Reference:
There really isn't a way of doing this with straight up CSS right now in browsers other FireFox (see the MDN docs). You could use javascript to do the same, but I'd suggest rethinking your layout.
EDIT: actually IE 9 can handle it as well, see MSDN docs. Yay for IE?
Maybe this is not directly related to the question but I had a similar problem to arrange items in a list. The fixed-width element is to the right of each item. I've managed to solve this with the following approach. The idea is to balance a positive
padding-left
with a negativemargin-left
, while the width is set to100%
:Simple solution:
CSS:
This will allow you to have a fixed sidebar width and a full width content area. I have used it many times and it works like a charm.
float the left div and make a new block formatting context out of the right div (overflow: hidden is one way to do that), that way it will take the remaining space
Example Fiddle