0

I know the spec allows both ' and " as delimiters for attribute values, and I also know it's a good practice to always quote.

However I consider " being the cleaner way, maybe it's just me having grown up with C and C++' syntax.

What is the cleanest way of quoting attribute values and why? Please no subjective answers.

Flavius
  • 13,566
  • 13
  • 80
  • 126

3 Answers3

3

Both are fine, but Double quotes are better (IMHO) as you reduce the risk of dynamic values causing errors. e.g.

<input value='${lastName}'/>

<input value='O'Graddy'/>
                ^^^^^^^

vs.

<input value="${lastName}"/>

<input value="O'Graddy"/>
scunliffe
  • 62,582
  • 25
  • 126
  • 161
1

There’s a lot of rules to remember if you want to omit quotes around attribute values. It’s probably easiest to just use quotes consistently; it avoids all kinds of problems.

If you’re interested, I did some research on unquoted attribute values in HTML, CSS and JavaScript a while ago, and wrote about it here: http://mathiasbynens.be/notes/unquoted-attribute-values

I’ve also created a tool that will tell you if a value you enter is a valid unquoted attribute value or not: http://mothereffingunquotedattributes.com/#foo%7Cbar

Mathias Bynens
  • 144,855
  • 52
  • 216
  • 248
0

Either is good, as long as you use it. " is more popular.

Delan Azabani
  • 79,602
  • 28
  • 170
  • 210