1

I am about to finish up my dynamic classifieds website (php) and have some problem with figuring out how to write the meta and title tags.

I have read mixed articles about the & sign, and how to use it in the title or meta tags properly.

What role does the document encoding have and does the DOCTYPE have anything to do with this also?

In my case, this is my document top:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

I wonder, should I write the and tags with an '&' or an '&amp;' instead?

Just as an example:

 <title>Dolce & Gabbana</title>
             or
 <title>Dolce &amp; Gabbana</title>

I can't use the word 'and' simply because it doesn't "look" as good.

Thanks

4 Answers4

3

In this case both &amp; and even just & are valid as the latter is followed by a whitespace and thus cannot be confused with a reference open delimiter.

But the HTML 4 specification recommends to use &amp; to avoid ambiguity:

Authors should use "&amp;" (ASCII decimal 38) instead of "&" to avoid confusion with the beginning of a character reference (entity reference open delimiter). Authors should also use "&amp;" in attribute values since character references are allowed within CDATA attribute values.

See my answer on Do I really need to encode '&' as '&amp;'? for further information.

Community
  • 1
  • 1
Gumbo
  • 594,236
  • 102
  • 740
  • 814
2

run you page through http://validator.w3.org/ and follow the suggestions

Grumpy
  • 1,712
  • 1
  • 21
  • 34
1

In my opinion, a hypen '-' works better. Nobody's gonna search with an ampersand symbol anyway.

Ruel
  • 14,301
  • 6
  • 34
  • 49
0

The & symbol is invalid in HTML except as part of an entity (such as &amp;).

That kinda answers your question -- your first example is invalid HTML, so you should be using the second.

Most browsers will cope with the first example, as there's no ambiguity, but you should still be using the entity code if you're aiming to produce good code.

Spudley
  • 157,081
  • 38
  • 222
  • 293
  • @Gumbo - the w3c validator disagrees with you. (assuming strict mode, as per the question) – Spudley Sep 29 '10 at 08:50
  • Invalid in HTML4, valid in HTML5. – Matthew Wilson Sep 29 '10 at 09:00
  • ` & ` is valid in HTML4. You will get a validator warning (as it's highly inadvisable!), but it's not an error. It is of couse invalid (not even well-formed) in XHTML. – bobince Sep 29 '10 at 09:09
  • @Spudley: It is valid. Just try it on your own using the [W3C Validator in fragment mode](http://validator.w3.org/#validate_by_input+with_options): It selects Strict automatically and the only error is that `title` is not allowed in the context (since it’s only a fragment). – Gumbo Sep 29 '10 at 09:36
  • @Gumbo (and others) - okay, I saw 'strict' in the doctype in the question and assumed 'xhtml'. My bad. In strict xhtml, the validator does indeed throw it as an error. For other doctypes, it's valid.... but still better to use the entity. – Spudley Sep 29 '10 at 09:43