Saturday 26 January 2013

HTML5 & CSS document encoding

I always knew it was right and proper to declare the type of encoding in a HTML document. Specifying the encoding type means that characters in the content are correctly interpreted and displayed.

HTML5 has a brand new attribute, charset which makes the declaration cleaner and simpler than the old method.  In the past (HTML 4.01) we would have written:

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">

Now in HTML5 we write:

    <meta charset="UTF-8">

How easy is that? Additional stuff you might want to know:
  1. UTF-8 - Specifies character encoding for Unicode.  This should be your default encoding type unless you have some specific need to have another encoding type e.g. to specify another character set.
  2.  Metadata is used to provide information about the HTML document.  It is not displayed on the page, but is able to be interpreted or parsed by browsers or search engines.
  3. <meta> tags always go inside the <head> element.  The encoding needs to be within the first 1024 bytes of the document, so place it immediately inside the <head> tag.
  4. An alternative to the meta charset element is to use a Unicode Byte Order Mark (BOM) character at the start of the file.  I do not recommend this system over the <meta> method.
What you didn't know....

You should also specify the encoding type in your external CSS documents!  When I read this I had a really? moment. I hadn't known that.  Now I do - and so do you.

Like the HTML5 spec above, the encoding for CSS documents should appear at the very start of the document.  Here's how to write the encoding:

    @charset "UTF-8";

This declaration tells the browser to read the following CSS file as UTF-8.  Easy.

No comments:

Post a Comment