简体   繁体   English

UTF-8编码,正确的方法

[英]UTF-8 encoding, the right way

I'm curious, as to what is the right way to have UTF-8 encoding on all web files 我很好奇,在所有Web文件上使用UTF-8编码的正确方法是什么

All my files (incl. CSS and JS) are made and saved in UTF-8 encoding 我所有的文件(包括CSS和JS)均已制成并保存为UTF-8编码

In PHP, I set the char-set on top of the main page (this page includes all others) with: 在PHP中,我在主页(此页面包括所有其他页面)的顶部设置了字符集,方法是:

header('Content-type: text/html; charset=utf-8');

At the same page I have this html meta tag: 在同一页面上,我有以下html meta标签:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Then I stubled upon an external css file that has this on first line: 然后,我在一个外部的css文件上打了个草稿,该文件在第一行中有:

@charset "UTF-8";

And now I wonder, should I set the charset INSIDE all my CSS/JS files too, like that? 现在我想知道,是否也应该像我一样在所有CSS / JS文件中设置字符集?

And/or should I serve each file with charset=utf-8 in the meta tag? 和/或我应该在meta标签中为每个具有charset = utf-8的文件提供服务吗? EDIT: this is obsolete for HTML5 编辑:这对于HTML5已过时

You only need to do it if you have non-ascii characters. 仅当您使用非ASCII字符时才需要这样做。 Also, watch out for the note below... the file has to be saved as UTF8 as well. 另外,请注意以下注释...文件也必须另存为UTF8。

From: http://www.w3.org/International/questions/qa-css-charset.en.php 来自: http : //www.w3.org/International/questions/qa-css-charset.en.php

If you have any non-ASCII text in your CSS file, for example non-ASCII characters in font names, in values of the content property, in selectors, etc., you need to be sure that the CSS parser knows how to transform the bytes into characters correctly, so that it understands your CSS code. 如果CSS文件中有任何非ASCII文本,例如字体名称,内容属性的值,选择器等中的非ASCII字符,则需要确保CSS解析器知道如何转换字节正确地转换为字符,以便它理解您的CSS代码。 This article describes how to do this. 本文介绍了如何执行此操作。

Note! 注意! It is not enough to simply put @charset "utf-8"; 仅将@charset“ utf-8”放在其中是不够的; at the top of the style sheet – you need to also save your style sheet in the UTF-8 character encoding 在样式表的顶部–您还需要将样式表保存为UTF-8字符编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM