HomeSoftware TutorialHow does a browser obtain the encoding of a CSS document?
How does a browser obtain the encoding of a CSS document?

Getting the encoding of a CSS document is much more complex than getting the encoding of an HTML document. There are several places where you can set the encoding: the Content-type field of the ResponseHeader, the charset attribute of the link tag, the CSS document itself, and finally, a default encoding document.

Influence

What are the consequences if the encoding of a CSS document is incorrectly recognized? It can normally recognize English characters, but Chinese characters will be displayed as garbled text. This is mainly because a Chinese font is used, but the Chinese font displayed on the page is an English font (the content is still Chinese; I'm referring to the change in the displayed font).

What is the order in which browsers retrieve the encoding of CSS documents?

According to the description in [1]? CSS 2.1 specification, the encoding of an external CSS file should be determined according to the following priority:

1. The encoding specified by the "charset" parameter in the "Content-Type" field of the HTTP response header.

2. The encoding defined by BOM and/or @charset.

3.<link charset="">The encoding specified by metadata provided by other linking mechanisms (if any).

4. The encoding already determined in the HTML of the CSS file or another CSS file (if any).

5. If the above steps fail to determine the encoding, then assume that the encoding is UTF-8.


Here's a method (C#) for retrieving the encoding from the BOM:

///  /// Determine the encoding from the byte stream (returning null means the encoding cannot be determined) ///  /// Input byte stream ///  internal static string GetEncodingByByte(byte[] bt) { // UTF-8 with BOM var utf8 = new byte[] { 0xEF, 0xBB, 0xBF }; if (bt[0] == utf8[0] && bt[1] == utf8[1] && bt[2] == utf8[2]) { return "utf-8"; } // UTF-32-BE var utf32Be = new byte[] { 0x00, 0x00, 0xFE, 0xFF }; if (bt[0] == utf32Be[0] && bt[1] == utf32Be[1] && bt[2] == utf32Be[2] && bt[3] == utf32Be[3]) { return "utf-32"; } //UTF-32-LE var utf32Le = new byte[] { 0xFF, 0xFE, 0x00, 0x00 }; if (bt[0] == utf32Le[0] && bt[1] == utf32Le[1] && bt[2] == utf32Le[2] && bt[3] == utf32Le[3]) { return "utf-32"; } //UTF-32-2143 var utf322143 = new byte[] { 0x00, 0x00, 0xFF, 0xFE }; if (bt[0] == utf322143[0] && bt[1] == utf322143[1] && bt[2] == utf322143[2] && bt[3] == utf322143[3]) { return "utf-32"; } //UTF-32-3412 var utf323412 = new byte[] { 0xFE, 0xFF, 0x00, 0x00 }; if (bt[0] == utf323412[0] && bt[1] == utf323412[1] && bt[2] == utf323412[2] && bt[3] == utf323412[3]) { return "utf-32"; } //UTF-16-BE var utf16Be = new byte[] { 0xFE, 0xFF }; if (bt[0] == utf16Be[0] && bt[1] == utf16Be[1]) { return "utf-16"; } //UTF-16-LE var utf16Le = new byte[] { 0xFF, 0xFE }; if (bt[0] == utf16Le[0] && bt[1] == utf16Le[1]) { return "utf-16"; } return null; }
Software Applications: Front-end template download; development and design; SEO optimization; offline web browsing. Note: This software is not a hacking program and cannot download backend data!
Disclaimer: This service is for personal study, research, or enjoyment purposes only, and is for non-commercial, non-profit use. Users must comply with copyright laws and related regulations and must not infringe upon the legitimate rights and interests of this website and related rights holders. Any risks arising from the use of this tool are the sole responsibility of the user and are not the responsibility of the software itself.
Copyright © 2019-2026 Xiaofeitu Software. All Rights Reserved. Guangdong ICP Registration No. 19111427-2
Tutorial User Manual Website Special