CSS3 Flexbox « Isotoma Blogcss Friday, June 17, 2011
CSS3 Flexbox « Isotoma Blog
Extracted Page: http://blog.isotoma.com/2010/08/css3-flexbox/CSS3 Flexbox « Isotoma Blog
CSS3 brings us a host of new features. Whilst most people will be familiar with gradients, text-shadow etc. I’d like to draw your attention to one of the lesser known modules in CSS3: flexbox. What’s interesting about Flexbox is that you can use it now if you use a Gecko or Webkit based browser (no IE sadly and Opera’s support isn’t there yet).
What does Flexbox bring us that we didn’t have before? Well it gives us a whole raft of new ways of controlling layout and flow. What we now achieve with floats we can do far more successfully and with more control with Flexbox.
I’ve never liked floats, they always seemed a hacky way of structuring HTML. Unfortunately with the meager positional CSS available, floats ended up being the best way of creating flexible fluid layouts. Let’s look at some examples.
To define an element as a flexbox we set the display to box.
Notice that by default flexbox elements are aligned horizontally. If we want to change that to vertical:
Box-orient is inherited i.e. child elements will also be aligned horizontally.
Observant viewer will have noticed that Firefox (3.6.8) ignores the width and height setting on the child boxes, whilst Webkit respects it but overflows the content.
So if you want child elements to be aligned vertically, you’ll need to set the containing element to box-orient:vertical.
We can reorder elements as we like, using box-ordinal-group.
The immediate application of this should be obvious, for the first time we have way of defining display order without reverting to all the hacks we’ve had to use up to now (floats, relative positioning, negative margins etc). Powerful stuff.
Box-flex allow the content to expand to fill the available space.
Notice once again the difference between Webkit and Gecko; whilst Webkit expands the main block to the browser window, Gecko expands to fit the content.
Box-flex fills space in proportion to the value given. Quoting the specs:
“All flex is relative. For example, a child with a box-flex of 2 is twice as flexible as a child with a box-flex of 1.”
To explain this better lets look at an example:
For ease of calculation I’ve given the H1 child element a width of 50px, and the containing element a width of 600px. So the available space is:
600px – 150px = 450px
Working out the ratio is:
1 + 2 + 7 = 10
450px / 10 = 45px
box 1 width: 50px + (45px * 1) = 95px
box 2 width: 50px + (45px * 2) = 120px
box 3 width: 50px + (45px * 7 ) = 365px
Further more you can set relative spacing whilst having fixed width paddings and margins. If you tried that with floats you’d either have to use percentage margins or have boxes bouncing all over the place.
Box-align works in the opposite direction to the orient setting. i.e. if the element is set to horizontal, box-align controls alignment in the vertical and vica versa.
This reverses the order of elements.
Notice the inconsistency between Gecko and Webkit again. Gecko aligns the right hand column, whilst Webkit aligns to left but reorders the elements.
Box-pack controls alignment in the direction set by orient.
Justify is a nice attribute, equally spacing elements within the parent container.
The classic 3 column layout. Notice the navigation appearing first, also note that the background colour extends to the full height of all boxes.
Sadly flexbox is not supported by IE and looks unlikely to be supported by IE9. So it will be a while before these techniques become mainstream.