HTML5 Series – Flexbox

If you’ve ever done any WPF, Silverlight, Windows Phone or Windows 8 development using xmal then you will appreciate how good it is to have controls like the StackPanel and Grid, and that HTML is rather lacking in its layout functionality. True you can produce most layouts but it just seems needlessely harder.

Thankfully HTML5 improves on this situation by introducing some new CSS properties.
The first is flexbox. When you define a div as a flexbox you can set its containing items to either space themselves out evenly, or you can set ratio values on each item to say how big it should be compared to the other items in the flexbox and they will stretch to fill the flebox.

Examples
In this example the flexbox is set to distributive which means the containing items will keep there size but distribute themselves evenly in the flexbox.

The key CSS is where the class example 1 sets the display to -ms-flexbox which makes example 1 div a flexbox container, and -ms-flex-pack: distribute, which set the flex mode to distribute the containing items evenly.

    <style type="text/css">
        .Example1 {
            display:-ms-flexbox;
            -ms-flex-pack:distribute;
            width:400px;
            border:solid 1px black;
        }
        .Example1 div:nth-child(1) {
            background-color: red;
            min-width:80px;
            min-height:80px;
        }
        .Example1 div:nth-child(2) {
            background-color: green;
            min-width:80px;
            min-height:80px;
        }
        .Example1 div:nth-child(3) {
            background-color: blue;
            min-width:80px;
            min-height:80px;
        }

    </style>
 <div class="Example1">
        <div></div>
        <div></div>
        <div></div>
    </div>

Flexbox-Distribute

In the next example the containing items have had ratios set so that the second item is twice the width of the first and the third remains at a fixed size.

The key CSS to look at here is on the child div’s styles where it says -ms-flex: 1 or -ms-flex: 2.

    
    <style type="text/css">
        .Example2 {
            display:-ms-flexbox;
            width:400px;
            border:solid 1px black;
        }
        .Example2 div {
            min-width: 80px;
            min-height: 80px;
        }
        .Example2 div:nth-child(1) {
            background-color: red;
            -ms-flex: 1 0 auto;
        }
        .Example2 div:nth-child(2) {
            background-color: green;
            -ms-flex: 2 0 auto;
        }
        .Example2 div:nth-child(3) {
            background-color: blue;
            min-width:80px;
        }
    </style>
    
    <div class="Example2">
        <div></div>
        <div></div>
        <div></div>
    </div>

Flexbox-Ratios

The third example shows how these techniques also work vertically as well as horizontally. See -ms-flex-direction:column on the containing div style.

    <style type="text/css">
        .Example3 {
            display:-ms-flexbox;
            -ms-flex-direction:column;
            width:400px;
            border:solid 1px black;
        }
        .Example3 div {
            min-width: 80px;
            min-height: 80px;
        }
        .Example3 div:nth-child(1) {
            background-color: red;
            -ms-flex: 1 0 auto;
        }
        .Example3 div:nth-child(2) {
            background-color: green;
            -ms-flex: 2 0 auto;
        }
        .Example3 div:nth-child(3) {
            background-color: blue;
            min-width:80px;
        }
    </style>
    <div class="Example3">
        <div></div>
        <div></div>
        <div></div>
    </div>

Flexbox-Verticle

Lastly in the fourth example I’ve shown how items can be set to wrap when they fill up a line with -ms-flex-wrap:wrap;

    <style type="text/css">
        .Example4 {
            display:-ms-flexbox;
            -ms-flex-wrap:wrap;
            width:400px;
            border:solid 1px black;
        }
        .Example4 div {
            min-width: 80px;
            min-height: 80px;
        }
        .Example4 div:nth-child(1) {
            background-color: red;
        }
        .Example4 div:nth-child(2) {
            background-color: green;
        }
        .Example4 div:nth-child(3) {
            background-color: blue;
        }
        .Example4 div:nth-child(4) {
            background-color: yellow;
        }
        .Example4 div:nth-child(5) {
            background-color: orange;
        }
        .Example4 div:nth-child(6) {
            background-color: violet;
        }
    </style>
    <div class="Example4">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>

Flexbox-Wrap

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s