This topic covers Character sets & encodings in XHTML, HTML and CSS
Basic
If a user agent (eg. a browser) is unable to detect the character encoding used in a Web document, the user may be presented with unreadable text. Therefore declaring the character encoding of the document is important for anyone producing XHTML/HTML or CSS. This chapter explains best practice. These are only snippets from the advice given by Richard Ishida, of W3C, at: http://www.w3.org/International/tutorials.
Objectives
This tutorial provides advice in the following areas :
choosing an encoding for XHTML/HTML documents
when and how to declare the character encoding (charset) for documents using XHTML/HTML and CSS
aspects of serving and coding XHTML/HTML files that affect the above
when and how to use escapes and entities to represent characters
The tutorial attempts to assist newcomers to this area by incorporating explanations of the basic concepts needed to understand the advice given.
Essential definitions
This section covers
Unicode
Character sets, coded character sets, and encodings
Document character set
Character escapes
Unicode
Unicode is a universal character set, ie. a standard that defines, in one place, all the characters needed for writing the majority of living languages in use on computers. It aims to be, and to a large extent already is, a superset of all other character sets that have been encoded, in particular the characters peculiar to each of many languages.
Character sets, coded character sets, and encodings
A character set or repertoire is the set of characters designed for a particular purpose. Examples are Roman alphabet, Russian alphabet, Chinese characters, mathematics.
A coded character set is a set of characters for which a unique number has been assigned to each character. Units of a coded character set are known as code points. For example, in utf-8. the code point for the letter ŕ in the Unicode coded character set is 225 in decimal, or E1 in hexadecimal notation. (Note that hexadecimal notation is commonly used for identifying such characters, and will be used here.)
Put simply:
A character set is a set of characters chosen for any particular purpose.
A code point is a number representing a character.
A coded character set is a set of characters which have been given code points.
One character set, multiple encodings
UTF-8 uses 1 byte to represent characters in the ASCII set, two bytes for characters in several more alphabetic blocks, and three bytes for the rest of the BMP. Supplementary characters use 4 bytes.
UTF-16 uses 2 bytes for any character in the BMP, and 4 bytes for supplementary characters.
UTF-32 uses 4 bytes for all characters.
In the following chart, the first line of numbers represents the position of the characters in the Unicode coded character set. The other lines show the byte values used to represent that character in a particular character encoding.
A
D790;**
E5A5BD;**
Code point
U+0041
U+05D0
U+597D
U+233B4
UFT-8
41
D7 90
E5 A5 BD
F0 A3 8E B4
UTF-16
00 41
05 D0
59 7D
D8 4C DF B4
UTF-32
00 00 00 41
00 00 05 D0
00 00 59 7D
00 02 33 B4
** For your browser to render these characters, their scripts must be installed on your computer (which is unlikely).
Document character set
HTML and XML documents can contain only characters defined by Unicode. Any encoding can be used for your document as long as it is properly declared and a subset of the Unicode repertoire.
(Note that browsers do not necessarily display all Unicode characters. Numerous scripts, such as Arabic and Indic, require additional rules to transform the character sequence in memory to an appropriate sequence of font glyphs for display.)
XHTML & MIME types
Every file sent across the Internet has attached to its front end a header carrying lots of information to tell the user's computer about the incoming file. When you send an email addresses: http://wotsit@hotmail.com, the "http" at the beginning declares an http header-type.
Inside that http-header might be the line: Content-Type: text/html; charset=utf-8 which obviously declares the content-type and the character-set.
This is a bit like a friend sending you an email: "I am going to contact you by phone, and I will skype your mobile." Your mind then switches from emails to phones. You can ignore your landline phone but remember to switch on your mobile. The email (or header) proves very helpful.
Before describing how to declare character encodings in XHTML or HTML and CSS we need to review some aspects of how servers send the information to the user agent, and how common user agents handle the markup they receive.
This information is expressed using a MIME type label.
A server normally sends HTML 4.01 files with a MIME type of text/html. HTML is an SGML application.
Things are not so straightforward when dealing with XHTML 1.0, because XHTML is XML-based.
Many people prefer to use XHTML because of the advantages XML brings for editing or processing of documents. However, there is still a lack of support for XML files in mainstream browsers, so many XHTML 1.0 files are actually served using the text/html MIME type. In this case, the user agent will treat the file as HTML.
To ensure that the slight differences between XML and HTML do not trip up older user agents, you should always follow the compatibility guidelines in Appendix C of the XHTML specification when serving XHTML as HTML. These compatibility guidelines recommend, amongst other things, that you leave a space before the '/>' at the end of an empty tag (such as img, hr or br), that you always use both id and name attributes for fragment identifiers, etc, even tho the name attribute has now been deprecated.
XHTML 1.0 can also be served as XML but the later version, XHTML 1.1, is always served as XML.
To serve XHTML as XML you use one of the MIME types application/xhtml+xml, application/xml or text/xml. The W3C recommends that you serve XHTML as XML using only the MIME type, application/xhtml+xml. Thus XHTML can be served as HTML or XML, each bringing its own rendering.
To have our pages rendered consistently, requires declarations. (Read on.)
Language:
Served with MIME type:
Treated as:
HTML
text/html
HTML
XHTML
application/xhtml + xml
XML
application/xml
text/xml
You need a DOCTYPE declaration
A DOCTYPE declaration is: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
This advises your user's browser to conform to the W3C standard (screen-dump 1). Omission allows the non-standard behavior of older browser (screen-dump 2).
The two pictures show two pages with exactly the same markup and CSS styling, but the left one has a DOCTYPE declaration, and the right one does not.
Examples of differences between DOCTYPE and non-DOCTYPE:
With DOCTYPE, the CSS width setting applied to the div does not absorb any widths set for padding and border settings, whereas in non-DOCTYPE mode it does - which is why the large box is wider in the left-most (standards) picture.
With DOCTYPE, the table inherits the font size setting from the body element, but in non-DOCTYPE it does not, so the font-size is different.
The XML declaration
The XML declaration appears as the very first line, at the top of the file:
<?xml version="1.0" encoding="UTF-8"?>
An XML declaration is required for an XML document :
if the encoding is not provided by a higher level protocol, ie. the HTTP header
AND IF you are not encoding in utf-8 or utf-16.
This XML declaration causes difficulties with some browsers, so stick to utf-8 and omit the XML declaration.
Since your HTML and XHTML files might be served as text/html: always include a meta charset declaration as close as possible to the top of the head element:
CSS style sheets
Declare the encoding of external CSS style sheets. (It is not necessary for CSS embedded in a document.) This is done by adding a statement to the top of the file such as:
@charset "utf-8";
Note that this must be the very first thing in the file. It is particularly important if your style sheet contains non-ASCII values for the content property, or refers to non-ASCII element or attribute names or values, but it will also become more important in the future to use such a declaration with any CSS file.
Note: Some editors, such as Windows Notepad, add the optional UTF-8 signature or byte order mark (BOM) at the beginning, causing some browsers to fail to recognize the initial statement in your CSS file. Change your text editor. (?) As long as you use utf-8, this loss of the first line declaring the encoding, should not be a problem.
Precedence rules
In the case of conflict between multiple encoding declarations, precedence rules apply to determine which declaration wins out. For XHTML and HTML, the precedence is as follows, with 1 being the highest
HTTP Content-Type
XML declaration
meta charset declaration
link charset attribute
The fourth item here is a method of declaring the encoding of a file that we have not yet mentioned. A charset attribute can be added to an a element to indicate the encoding of the file being linked to. In general, this approach is not recommended, since it is likely to provide incorrect information if the encoding of the target file is changed.
The high precedence of the HTTP header is useful, as mentioned earlier, in situations where the encoding of the document is changed by an intermediary server, since that transcoding is unlikely to change the in-document declarations. The transcoding server should declare the new encoding in the HTTP header.
For external, linked CSS style sheets the precedence rules are:
HTTP Content-Type
@charset rule
The same comments about charset attribute (this time on the link element) and transcoding apply equally here.
Entities and Numeric Character References (NCRs)
NCRs, or Numeric Character References, and entities are ways of representing any Unicode character in XHTML / HTML using only ASCII characters. For example, the following are different ways of representing the character á
Escapes
Escapes are codes for standard symbols.
The browsers then convert these codes back into those symbols for the user's screen.
If I type:Add <br />, your browser will interpret <br /> as a line-break, and all you will read is:Add. But if use escapes, as in:Add <br />.then your browser will screen:Add <br />.
There are three characters which should always appear in content as escapes, so that they do not interact with the syntax of the markup
< (for <)
> (for >)
& (for &)
As a self-test, write this last paragraph as I have written it for your browser.
You might use the double-quote (") as " - particularly in attribute text when you need to use the same type of quotes as you used to surround the attribute value.
Types of escapes
á: A hexadecimal NCR. NCRs are a type of escape. All NCRs begin with and end with ;. The x indicates that what follows is a hexadecimal number representing the scalar value of a Unicode character, ie. the number assigned in the Unicode code charts.
á: A decimal NCR. This uses a decimal number to represent the same scalar value.
á: A character entity. Very different. All entities need to be predefined in the markup language definition (DTD), so this approach is only available for those characters that HTML 4.01 has specifically chosen to represent as entities. That includes only a small subset of the Unicode range. Note that the entity name is case sensitive: Á/Á = Á and á/á = á.
á
á á á \E1
hex NCR decimal NCR character entity CSS escape
When to use escapes
Use escapes:
only in exceptional circumstances
in style attributes
in css stylesheets
Note that values of numeric character references (such as ǵ and ǵ for ǵ?) are interpreted as Unicode characters - no matter what encoding you use for your document.
Escapes only in exceptional circumstances
Using escapes can make it difficult to read and maintain source code, and can also significantly increase file size. Many English-speaking developers have the expectation that other languages only make occasional use of non-ASCII characters, but this is wrong.
Take for example the following passage in Czech.
Jako efektivnĕjší se nám jeví pořádání tzv. Road Show prostřednictvím našich autorizovanǽch dealerů v Čechách a na Moravě, které proběhnou v průbůhu zá ří a října.
If you were to require NCRs for all non-ASCII characters, the passage would become unreadable, difficult to maintain and much longer. It would, of course, be much worse for a language that didn't use Latin characters at all.
Jako efektivnĕjší se nám jeví pořádání tzv. Road Show prostřednictvím našich autorizovanǽch dealerů v Čechách a na Moravě, které proběhnou v průbůhu zá ří a října.
It is much better to use an encoding that allows you to represent the characters in their normal form.
An example of an ambiguous character is 00A0: NO-BREAK SPACE. This type of space prevents line breaking, but it looks just like any other space when used as a character. Using (or  ) makes it quite clear where such spaces appear in the text.
Escapes in style attributes
Style attributes occur in 5 places:
Best practice puts style information in an external style sheet.
Next comes a style element within the head of an XHTML or HTML file
Occasionally, or perhaps on a temporary basis, you may use a style attribute on a particular element, such as <div style="color:red">.
Even more rarely, you may want to represent one or more characters in the style attribute using character escapes.
A style attribute in XHTML or HTML can represent characters using NCRs, entities or CSS escapes. On the other hand, the style element in HTML can contain neither NCRs nor entities, and the same applies to an external style sheet.
Because there is a tendency to want to move styles declared in attributes to the style element or an external style sheet (for example, this might be done automatically using an application or script), it is safest to use only CSS escapes.
For example, it is better to use: <span style="font-family: L\FC beck">... </span>
than
<span style="font-family: Lübeck">... </span>
Escapes in css stylesheets
The escape mechanism for representing characters in CSS is a backslash followed by a hexadecimal number representing the Unicode scalar value. Note that these escapes are terminated by a space, not a semi-colon. The CSS escape for á is \E1.
Also bear in mind...
If you use entities (such as á) to represent characters to be processed by XML tools, or converted to XML, these entities must be declared in the Document Type Definition to work. For this reason, it may be safer to use numeric values. Use á instead of á.
For line and paragraph separators, use <xhtml:br />, instead of [Shift+Return] or ^l, and <xhtml:p>, instead of [Enter/Return] or ^p.