为样式表指定字符集 [英] Specifying a charset for stylesheets

查看:121
本文介绍了为样式表指定字符集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的样式表包含类似于以下内容的CSS代码:

My stylesheets contain CSS code similar to the following:

#nav > li::after {
    content: " ➻";
}

您可能会注意到➻不是ASCII字符,

You may notice that ➻ is not an ASCII character, and therefore it's "dangerous" to include it in a file without specifying a charset.

现在事情已经顺利,我从来没有遇到CSS样式表的编码问题(主要是因为用户代理正在改变猜测UTF-8),但我想知道是否有一个正确的方法来明确指定它。

For now things have been smooth and I've never run into encoding issues with CSS stylesheets (mostly because user agents are getting better at guessing "UTF-8"), but I was wondering if there was a right way to explicitly specify it.

我试过这个:

<link rel="stylesheet" type="text/css; charset=UTF-8" href="foo.css"/>

但它似乎没有做任何事情,因为我试图指定一个伪造的编码,

But it doesn't seem to do anything, as I tried to specify a bogus encoding and it still displayed correctly.

推荐答案

发现我需要的引用:

http://www.w3.org/TR/CSS2/syndata.html#charset


当样式表位于单独的文件中时,用户代理必须
在确定样式表的
字符编码(从最高优先级到最低):

When a style sheet resides in a separate file, user agents must observe the following priorities when determining a style sheet's character encoding (from highest priority to lowest):


  1. Content-Type 字段(或其他协议中的类似参数)

  2. BOM和/或@charset(见下文)

  3. 如果有的话)

  4. 引用样式表或文档的字符集(如果有)

  5. 假设UTF-8

  1. An HTTP "charset" parameter in a "Content-Type" field (or similar parameters in other protocols)
  2. BOM and/or @charset (see below)
  3. or other metadata from the linking mechanism (if any)
  4. charset of referring style sheet or document (if any)
  5. Assume UTF-8

因此,尽管 @charsetUTF-8; 覆盖它。您可以使用< meta charset =utf-8> 在HTML文档的元标记中指定字符集,这将满足#4,用户代理

So, although @charset "UTF-8"; is relevant, HTTP headers override it. You can specify the charset in a meta tag of your HTML document with <meta charset="utf-8">, which will satisfy #4, and user agents are supposed to fall back to UTF-8 anyways.

对于这些字形,重要的是最终用户必须至少安装一个支持它的字体,因此,在编写CSS时记住这一点。并非所有字体都支持所有Unicode字符。

With some of these "glyphs", it's important that the end user has at least one font installed that supports it, so keep that in mind while writing your CSS. Not all fonts support all unicode characters.

这篇关于为样式表指定字符集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆