HTML attribute with/without quotes
There is no practical difference except
- if you validate your page, quotation marks may or may not be needed to avoid error messages, depending on doctype being used
- if you serve the page with an XML content type to browsers (which is rare and seldom useful), then the quotes are required – otherwise the page is not displayed at all, just an error message
- if the page is otherwise processed with XML tools, the quotes are necessary.
Otherwise, the quotation marks are really needed only if the attribute value contains a space, a line break, an Ascii quotation mark ("), an Ascii apostrophe ('), a grave accent (`), an equals sign (=), a less than sign (<), or a greater than sign (>). So style = width:20em
would work (though it might be seen as somewhat obscure), whereas style = width: 20em
would not – due to the space, you would need to write style = "width: 20em"
.
Many people always write quotation marks around all attribute values, for simplicity. Others think that quotation marks make the code a bit messy, so they omit them when possible.
Quite independently of this, src="www.example.com"
means a relative URL reference, not what people expect to mean. You probably meant src="http://www.example.com"
.
According to the W3C there are four types of attribute syntax:
- empty attribute syntax
- unquoted attribute-value syntax
- single-quoted attribute-value syntax
- double-quoted attribute-value syntax
These really apply to HTML5, however when referring to < HTML5 the W3C says that quotes (single or double) are required based on the doctype (e.g. strict, transitional, etc.) used.