20

Are they allowed? and do they work with all browsers?

Example:

<div role = "region"
     id = "some-id"
     class = "a-class another-class">
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Ian Storm Taylor
  • 8,520
  • 12
  • 55
  • 72

3 Answers3

16

Yes, any amount of whitespace is allowed and will work in all browsers.

From the Attributes section of the HTML5 living standard on unquoted, single-, and double-quoted attribute value syntax:

The attribute name, followed by zero or more ASCII whitespace, followed by a single U+003D EQUALS SIGN character, followed by zero or more ASCII whitespace, [...]

One consideration - this will add to the page size, so if bandwidth and performance are concerns, try to limit the amount of whitespace you use.

Henry Woody
  • 14,024
  • 7
  • 39
  • 56
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Does any amount of whitespace allows *zero-width* for quoted attributes after the attribute value? - Ref: http://stackoverflow.com/q/23887069/367456 – hakre May 27 '14 at 10:31
  • Not sure about that. I'd expect they should be. – Oded May 27 '14 at 10:33
  • If you're asking about having *no* whitespace, then.. no. That's not accepted. "Any amount" really means "one or more characters" in this context. That's also mentioned in the accepted answer on the post you linked. – apraetor Apr 24 '17 at 21:42
6

Yes they are, and they will work in all major browsers, although I would say it should be considered bad practice to include unnecessary white-space as it pointlessly increases the size of the document.

HTML, XHTML, XML and others are all variants of SGML, so if you want to know what is/isn't allowed in general, have a look at that specification. You should always pass all your documents through the W3C markup validators to ensure they are valid.

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • Good point. Realized I could save 20% by compressing this HTML. Thanks! – Ian Storm Taylor Aug 15 '11 at 11:15
  • I think the benefits in readability out way the costs, especially if you use a compressor. This is especially true when using a templating preprocessor, where large attribute values can make the markup unreadable. – rectangletangle May 05 '14 at 09:17
4

Yes, it is perfectly valid markup. Whitespace is handled nicely by all browsers.

Any time you have confusion, you can validate your code at official W3 validation service:

Sarfraz
  • 377,238
  • 77
  • 533
  • 578