20

I'm inserting content which has the & sign. I would like to know if it's needed to insert it as & so this sign is read correctly.

Like this:

<meta name="description" content="lorem lorem S&amp;B lorem lorem">

Or like this:

<meta name="description" content="lorem lorem S&B lorem lorem">
unor
  • 92,415
  • 26
  • 211
  • 360
Daniel Ramirez-Escudero
  • 3,877
  • 13
  • 43
  • 80

3 Answers3

20

You always represent an ampersand in HTML content as &amp; or &#38;, never a bare &. This includes both text between an element's tags, as well as attribute values. No exceptions.

This is because & itself denotes the start of an HTML entity reference (as seen above) and can often cause problems when appearing in the middle of text that could be interpreted as an entity reference.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • 2
    @Daniel Ramirez-Escudero: One is a named entity, the other is its decimal code point. Two different ways to express the same thing. You can continue using `&` if you prefer. – BoltClock Dec 14 '13 at 16:52
  • So in the html content, can the "&" be escaped to be represented as "&" or will it always show up as "&" or "&" ? – boddhisattva Feb 28 '16 at 06:15
  • I got an answer, it's possible to do so in the context of the Rails web framework using the `html_safe` method. If anybody wants to go this way please read about when is it ideal to use the html_safe method first. – boddhisattva Feb 28 '16 at 13:35
  • IMO, supporting legacy just in case a highly unlikely fringe condition causes a problem < understanding the problem and not ending up with a new legacy problem down the line if entities actually get deprecated and ultimately pulled. Our great-great-great-great-great-great-great-great-great-great-grandchildren will thank us when their sewers aren't exploding because of &3k. – Erik Reppen Jul 12 '16 at 15:54
18

In HTML5, you are not allowed to use "ambiguous ampersands" in attribute values:

Attribute values are a mixture of text and character references, except with the additional restriction that the text cannot contain an ambiguous ampersand.

An ambiguous ampersand is defined as:

[…] a U+0026 AMPERSAND character (&) that is followed by one or more alphanumeric ASCII characters, followed by a ";" (U+003B) character, where these characters do not match any of the names given in the named character references section.

In your example (&B lorem), the ampersand (&) is followed by an alphanumeric ASCII character (B), but this one is not followed by a semicolon (;). So your ampersand is not ambiguous.

Which means that both of your examples are valid.

unor
  • 92,415
  • 26
  • 211
  • 360
-1

It depends on the search engine. Google tends to pick this up as a string so you would want to add &amp; and other related ascii codes.

I recommend due to web standards that you always write an ampersand as &amp;