HTML and CSS Reference
In-Depth Information
In-Document Declarations
In HTML 4, the pragma directive should be used at the top of the head element in the form shown in Listing 2-2.
Listing 2-2. Declaring the Character Encoding with the Pragma Directive
<meta http-equiv="Content-type" content="text/html; charset=UTF-8 ">
The previous declaration can be used in HTML5 as well, but HTML5 introduced another option, namely the meta
charset attribute (see Listing 2-3). Either of them can be used, but only one at a time. The whole declaration must fit
within the first 512 bytes of the page.
Listing 2-3. HTML5 meta charset
<meta charset="UTF-8" >
The encoding declaration of XHTML documents depends on which MIME type they are served with. If they are
served as text/html , the pragma directive in Listing 2-2 can be used at the top of the head element.
XHTML documents served as XML can use the encoding information of the XML declaration (see Listing 2-4)
in the first line of the document (see Chapter 3).
Listing 2-4. Setting the Character Encoding in XML Documents
<?xml version="1.0" encoding="utf-8" ?>
The XML declaration is required for all XML documents that use character encoding other than UTF-8 or UTF-16
or when the encoding is missing from the HTTP header (see later in Chapter 3).
Declaring Character Encoding for CSS
Character encoding declarations can usually be omitted in style sheets. The encoding of external CSS files needs to be
declared if and only if there are non-ASCII characters in the file. The best practice is to use characters from the English
alphabet only, or write non-Latin characters in descriptive selector names or other CSS content without accents.
HTTP Header Declarations
CSS encoding can also be declared in the HTTP Content-Type header. For example, if the character encoding is
UTF-8, the HTTP declaration looks like Listing 2-5.
Listing 2-5. Declaring the Character Encoding for CSS (Rarely Used)
Content-Type: text/css; charset=UTF-8
Using an in-document declaration on top of the HTTP declaration can guarantee that the encoding of the
external CSS file can be determined even if the file is moved or used locally (see the next section).
The character encoding declared in the HTTP header should coincide with the one declared in the CSS file, and
the first one has higher precedence.
Search WWH ::

Custom Search