Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you quote HTML5 attributes? [closed]

Attribute quotes are optional in HTML5.

What are the pros/cons to quoting them?

id=example                     <!--quotes optional--> href=http://example.com        <!--quotes optional--> class="example example-1"      <!--quotes required due to space--> href="http://example.com/p=47" <!--quotes required due to '=' sign--> 

Update: Added advantages based on the answers:

Advantages to quoting all attributes:

  • all editors can deal with it properly
  • more consistent
  • better portability (easier to change doctype)
  • easier to maintain (esp. if attributes might become empty)
  • easier to 'find and replace' changes
  • cleaner doc (if you think quotes improve readability)
  • ?

Advantages to omitting optional quotes:

  • slightly reduced filesize
  • cleaner doc (if you prefer minimal text)
  • ?
like image 851
ryanve Avatar asked Jun 27 '11 15:06

ryanve


People also ask

Should HTML5 attribute values be enclosed in quotes?

With HTML5, you don't have to quote attribute values. Until you do. One of the benefits often touted for HTML5 over XHTML is what I once heard Paul Irish describe as its “loosey goosey” approach to syntax.

Do you need quotes for HTML attributes?

The HTML specification says: Attributes are placed inside the start tag, and consist of a name and a value, separated by an = character. The attribute value can remain unquoted if it doesn't contain spaces or any of " ' ` = < or > . Otherwise, it has to be quoted using either single or double quotes.

Is quotes are optional for attributes in HTML5?

Quotes are optional for attributes. Attribute values are optional. Closing empty elements are optional.

Should all values be placed in quotation marks for HTML?

The official rules In the HTML 4.0 specification, section Attributes , the rule is formulated as follows: By default, SGML requires that all attribute values be delimited using either double quotation marks (ASCII decimal 34) or single quotation marks (ASCII decimal 39).


2 Answers

I'm in favour of always using quotes.

  • It looks way cleaner and more consistent

  • All editors can deal with it properly

  • It's easier to maintain - you can edit values without breaking them because quotes are missing.

The few bytes you save in document size by dropping quotes where they are not needed are not worth mentioning (unless maybe you're Google's home page).

like image 108
Pekka Avatar answered Oct 14 '22 20:10

Pekka


I do quote all attributes and will continue to do so. Primarily because it visually distinguishing where the attribute value starts and stops.

Additionally, it just makes sense for portability and compatibility reason. Though the quotes are optional in HTML[5], they are not optional in XHTML. You have a lot of tedious work to do if you need to convert your documents to XHTML (say, to display SVG on Webkit browsers). We really don't need to dredge up the XHTML v. HTML debate, but it seems like too little hassle to not quote your attributes.

Saving a few bytes in the document body is nigh insignificant when you're downloading kilobytes and kilobytes of images and JavaScript libraries.

like image 45
Courtney Christensen Avatar answered Oct 14 '22 21:10

Courtney Christensen