Declaring Character Encoding

Now that you understand what character encoding is and why it is needed, choosen a character encoding for the web documents on the website it is time to declare the character encoding.

Ways to Declare Character Encoding

There are 3 ways to declare character encoding:

  1. At the server
  2. Using a meta tag
  3. XML declaration (beyond the scope of this site for more information see references and further reading at the end of this article)

Declaring Character Encoding at Server Level

Declaring character encoding at the server level is preferred. When the web document is "served" by the server the server sends what is called a HTTP header. The browser takes the information sent and prepares to render the web page/document.

You will have to check with your hosting company to see if this is possible. A web server that has multiple users may not be sending a HTTP header at all due to the variety of customers it has or may not allow custom HTTP headers for the same reason.

If the website is hosted on a web server using Apache this can be done using the htaccess file. Again, make sure your web host permits this. In the htaccess file a line can be added as follows:

Content-Type: text/html; charset=iso-8859-1

where charset= has your chosen character encoding

Declaring Character Encoding using Meta Tag

If you are not able to declare the character encoding at the server level the character encoding of the web document can be declared using a meta tag. This must be right after the <head> tag to avoid the browser reparsing the document.

Including the character encoding declaration as a meta tag even though it is already declared at the server level is a good idea:

To declare character encoding using a meta tag you insert a meta tag as follows right after the <head> tag:

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

where charset= is the character encoding you have chosen

Note: The example above is the HTML coding for this meta tag. If you are coding using XHTML, adjust the end of the tag accordingly.

You have character encoding declared at the server level (if possible) and in the web documents of the website but what about the external Cascading Style Sheet (CSS)? Character encoding should be declared in the Cascading Style Sheet (CSS) also.

Declaring Character Encoding in External Stylesheets

For the same reasons you include a character encoding declaration in the web documents within a website you should include a character encoding in the external stylesheets (CSS) for the site. If internal stylesheets are used instead, this is not necessary.

To declare character encoding in an external stylesheet you would add the following at the very top of the file:

@charset "utf-8";

where "utf-8" would be the character encoding chosen for the website

Character Encoding Related Articles

If you found this web page a useful resource for your own website please link as follows:

HTML Basic Tutor - www.htmlbasictutor.ca/

Web design practices and coding in a fashion small business owners, beginner web designers and hobbyist web site owners with little or no knowledge of web design can learn from.
URL: