Liquid faux columns with background-size
Until the Advanced Layout and Grid Layout modules are implemented, we have to get by with the existing tricks of the trade. One of those is the use of faux columns, a background image which simulates equal-height columns. This is a good technique, but the drawback is that it only works with fixed-width columns.
That problem was overcome with the advent of liquid faux columns, which uses some background-position trickery and a bit of maths to create a variable-width effect.
With the (tentative) introduction of background-size in Safari and Opera, however, faux columns are about to become a lot easier; all you need to do is create a background image and assign a percentage value to background-size, allowing the image to expand or contract with its containing box.
Take a look at this example (only tested in Safari 3 and Opera 9.5; may work in Konqueror 3.5.7 also). If you resize your browser window, the text and columns should maintain their proportions.
The way this is done is with a simple PNG image; I’ve made it 1000px wide, with two coloured columns of 250px each, so that it’s easier to calculate column widths (25%,50%,25%).
I’ve set this as the background-image on the html element, which has been assigned a width of 90%. Inside this there is a container div with a width of 100%, and my three columns with the widths set at the same ratio as the background image:
<div id="container">
<div id="one">
</div>
<div id="two">
</div>
<div id="tre">
</div>
</div>
#container {
position: relative;
width: 100%;
}
#one {
margin: 0 25%;
}
#two, #tre {
position: absolute;
top: 0;
width: 25%;
}
#two { left: 0; }
#tre { right: 0; }
The html element has the background-size declaration applied to it, with a percentage value to make it liquid, using the browser-specific prefixes followed by the proposed declaration (for safety):
html {
background-image: url('opera_bg.png');
-khtml-background-size: 100%;
-o-background-size: 100%;
-webkit-background-size: 90%;
background-size: 100%;
background-position: 50% 0;
background-repeat: repeat-y;
width: 90%;
You’ll notice that the Webkit value is different from the others; during this test, it seems that Webkit obtains its width from the entire browser window, not from the containing element; therefore, we have to set this value to be equal to the width of the containing element. I haven’t tested this thoroughly yet, so I’m not sure if this is a persistent bug or if there’s something I’m doing wrong. Anyway, Opera 9.5 behaves as expected.
After that I’ve added the background-position and background-repeat declarations; background-repeat to tile the image down the page, and background-position because Webkit seems to ignore the margin values and puts the image at top left of the browser window; this is only necessary if you’re centre-aligning your page.
Apart from a little tidying up, that’s it; once the module becomes a recommendation and the browser-specific prefixes are dropped, it will require only a few lines of code for the simple effect. In the meantime, remember that this is for experimentation purposes only and shouldn’t be used in a live environment. This is just a sketch of the technique at the moment, and requires more testing.











Faux Columns líquido mediante CSS3…
Mediante la propiedad background-size…
That’s pretty cool and something that I see getting asked for repeatedly on forums! However, I rarely use the faux columns technique with just simple solid-coloured bg-images; there’s usually shadows or borders as part of the image which probably aren’t going to look that great when stretched and compressed.
@John: we should test how background-size and multiple backgrounds go together :)
@John: That should be easier to do with SVG backgrounds, which shouldn’t distort as much when resized. I’ve yet to seriously test it, however.
I’ve done that once, I think WebKit supports that pretty well…
The example is a bit off on Safari; I guess you meant to set -webkit-background-size to 100%, like the others?
@Daniel: No, I’d forgotten to upload the most recent example! I don’t have a copy of Safari 3 to hand right now, but that should work now.
Really interesting technique, looks nice in Opera 9.5
Let’s just hope all the browsers catch up fast enough and make the developer’s lives easier :)
[...] CSS3 ile gelen background-size yardımı ile likit aksak kolon sorunun çözümü. Link [...]
i advise ageinst using the background-image solution, if you got the time to experiment.
Im just saying without going into to much detail:
It is indeed possible to achive in other more effective ways.
Btw, the examble dosent display the intended way for me, and im using IE7 as my standard browser.
I’m aiming for IE6-7, FF and Safari, with the above.
[...] Liquid faux columns with background-size - CSS3 . Info (tags: layout) [...]
[...] 3.Liquid faux columns with background-size the CSS3 way [...]