It seems that it is best to use the & escape, instead of simply typing the ampersand (&).

However, should we be using X/HTML character entity references for dashes and other common typographical characters when writing blog posts on CMSs like WordPress or hard-coding websites by hand?

For example:

– is an en dash (–)

— is an em dash (—)

What is the risk if we do not?

Why is the hyphen (-) never written as - but simply typed directly from the keyboard in HTML? (Assuming that it the hyphen, and not a minus sign.)

有帮助吗?

解决方案

The W3C released an official response about when to use and when not to use character escapes which you can find here. As they are also the group that is in charge of the HTML specification, I think it's best to follow their advice.

From the section "When to Use Escapes"

Syntax characters. There are three characters that should always appear in content as escapes, so that they do not interact with the syntax of the markup. These are part of the language for all documents based on XML and for HTML.

  • &lt; (<)

  • &gt; (>)

  • &amp; (&)

They also mention using characters that might not be supported in the current encoding.

From the section "When Not to Use Escapes"

It is almost always preferable to use an encoding that allows you to represent characters in their normal form, rather than using character entity references or NCRs.

Using escapes can make it difficult to read and maintain source code, and can also significantly increase file size.

http://www.w3.org/International/questions/qa-escapes

其他提示

Those entities are there to help you, the author, with characters not usually typable on your average keyboard. (The em dash is an example , as well as &copy; and &nbsp;).

You only need to escape those characters that have meaning in (X)HTML < > and &.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top