今回はちょっとHTMLタグの説明をはなれて、文書の本文を書くときに気をつけなきゃいけないことについて。HTML5だけでなく以前のバージョンでも同じ話ではあるのですが。特殊な文字を表示する場合にはどうしなければならないか、という話です。
タグの中で使ってる特殊文字を本文内に使っちゃうと
前回、前々回と読んだ方はHTML文書書くときはタグというもので文書にしるし付けをしていく、というのはわかっていただけたと思いますが、このタグをあらわす「<」とか「>」という記号そのものを本文中に使っちゃうとどうなるでしょう?
結論を言うと「<」と「>」でくくられてるものはたいていのブラウザではその間をタグとして解釈してしまいます。たとえばHTMLソースの本文中に
今日はとっても<いい天気>ですね
と書いちゃうと、<いい天気>の部分は意味不明のタグとして解釈されて無視されたりします(その辺の挙動はブラウザによって違ったりするかもしれないけど)。
同様にHTMLコードを書くにあたって特別な意味づけを持っている「"」や「&」という文字もそのまま本文中に書くことは出来ません。
ではどうすれば良いかというとそのような特殊文字は別な表示法に置き換えて書かなければなりません。実体参照とか数値参照といわれるものです。
具体的には
- 【<】:<(実体参照)/<(ユニコード10進数表示による数値参照)
- 【>】:>(実体参照)/>(ユニコード10進数表示による数値参照)
- 【"】:"(実体参照)/"(ユニコード10進数表示による数値参照)
- 【&】:&(実体参照)/&(ユニコード10進数表示による数値参照)
と書き換えます。ちょっとわかりにくいですかね?先ほどの例の
今日はとっても<いい天気>ですね
というのをブラウザに表示するためには、HTMLファイルのなかでは
<p>今日はとっても<いい天気>ですね</p>
のように表記する、ということです。なんとなくお分かりいただけたでしょうか(pタグについてはまだ説明していませんのでまた後日)。
通常のコーディングではさしあたって上の4つだけ覚えておけば問題ないかと思いますが、キリル文字やギリシャ文字、数学記号などについても入力が困難だったり機種依存文字だったりで製作者の意図の通り表示されない場合があります。
そういった文字化けなどの問題を起こさないように特殊文字はできるだけ実体参照や数値参照を使ったほうが無難ということです。
以下にHTMLでの実体/数値参照の一覧を載せておきますので参考に。
HTML文字実体参照・ユニコード10進数値参照一覧
| 表示 | 文字符号 | 10進数 | 呼称(英) |
|---|---|---|---|
| < | < | < | Left angle bracket |
| > | > | > | Right angle bracket |
| & | & | & | Ampersand |
| " | " | " | Duble quote mark |
| |   | Nonbreaking space | |
| ¡ | ¡ | ¡ | Inverted exclamation mark |
| ¢ | ¢ | ¢ | Cent sign |
| £ | £ | £ | Pound sterling symbol |
| ¤ | ¤ | ¤ | Currency symbol |
| ¥ | ¥ | ¥ | Yen symbol |
| ¦ | ¦ | ¦ | Broken vertical bar |
| § | § | § | Section symbol |
| ¨ | ¨ | ¨ | Umalaut/Dieresis |
| © | © | © | Copyright sign |
| ª | ª | ª | Ordinal indicator,Feminine |
| « | « | « | Left angle quote mark |
| ¬ | ¬ | ¬ | "not" symbol |
| | ­ | ­ | Soft hyphen |
| ® | ® | ® | Registered trademark symbol |
| ¯ | ¯ | ¯ | macron |
| ° | ° | ° | Degree symbol |
| ± | ± | ± | Plus/minus symbol |
| ² | ² | ² | Superscript two |
| ³ | ³ | ³ | Superscript three |
| ´ | ´ | ´ | Acute accent |
| µ | µ | µ | Micro symbol |
| ¶ | ¶ | ¶ | Paragraph symbol |
| · | · | · | Middle dot |
| ¸ | ¸ | ¸ | Cedilla |
| ¹ | ¹ | ¹ | Superscript one |
| º | º | º | Ordinal indicator |
| » | » | » | Angle quote mark, right |
| ¼ | ¼ | ¼ | Fraction one quarter |
| ½ | ½ | ½ | Fraction one half |
| ¾ | ¾ | ¾ | Fraction three quarters |
| ¿ | ¿ | ¿ | Inverted question mark |
| À | À | À | Latin capital A with accent |
| Á | Á | Á | Latin capital A with acute accent |
| Â | Â | Â | Latin capital A with circumflex |
| Ã | Ã | Ã | Latin capital A with tilde accent |
| Ä | Ä | Ä | Latin capital A with diaeresis accent |
| Å | Å | Å | Latin capital A with ring above |
| Æ | Æ | Æ | Latin capital AE diphthong |
| Ç | Ç | Ç | Latin capital C with cedilla |
| È | È | È | Latin capital E with grave accent |
| É | É | É | Latin capital E with acute accent |
| Ê | Ê | Ê | Latin capital E with circumflex |
| Ë | Ë | Ë | Latin capital E with umlaut |
| Ì | Ì | Ì | Latin capital I with grave accent |
| Í | Í | Í | Latin capital I with acute accent |
| Î | Î | Î | Latin capital I with circumflex |
| Ï | Ï | Ï | Latin capital I with diaeresis accent |
| Ð | Ð | Ð | Latin capital "Tth" (Icelandic) |
| Ñ | Ñ | Ñ | Latin capital N with tilde |
| Ò | Ò | Ò | Latin capital O with grave accent |
| Ó | Ó | Ó | Latin capital O with acute accent |
| Ô | Ô | Ô | Latin capital O with circumflex |
| Õ | Õ | Õ | Latin capital O with tilde |
| Ö | Ö | Ö | Latin capital O with umlaut |
| × | × | × | Multiplication symbol |
| Ø | Ø | Ø | Latin capital O with slash |
| Ù | Ù | Ù | Latin capital U with grave accent |
| Ú | Ú | Ú | Latin capital U with acute accent |
| Û | Û | Û | Latin capital U with circumflex |
| Ü | Ü | Ü | Latin capital U with umlaut accent |
| Ý | Ý | Ý | Latin capital Y with acute accent |
| Þ | Þ | Þ | Latin capital "Thorn" (Icelandic) |
| ß | ß | ß | Latin small sharp s (German) |
| à | à | à | Latin small a with grave accent |
| á | á | á | Latin small a with acute accent |
| â | â | â | Latin small a with circumflex |
| ã | ã | ã | Latin small a with tilde |
| ä | ä | ä | Latin small a with umlaut mark |
| å | å | å | Latin small a with ring |
| æ | æ | æ | Latin small ae dipththong (Ligature) |
| ç | ç | ç | Latin small c with cedilla |
| è | è | è | Latin small e with grave accent |
| é | é | é | Latin small e with acute accent |
| ê | ê | ê | Latin small e with circumflex |
| ë | ë | ë | Latin small e with umlaut mark |
| ì | ì | ì | Latin small i with grave accent |
| í | í | í | Latin small i with acute accent |
| î | î | î | Latin small i with circumflex |
| ï | ï | ï | Latin small i with umlaut mark |
| ð | ð | ð | Latin small "eth" (Icelandic) |
| ñ | ñ | ñ | Latin small n with tilde |
| ò | ò | ò | Latin small o with grave accent |
| ó | ó | ó | Latin small o with acute accent |
| ô | ô | ô | Latin small o with circumflex |
| õ | õ | õ | Latin small o with tilde |
| ö | ö | ö | Latin small o with umlaut mark |
| ÷ | ÷ | ÷ | Division symbol |
| ø | ø | ø | Latin small o with slash |
| ù | ù | ù | Latin small u with grave accent |
| ú | ú | ú | Latin small u with acute accent |
| û | û | û | Latin small u with circumflex |
| ü | ü | ü | Latin small u with umlaut mark |
| ý | ý | ý | Latin small y with acute accent |
| þ | þ | þ | Latin small "Thorn" (Icelandic) |
| ÿ | ÿ | ÿ | Latin small y with umlaut mark |
| Α | Α | Α | Greek capital letter alpha |
| Β | Β | Β | Greek capital letter beta |
| Γ | Γ | Γ | Greek capital letter gamma |
| Δ | Δ | Δ | Greek capital letter delta |
| Ε | Ε | Ε | Greek capital letter epsilon |
| Ζ | Ζ | Ζ | Greek capital letter zeta |
| Η | Η | Η | Greek capital letter eta |
| Θ | Θ | Θ | Greek capital letter theta |
| Ι | Ι | Ι | Greek capital letter iota |
| Κ | Κ | Κ | Greek capital letter kappa |
| Λ | Λ | Λ | Greek capital letter lambda |
| Μ | Μ | Μ | Greek capital letter mu |
| Ν | Ν | Ν | Greek capital letter nu |
| Ξ | Ξ | Ξ | Greek capital letter xi |
| Ο | Ο | Ο | Greek capital letter omicron |
| Π | Π | Π | Greek capital letter pi |
| Ρ | &Pho; | Ρ | Greek capital letter rho |
| Σ | Σ | Σ | Greek capital letter sigma |
| Τ | Τ | Τ | Greek capital letter Tau |
| Υ | Υ | Υ | Greek capital letter upsilon |
| Φ | Φ | Φ | Greek capital letter phi |
| Χ | Χ | Χ | Greek capital letter chi |
| Ψ | Ψ | Ψ | Greek capital letter psi |
| Ω | Ω | Ω | Greek capital letter omega |
| α | α | α | Greek small letter alpha |
| β | β | β | Greek small letter beta |
| γ | γ | γ | Greek small letter gamma |
| δ | δ | δ | Greek small letter delta |
| ε | ε | ε | Greek small letter epsilon |
| ζ | ζ | ζ | Greek small letter zeta |
| η | η | η | Greek small letter eta |
| θ | θ | θ | Greek small letter theta |
| ι | ι | ι | Greek small letter iota |
| κ | κ | κ | Greek small letter kappa |
| λ | λ | λ | Greek small letter lambda |
| μ | μ | μ | Greek small letter mu |
| ν | ν | ν | Greek small letter nu |
| ξ | ξ | ξ | Greek small letter xi |
| ο | ο | ο | Greek small letter omicron |
| π | π | π | Greek small letter pi |
| ρ | ρ | ρ | Greek small letter rho |
| ς | ς | ς | Greek small letter final sigma |
| σ | σ | σ | Greek small letter sigma |
| τ | τ | τ | Greek small letter tau |
| υ | υ | υ | Greek small letter upsilon |
| φ | φ | φ | Greek small letter phi |
| χ | χ | χ | Greek small letter chi |
| ψ | ψ | ψ | Greek small letter psi |
| ω | ω | ω | Greek small letter omega |
| – | – | – | en size dash |
| — | — | — | em size dash |
| ‘ | ‘ | ‘ | Left single quotation mark |
| ’ | ’ | ’ | Right single quotation mark |
| ‚ | ‚ | ‚ | Low single quotation mark |
| “ | “ | “ | Left double quotation mark |
| ” | ” | ” | Right double quotation mark |
| „ | „ | „ | Low double quotation mark |
| † | † | † | Dagger mark |
| ‡ | ‡ | ‡ | Double dagger mark |
| • | • | • | Bullet, Black small circle |
| … | … | … | Three dot leader |
| ‰ | ‰ | ‰ | Per mille sign |
| ′ | ′ | ′ | Prime, Minutes |
| ″ | ″ | ″ | Double prime, Seconds |
| ‹ | ‹ | ‹ | Single left angle quotation mark |
| › | › | › | Single right angle quotation mark |
| ‾ | ‾ | ‾ | Overline, Spacing overscore |
| ⁄ | ⁄ | ⁄ | Fraction slash |
| € | € | € | Euro sign |
| ℘ | ℘ | ℘ | Script capital P |
| ℑ | ℑ | ℑ | Blackletter capital I |
| ℜ | ℜ | ℜ | Blackletter capital R |
| ™ | ™ | ™ | Trade mark sign |
| ℵ | ℵ | ℵ | Aref Symbol |
| ← | ← | ← | Leftwards arrow |
| → | → | → | Rightwards arrow |
| ↓ | ↓ | ↓ | Downwards arrow |
| ↔ | ↔ | ↔ | Left right arrow |
| ↵ | ↵ | ↵ | Carrige return symbol |
| ⇐ | ⇐ | ⇐ | Leftwards double arrow |
| ⇑ | ⇑ | ⇑ | Upwards double arrow |
| ⇒ | ⇒ | ⇒ | Rightwards double arrow |
| ⇓ | ⇓ | ⇓ | Downwards double arrow |
| ⇔ | ⇔ | ⇔ | Left right double arrow |
| ◊ | ◊ | ◊ | Lozenge |
| ♠ | ♠ | ♠ | Black spade suit |
| ♣ | ♣ | ♣ | Black club suit, Shamrock |
| ♥ | ♥ | ♥ | Black heart suit, Valentine |
| ♦ | ♦ | ♦ | Black diamond suit |
| ∀ | ∀ | ∀ | For all |
| ∂ | ∂ | ∂ | Partial differential |
| ∃ | ∃ | ∃ | There exists |
| ∅ | ∅ | ∅ | Empty set, Null set |
| ∇ | ∇ | ∇ | Nabla, Backward difference |
| ∈ | ∈ | ∈ | Elemant of |
| ∉ | ∉ | ∉ | Not an elemant of |
| ∋ | ∋ | ∋ | Contains as member |
| ∏ | ∏ | ∏ | n-ary product, Product sign |
| ∑ | ∑ | ∑ | n-ary sumation |
| − | − | − | Minus sign |
| ∗ | ∗ | ∗ | Asterisk operator |
| √ | √ | √ | Square root, Radical sign |
| ∝ | ∝ | ∝ | Proportional to |
| ∞ | ∞ | ∞ | Infinity |
| ∠ | ∠ | ∠ | Angle |
| ∧ | ∧ | ∧ | Logical and, Wedge |
| ∨ | ∨ | ∨ | Logical or, Vee |
| ∩ | ∩ | ∩ | Intersection, Cap |
| ∪ | ∪ | ∪ | Union, Cup |
| ∫ | ∫ | ∫ | Intergral |
| ∴ | ∴ | ∴ | Therefore |
| ∼ | ∼ | ∼ | Tilde opetator, Varies with |
| ≅ | ≅ | ≅ | Approximately equal to |
| ≈ | ≈ | ≈ | Almost equal to |
| ≠ | ≠ | ≠ | Not equal to |
| ≡ | ≡ | ≡ | Identical to |
| ≤ | ≤ | ≤ | Less then or equal to |
| ≥ | ≥ | ≥ | Greate then or equal to |
| ⊂ | ⊂ | ⊂ | Subset of |
| ⊃ | ⊃ | ⊃ | Superset of |
| ⊄ | ⊄ | ⊄ | Not a superset of |
| ⊆ | ⊆ | ⊆ | Subset of or equal to |
| ⊇ | ⊇ | ⊇ | Superset of or equal to |
| ⊕ | ⊕ | ⊕ | Circled plus, Direct sum |
| ⊗ | ⊗ | ⊗ | Circled times, Vector product |
| ⊥ | ⊥ | ⊥ | Up tack |
まとめ
上の一覧丸暗記してる人は多分いないと思います。あるいは実体・数値参照使わずに書いてるけどたまたま問題発生していないので気にしてない、なんてこともあるかもしれません。だからといって放置しておいていいわけでもないんですけど。
なのでまずは
- 本文中の「<」「>」「"」「&」については必ず「<」「>」「"」「&」に置き換える
- 普段使わない特殊な文字・記号を使うときは一度一覧表を参照してみる
- 参考資料としてこのエントリをブクマしておく(まぁいやらしい…)
そんなことを頭の中に入れておいていただければと思います。

コメント