HTML5 Series – Fonts

It’s an all to common story, designer designs a website hands it to a developer who then goes, “what kind of font is that?” and get’s the response of some completely unheard of name. The developer then try’s to explain what a web safe font is and they agree to use Arial.

With CSS3 custom fonts are now a reality. On the item you are styling you still set the font family in the same way but additionally in your CSS you specify a @font-face rule so the browser can find out what the font is. The @font-face rule should specify a name and the src of the font. e.g.

@font-face { 
     font-family: myFont;
     src: url('myFontFileName.ttf');
}

One point to note different browsers support different font type. Luckily though you can specify multiple sources for your font.

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

HTML5 Series – Columns

I’m not sure I’ve ever needed to add columns where text flows from one into the other, but if your doing Windows 8 development where you would want a sideways scroll rather than vertical this could be quite useful.

To create columns using CSS3 you simply set the columns property to either the number of columns you want or the width each columns should be. e.g. This would produce 4 columns.

<style type="text/css">
        div {
            columns: 4;
        }
</style>

There are a couple other properties that are also quite useful when using columns.

Column-Gap Specifies the size of the gap between columns
Column-Fill Can either be set to auto where columns are filled sequentially and have different lengths, or balance in which case the text will be balanced so each column is even. The default is balance.

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