Do you quote HTML5 attributes? [closed]

2019-01-01 10:59发布

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)
  • ?

5条回答
长期被迫恋爱
2楼-- · 2019-01-01 11:29

HTML5 was designed with backward compatibility in mind, so quoting attributes is okay. Many folks argue that since XHTML requires quotes it can't hurt to always include them. Personally I choose to quote. Required? No.

Here's some guidance I find reasonable. http://www.impressivewebs.com/html5-syntax-style/

查看更多
刘海飞了
3楼-- · 2019-01-01 11:36

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.

查看更多
爱死公子算了
4楼-- · 2019-01-01 11:45

I would say that because quotes are required in some instances you should use them consistently. This makes your code cleaner and easier to read. Switching between just creates unnecessary confusion.

查看更多
情到深处是孤独
5楼-- · 2019-01-01 11:48

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).

查看更多
君临天下
6楼-- · 2019-01-01 11:50

I think one of the advantages to quoting all attributes is consistency.

If you're quoting some (with spaces or certain symbols), it feels nicer to me to quote all of them.

查看更多
登录 后发表回答