HTML5 Series – Grid

There was a time when an HTML page layout mainly consisted of tables, and lots of them. Tables went inside other tables and complex layouts were achieved through the use of colspans and rowspans. The only problem though was it was a mess.

By using tables the HTML mark-up was dictating the design/layout of the page and if you ever wanted to change anything it was a huge task to undertake. Since then the world has moved on and now most webpages consist mainly of div’s. But what if you did want to quite a rigid layout with columns and rows?

HTML5 introduces the Grid style through CSS. Your page can still consist mainly of div’s (or anything for that matter), but in your css you set a display property of grid (for now in IE it’s -ms-grid). You can then specify a grid-columns and grid-rows property to create a grid within your div.

Child items can be placed in grid cells through grid-row and grid-column properties in the CSS, and if you want something to span more than one cell you can use grid-row-span or grid-column-span.

If you’ve done any XMAL programming (WPF/Silverlight/Win8/Windows Phone) then this should all sound very similar to XMAL’s Grid control.

The example below creates something like this:

GRid

    <style>
            /* Set a grid with 3 columns and 2 rows */
            div {
                display: -ms-grid;
                -ms-grid-columns:200px 200px 200px;
                -ms-grid-rows:200px 200px;
            }

            /* Position the child div's in the grid */
            div:nth-child(1) {
                -ms-grid-row: 1;
                -ms-grid-column: 1;
            }
            div:nth-child(2) {
                -ms-grid-row: 2;
                -ms-grid-column: 1;
            }
            div:nth-child(3) {
                -ms-grid-row: 1;
                -ms-grid-column: 2;
                -ms-grid-row-span:2; /* The middle cell spans 2 rows */
            }
            div:nth-child(4) {
                -ms-grid-row: 1;
                -ms-grid-column: 3;
            }
            div:nth-child(5) {
                -ms-grid-row: 2;
                -ms-grid-column: 3;
            }

            /* Add a border to the cells */
            div>div {
                border: solid 1px black;               
            }
    </style>
    <div class="myGrid">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>

It’s also worth noting that the column/row sizes can be specified using fractions rather than fixed sizes. e.g. The following will produce a grid 600px wide where the middle column is twice the width of the other 2.

    <style>
            /* Set a grid with 3 columns and 2 rows */
            div {
                display: -ms-grid;
                width:600px;
                -ms-grid-columns:1fr 2fr 1fr;
                -ms-grid-rows:200px 200px;
            }
    </style>

For another great example check out this hands on page from Microsoft Hands on: CSS3 Grid

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