Recently I've been seeing a lot of this:
<a href='http://widget-site-example.com/example.html'>
<img src='http://widget-site-example.com/ross.jpg' alt='Ross's Widget' />
</a>
Is it valid to use single quotes in HTML? As I've highlighted above it's also problematic because you have to escape apostrophes.
What's against single quotes?
You can have single/double quotes all over your html code without any problem, as long as you keep the same quoting style inside a current tags ( many browser won't complain even about this, and validation just want that if you start with a quote, end with the same, inside the same propriety )
Free support to random quoting styles!!! (yay ;D )
Single quotes are perfectly legal in (X)HTML. Using a backslash to escape them, on the other hand, isn't.
<img src='http://widget-site-example.com/ross.jpg' alt='Ross\'s Widget' />
is an image with the alt text "Ross\", and emptys
andWidget
/Widget'
attributes. The correct way of escaping an apostrophe in HTML is'
.If you want to the html to be valid html4 or xhtml then both single-quotes or double-quotes will work for attributes. HTML4 can be validated here: https://validator.w3.org/
If you are supporting only modern browsers (ie10 and higher) then you can use the html5 syntax which allows single quotes, double quotes, and no quotes (as long as there are no special characters in the attributes' value). HTML5 can be validated here: https://validator.w3.org/nu
I know this is an old thread, but still very much relevant.
If you want control over quotes in your generated HTML, you can use the sprintf() function in PHP (and similar calls available in many other languages):
Using sprintf() allows the format string to be easily modifiable by retrieving it from a database or configuration file, or via translation mechanisms.
It is very readable, and allows either double or single quotes in the generated HTML, with very little change and never any escaping:
Someone may use it in PHP to avoid escaping " if they're using double quoted string to parse variables within it, or to avoid using string concatenation operator.
Example:
instead of
or
Nowadays I always stick to using double quotes for HTML and single quotes for Javascript.
It's certainly valid to use single quotes (HTML 4.01, section 3.2.2). I haven't noticed such a trend, but perhaps there's some framework that powers web sites you've visited that happens to quote using single quotes.