Home > Article > Web Front-end > What to do if css web page is garbled
Solutions to garbled css web pages: 1. Set the CSS character encoding to be consistent with the character encoding of the page, and set encoding statements such as "@charset "utf-8";"; 2. Strengthen when writing css Note; 3. Use the alias of the font.
Recommended: "css video tutorial"
Solution one:
The garbled characters that appear in CSS are caused by the inconsistency between the CSS character encoding and the character encoding of the page. Therefore, the most direct method is to make the character encoding consistent. Specify the encoding type in CSS, for example: @charset "utf-8"; (The specified encoding type is utf-8, which must be written on the first line of the CSS file)
Solution 2:
The garbled characters in CSS are all caused by Chinese characters, so as long as Chinese characters are not written, there will be no such situation as "garbled characters causing CSS failure".
Putting aside the above two solutions, when we dig deeper, we will find that "garbled codes" usually come from the following two situations.
1. Garbled codes caused by Chinese comments
Garbled code examples:
Normal code: /*three Chinese characters*/
Garbled codes caused by:/*Juanxi罽盛?/
The above example shows that the garbled code blocks the end character of the CSS comment, so that the following CSS content is within the comment range, resulting in CSS failure
Preventative measures: Strengthen comments
Example:
Normal code:/****Three Chinese characters****/
Caused garbled code:/****Juan 狋罽瀛?***/
This This enhanced version of the comment can prevent garbled characters from "mutating" the final end character of the comment, and can prevent it in advance when writing CSS
2. Chinese fonts cause garbled characters
garbled code example:
Normal code: font-family:"黑体"
Garbled code caused by: font-family:"翈戋"
The garbled code in the above example causes the font name to become garbled, resulting in The specified font is invalid. The consequences of this problem do not seem to be very serious, but in actual situations, there is indeed a situation where the garbled characters "mute" the following quotation marks, so that the following CSS is within the quotation marks of the font, so that all subsequent CSS becomes invalid.
Precautionary measures: Use aliases of fonts (so browsers can recognize them)
Example:
Normal code: font-family: "SimHei" (font-family: "\9ed1\4f53" )
Browser analysis: font-family: "SimHei" (font-family: "黑体", IE6 is still font-family: "\9ed1\4f53" but the font analysis shows In bold)
uses aliases to avoid using Chinese, thus avoiding garbled characters.
The above is the detailed content of What to do if css web page is garbled. For more information, please follow other related articles on the PHP Chinese website!