HTML字符集是指在网页中所使用的字符编码方式。在HTML文件中,可以使用实体名称或实体编号来表示特殊字符。这些特殊字符包括但不限于版权符号©、商标注册符号®、关闭符号×等。
一、HTML字符集发展
早期的网络采用ASCII码作为字符编码标准,它定义了128个可以在互联网上使用的字符:数字(0-9)、英文字母(A-Z)以及一些特殊字符,比如! $ + – ( ) @ < >等。
然而,随着互联网的发展,ASCII码无法满足日益增长的全球字符需求。于是,从HTML 2.0到HTML 4.01,ISO-8859-1被认定为标准,该字符集支持256个不同的字符代码。HTML 4 同时支持 UTF-8。
HTML5规范鼓励Web开发人员使用UTF-8字符集,因为UTF-8字符集涵盖了世界上几乎所有的字符和符号。与ISO-8859-1相比,UTF-8能够提供更多的全球字符支持,因此在处理国际化文本时更为优越。
二、HTML charset属性
在HTML中,charset属性被用来声明文档所使用的字符编码格式。这个属性的实质意义在于,浏览器会根据这个声明的编码格式去解析并显示文档中的字符,从而避免出现乱码。
在HTML5中,charset属性是新增的属性,用于取代过去的以下代码:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
此外,charset属性还可以规定在外部脚本文件中使用的字符编码。如果外部文件中的字符编码与主文件中的编码方式不同,就需要使用charset属性。
三、字符集之间差异
下表展示了上述字符集之间的区别:
Numb | ASCII | ANSI | 8859 | UTF-8 | Description |
32 | space | ||||
33 | ! | ! | ! | ! | exclamation mark |
34 | “ | “ | “ | “ | quotation mark |
35 | # | # | # | # | number sign |
36 | $ | $ | $ | $ | dollar sign |
37 | % | % | % | % | percent sign |
38 | & | & | & | & | ampersand |
39 | ‘ | ‘ | ‘ | ‘ | apostrophe |
40 | ( | ( | ( | ( | left parenthesis |
41 | ) | ) | ) | ) | right parenthesis |
42 | * | * | * | * | asterisk |
43 | + | + | + | + | plus sign |
44 | , | , | , | , | comma |
45 | – | – | – | – | hyphen-minus |
46 | . | . | . | . | full stop |
47 | / | / | / | / | solidus |
48 | 0 | 0 | 0 | 0 | digit zero |
49 | 1 | 1 | 1 | 1 | digit one |
50 | 2 | 2 | 2 | 2 | digit two |
51 | 3 | 3 | 3 | 3 | digit three |
52 | 4 | 4 | 4 | 4 | digit four |
53 | 5 | 5 | 5 | 5 | digit five |
54 | 6 | 6 | 6 | 6 | digit six |
55 | 7 | 7 | 7 | 7 | digit seven |
56 | 8 | 8 | 8 | 8 | digit eight |
57 | 9 | 9 | 9 | 9 | digit nine |
58 | : | : | : | : | colon |
59 | ; | ; | ; | ; | semicolon |
60 | < | < | < | < | less-than sign |
61 | = | = | = | = | equals sign |
62 | > | > | > | > | greater-than sign |
63 | ? | ? | ? | ? | question mark |
64 | @ | @ | @ | @ | commercial at |
65 | A | A | A | A | Latin capital letter A |
66 | B | B | B | B | Latin capital letter B |
67 | C | C | C | C | Latin capital letter C |
68 | D | D | D | D | Latin capital letter D |
69 | E | E | E | E | Latin capital letter E |
70 | F | F | F | F | Latin capital letter F |
71 | G | G | G | G | Latin capital letter G |
72 | H | H | H | H | Latin capital letter H |
73 | I | I | I | I | Latin capital letter I |
74 | J | J | J | J | Latin capital letter J |
75 | K | K | K | K | Latin capital letter K |
76 | L | L | L | L | Latin capital letter L |
77 | M | M | M | M | Latin capital letter M |
78 | N | N | N | N | Latin capital letter N |
79 | O | O | O | O | Latin capital letter O |
80 | P | P | P | P | Latin capital letter P |
81 | Q | Q | Q | Q | Latin capital letter Q |
82 | R | R | R | R | Latin capital letter R |
83 | S | S | S | S | Latin capital letter S |
84 | T | T | T | T | Latin capital letter T |
85 | U | U | U | U | Latin capital letter U |
86 | V | V | V | V | Latin capital letter V |
87 | W | W | W | W | Latin capital letter W |
88 | X | X | X | X | Latin capital letter X |
89 | Y | Y | Y | Y | Latin capital letter Y |
90 | Z | Z | Z | Z | Latin capital letter Z |
91 | [ | [ | [ | [ | left square bracket |
92 | \ | \ | \ | \ | reverse solidus |
93 | ] | ] | ] | ] | right square bracket |
94 | ^ | ^ | ^ | ^ | circumflex accent |
95 | _ | _ | _ | _ | low line |
96 | ` | ` | ` | ` | grave accent |
97 | a | a | a | a | Latin small letter a |
98 | b | b | b | b | Latin small letter b |
99 | c | c | c | c | Latin small letter c |
100 | d | d | d | d | Latin small letter d |
101 | e | e | e | e | Latin small letter e |
102 | f | f | f | f | Latin small letter f |
103 | g | g | g | g | Latin small letter g |
104 | h | h | h | h | Latin small letter h |
105 | i | i | i | i | Latin small letter i |
106 | j | j | j | j | Latin small letter j |
107 | k | k | k | k | Latin small letter k |
108 | l | l | l | l | Latin small letter l |
109 | m | m | m | m | Latin small letter m |
110 | n | n | n | n | Latin small letter n |
111 | o | o | o | o | Latin small letter o |
112 | p | p | p | p | Latin small letter p |
113 | q | q | q | q | Latin small letter q |
114 | r | r | r | r | Latin small letter r |
115 | s | s | s | s | Latin small letter s |
116 | t | t | t | t | Latin small letter t |
117 | u | u | u | u | Latin small letter u |
118 | v | v | v | v | Latin small letter v |
119 | w | w | w | w | Latin small letter w |
120 | x | x | x | x | Latin small letter x |
121 | y | y | y | y | Latin small letter y |
122 | z | z | z | z | Latin small letter z |
123 | { | { | { | { | left curly bracket |
124 | | | | | | | | | vertical line |
125 | } | } | } | } | right curly bracket |
126 | ~ | ~ | ~ | ~ | tilde |
127 | DEL | ||||
128 | € | euro sign | |||
129 | | | | NOT USED | |
130 | ‚ | single low-9 quotation mark | |||
131 | ƒ | Latin small letter f with hook | |||
132 | „ | double low-9 quotation mark | |||
133 | … | horizontal ellipsis | |||
134 | † | dagger | |||
135 | ‡ | double dagger | |||
136 | ˆ | modifier letter circumflex accent | |||
137 | ‰ | per mille sign | |||
138 | Š | Latin capital letter S with caron | |||
139 | ‹ | single left-pointing angle quotation mark | |||
140 | Œ | Latin capital ligature OE | |||
141 | | | | NOT USED | |
142 | Ž | Latin capital letter Z with caron | |||
143 | | | | NOT USED | |
144 | | | | NOT USED | |
145 | ‘ | left single quotation mark | |||
146 | ’ | right single quotation mark | |||
147 | “ | left double quotation mark | |||
148 | ” | right double quotation mark | |||
149 | • | bullet | |||
150 | – | en dash | |||
151 | — | em dash | |||
152 | ˜ | small tilde | |||
153 | ™ | trade mark sign | |||
154 | š | Latin small letter s with caron | |||
155 | › | single right-pointing angle quotation mark | |||
156 | œ | Latin small ligature oe | |||
157 | | | | NOT USED | |
158 | ž | Latin small letter z with caron | |||
159 | Ÿ | Latin capital letter Y with diaeresis | |||
160 | no-break space | ||||
161 | ¡ | ¡ | ¡ | inverted exclamation mark | |
162 | ¢ | ¢ | ¢ | cent sign | |
163 | £ | £ | £ | pound sign | |
164 | ¤ | ¤ | ¤ | currency sign | |
165 | ¥ | ¥ | ¥ | yen sign | |
166 | ¦ | ¦ | ¦ | broken bar | |
167 | § | § | § | section sign | |
168 | ¨ | ¨ | ¨ | diaeresis | |
169 | © | © | © | copyright sign | |
170 | ª | ª | ª | feminine ordinal indicator | |
171 | « | « | « | left-pointing double angle quotation mark | |
172 | ¬ | ¬ | ¬ | not sign | |
173 | soft hyphen | ||||
174 | ® | ® | ® | registered sign | |
175 | ¯ | ¯ | ¯ | macron | |
176 | ° | ° | ° | degree sign | |
177 | ± | ± | ± | plus-minus sign | |
178 | ² | ² | ² | superscript two | |
179 | ³ | ³ | ³ | superscript three | |
180 | ´ | ´ | ´ | acute accent | |
181 | µ | µ | µ | micro sign | |
182 | ¶ | ¶ | ¶ | pilcrow sign | |
183 | · | · | · | middle dot | |
184 | ¸ | ¸ | ¸ | cedilla | |
185 | ¹ | ¹ | ¹ | superscript one | |
186 | º | º | º | masculine ordinal indicator | |
187 | » | » | » | right-pointing double angle quotation mark | |
188 | ¼ | ¼ | ¼ | vulgar fraction one quarter | |
189 | ½ | ½ | ½ | vulgar fraction one half | |
190 | ¾ | ¾ | ¾ | vulgar fraction three quarters | |
191 | ¿ | ¿ | ¿ | inverted question mark | |
192 | À | À | À | Latin capital letter A with grave | |
193 | Á | Á | Á | Latin capital letter A with acute | |
194 | Â | Â | Â | Latin capital letter A with circumflex | |
195 | Ã | Ã | Ã | Latin capital letter A with tilde | |
196 | Ä | Ä | Ä | Latin capital letter A with diaeresis | |
197 | Å | Å | Å | Latin capital letter A with ring above | |
198 | Æ | Æ | Æ | Latin capital letter AE | |
199 | Ç | Ç | Ç | Latin capital letter C with cedilla | |
200 | È | È | È | Latin capital letter E with grave | |
201 | É | É | É | Latin capital letter E with acute | |
202 | Ê | Ê | Ê | Latin capital letter E with circumflex | |
203 | Ë | Ë | Ë | Latin capital letter E with diaeresis | |
204 | Ì | Ì | Ì | Latin capital letter I with grave | |
205 | Í | Í | Í | Latin capital letter I with acute | |
206 | Î | Î | Î | Latin capital letter I with circumflex | |
207 | Ï | Ï | Ï | Latin capital letter I with diaeresis | |
208 | Ð | Ð | Ð | Latin capital letter Eth | |
209 | Ñ | Ñ | Ñ | Latin capital letter N with tilde | |
210 | Ò | Ò | Ò | Latin capital letter O with grave | |
211 | Ó | Ó | Ó | Latin capital letter O with acute | |
212 | Ô | Ô | Ô | Latin capital letter O with circumflex | |
213 | Õ | Õ | Õ | Latin capital letter O with tilde | |
214 | Ö | Ö | Ö | Latin capital letter O with diaeresis | |
215 | × | × | × | multiplication sign | |
216 | Ø | Ø | Ø | Latin capital letter O with stroke | |
217 | Ù | Ù | Ù | Latin capital letter U with grave | |
218 | Ú | Ú | Ú | Latin capital letter U with acute | |
219 | Û | Û | Û | Latin capital letter U with circumflex | |
220 | Ü | Ü | Ü | Latin capital letter U with diaeresis | |
221 | Ý | Ý | Ý | Latin capital letter Y with acute | |
222 | Þ | Þ | Þ | Latin capital letter Thorn | |
223 | ß | ß | ß | Latin small letter sharp s | |
224 | à | à | à | Latin small letter a with grave | |
225 | á | á | á | Latin small letter a with acute | |
226 | â | â | â | Latin small letter a with circumflex | |
227 | ã | ã | ã | Latin small letter a with tilde | |
228 | ä | ä | ä | Latin small letter a with diaeresis | |
229 | å | å | å | Latin small letter a with ring above | |
230 | æ | æ | æ | Latin small letter ae | |
231 | ç | ç | ç | Latin small letter c with cedilla | |
232 | è | è | è | Latin small letter e with grave | |
233 | é | é | é | Latin small letter e with acute | |
234 | ê | ê | ê | Latin small letter e with circumflex | |
235 | ë | ë | ë | Latin small letter e with diaeresis | |
236 | ì | ì | ì | Latin small letter i with grave | |
237 | í | í | í | Latin small letter i with acute | |
238 | î | î | î | Latin small letter i with circumflex | |
239 | ï | ï | ï | Latin small letter i with diaeresis | |
240 | ð | ð | ð | Latin small letter eth | |
241 | ñ | ñ | ñ | Latin small letter n with tilde | |
242 | ò | ò | ò | Latin small letter o with grave | |
243 | ó | ó | ó | Latin small letter o with acute | |
244 | ô | ô | ô | Latin small letter o with circumflex | |
245 | õ | õ | õ | Latin small letter o with tilde | |
246 | ö | ö | ö | Latin small letter o with diaeresis | |
247 | ÷ | ÷ | ÷ | division sign | |
248 | ø | ø | ø | Latin small letter o with stroke | |
249 | ù | ù | ù | Latin small letter u with grave | |
250 | ú | ú | ú | Latin small letter u with acute | |
251 | û | û | û | Latin small letter with circumflex | |
252 | ü | ü | ü | Latin small letter u with diaeresis | |
253 | ý | ý | ý | Latin small letter y with acute | |
254 | þ | þ | þ | Latin small letter thorn | |
255 | ÿ | ÿ | ÿ | Latin small letter y with diaeresis |
1、ASCII字符集
ASCII字符集是计算机中最基本的字符集,它总共包含了128个字符。其中,前32个字符,即0到31的编码(包括127),被称为控制字符。这些字符在显示上是不可见的,然而,每个字符都对应着一个特殊的控制功能。例如,NUL (0) NULL,空字符,这个字符可以表示空操作,即忽略一个字符。
ASCII中的第32个到第126个字符,即32到126的编码,主要表示的是字母、数字和符号。例如,空格对应的编码是32,大写英文字母A到Z对应的编码是65到90,小写英文字母a到z对应的编码是97到122。
注意:ASCII字符集中并没有使用128到255之间的值。
2、ANSI字符集
ANSI字符集是美国国家标准局(ANSI)制定的,它包含了ASCII码,也就是美国标准信息交换码。在0到127的值范围内,ANSI与ASCII是相同的。这意味着它们都使用同样的编码来代表控制字符、字母、数字和符号。
ANSI字符集还定义了一组专有的字符,其值从128到159。这些字符并未被ASCII所定义,但是在ANSI中得到了专门的编码。例如,欧元符号就是由ANSI字符集中的特定编码来表示的。
此外,对于160到255的值,ANSI采用了与UTF-8相同的编码方式,对于那些超出ASCII范围的特殊符号或非英语语言的文字,可以使用ANSI字符集进行正确的编码和处理。
3、ISO-8859-1字符集
8859-1,也被称为ISO-8859-1,是一种被广泛使用的字符集,它与ASCII字符集有很多相似之处,例如,两者都使用0到127的编码值来表示控制字符、英文字母、数字和常见的符号。然而,8859-1并没有使用ASCII字符集中没有用到的128到159之间的值。
对于ASCII字符集中没有的字符,8859-1采用了不同的编码方式。具体来说,当编码值从160开始,一直到255结束,8859-1的编码方式就与UTF-8相同了。这意味着那些超出ASCII范围的特殊符号或非英语语言的文字,可以使用8859-1进行正确的编码和处理。
4、UTF-8字符集
UTF-8是一种广泛使用的字符编码方式,它可以用一至四个字节来表示一个字符。对于ASCII字符集中的字符,即0到127的值,UTF-8和ASCII是兼容的,因为ASCII字符只需要一个字节去存储。然而,UTF-8并没有使用ASCII字符集没有用到的128到159之间的值。
当涉及到ANSI字符集中使用的160到255的值时,UTF-8的处理方式与ANSI和8859-1相同。换句话说,那些超出ASCII范围的特殊符号或非英语语言的文字,可以使用UTF-8进行正确的编码和处理。虽然UTF-8在处理ASCII字符时与ASCII兼容,但UTF-8所能表示的字符范围远超ASCII。实际上,UTF-8从值256开始,可以表示超过10000个不同的字符。
5、@charset CSS规则
@charset CSS规则用于指定文档的字符编码。它通常放在HTML文件的<head>部分,如下所示:
<html> <head> <meta charset="UTF-8"> <title>示例页面</title> </head> <body> <!-- 页面内容 --> </body> </html>
在这个例子中,<meta charset=”UTF-8″>表示文档使用UTF-8字符编码。