Is it correct to use single quotes for HTML attributes?
Asked Answered
E

16

79

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&#39;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.

Eran answered 28/10, 2008 at 10:18 Comment(1)
I wish this had become popular to start! Because double quotes were more popular it became common JavaScript style to use single quotes in JavaScript, because strings would be full of HTML with double quotes. Now I've typed so many single quote strings in JavaScript that whenever I use a other language like Python, C#, C++ I end up typing single quotes and get syntax errors 😭 – Depredate
H
60

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.

Hunger answered 28/10, 2008 at 10:22 Comment(2)
Funnily enough it's mainly sites that give external code out - for example a stats image for Ohloh or as in pushuptheweb.com – Eran
Google Font Library is now providing sample code with single quotes: google.com/webfonts – Spanishamerican
D
47

I find using single quotes is handy when dynamically generating HTML using a programming language that uses double quote string literals.

e.g.

String.Format("<a href='{0}'>{1}</a>", Url, Desc)
Dissonance answered 28/10, 2008 at 10:25 Comment(5)
What? You dont like String.Format("<a href=" & ControlChars.Quote & "{0}" & ControlChars.Quote & ">{1}</a>", Url, Desc) ??? =P – Darius
String.Format(@"<a href=""{0}"">{1}</a>", Url, Desc) this is what you might consider :) – Baby
or String.Format("<a href=\"{0}\">{1}</a>", Url, Desc) – Wildwood
3 perfect examples of extreme ugliness :) – Odle
and this is a great example of how to create XSS vulnerabilities in your site – Reeder
I
12

When using PHP to generate HTML it can be easier to do something like:

$html = "<img src='$url' />";

than concatenating a string with a variable with a string, as PHP parses variables in double-quoted strings.

Immotile answered 28/10, 2008 at 10:28 Comment(5)
It's also easier than staring at $html = "<img src=\"{$url}\" />"; As escaped quotes are all to easy to forget – Catachresis
actually you can reverse it in php as well: $html = '<img src="$url" />'; – Quartet
@KevinLaity, using single quotes in PHP would make $url print out as $url, not the variable contents: php.net/manual/en/language.types.string.php – Dignitary
alternatives don't make it more readable either: $html = '<img src="'.$url.'" />' – Cornejo
I use formatted strings, like sprintf('<img src="%s" />', $url) – Massicot
T
5

Another case where you may want to use single quotes: Storing JSON data in data attributes:

<tr data-info='{"test":true}'></tr>

JSON object keys must be in double quotes, so the attribute itself cannot.

Tate answered 4/12, 2016 at 14:24 Comment(0)
K
4

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:

echo "<input type='text' value='$data'/>";

instead of

echo "<input type=\"text\" value=\"$data\" />";

or

echo '<input type="text" value="' . $data . '" />';

Nowadays I always stick to using double quotes for HTML and single quotes for Javascript.

Kindergartner answered 28/10, 2008 at 13:17 Comment(0)
D
3

It's easier when you want to embed double quotes.

Douzepers answered 28/10, 2008 at 10:32 Comment(4)
Same applies if you completely reverse the scenario... so not a good reason IMO – Apogamy
@JoePhillips Not always, some languages don't let you simply 'reverse the scenario'. .NET, for example, will only allow strings to be defined using double quotes. – Hackle
@Hackle This is true. But if you're storing javascript in C# strings, you're probably doing all kinds of things wrong – Apogamy
@JoePhillips I didn't mention JS but that would be just ONE reason to do it and, no, there is nothing wrong with doing this. One common example would be for cache busting purposes. Reponse.Write(String.Format("<script src='/js/myjs.js?{0}' type=text/javascript></script>", MakeUniqueStringFun())); This example would be better if it wouldn't work just as well like so: <script src='/js/myjs.js?<%=MakeUniqueStringFun()%>' type=text/javascript></script> but one is not really more correct than the other and it should be obvious that longer snippets of JS become unwieldy using the later method. – Hackle
I
3

In ASP.NET, it's easier to use single quotes if you're using data-binding expressions in attributes:

<asp:TextBox runat="server" Text='<%# Bind("Name") %>' />
Ingratitude answered 28/10, 2008 at 10:51 Comment(0)
B
2

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 empty s and Widget/Widget' attributes. The correct way of escaping an apostrophe in HTML is &#39;.

Bihari answered 14/1, 2009 at 18:49 Comment(2)
Single quotes are not perfectly legal in XHTML. – Apogamy
Single quotes are legal XHTML, aren't they? – Superior
H
2

In PHP, echo takes multiples parameters. So, if one would like to omit the concatenation operator, they could done something like and still use double quotes :

echo '<input type="text" value="', $data, '" />';
Hufuf answered 27/7, 2010 at 18:5 Comment(3)
but by the time you realize it, you already started your echo string with double quotes, so you just use singles inside the string. That happens to me all the time. – Priming
This is also more memory efficient, as it avoids creating a new string. – Eskill
This doesn't seem to answer the question at all. – Hackle
I
1

Single quotes generate a cleaner page with less clutter. You shouldn't be escaping them in HTML strings and it's your choice which to use... my preference is single quotes normally and if I need to include a single quote in the string (e.g. as delimiters for a string inside the string), I use double quotes for one & single for the other.

Imtiaz answered 10/10, 2010 at 12:56 Comment(0)
U
1

If you want to the to be valid or then both or will work for attributes. HTML4 can be validated here: https://validator.w3.org/

If you are supporting only modern browsers ( and higher) then you can use the 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

Unpen answered 30/10, 2015 at 16:18 Comment(0)
S
0

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 )

Spermatophore answered 28/10, 2008 at 10:31 Comment(0)
S
0

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

$html = sprintf('<a href="%s">%s</a>', $url, $text);

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:

$html = sprintf("<a href='%s'>%s</a>", $url, $text);
Snead answered 2/2, 2013 at 6:26 Comment(0)
Z
0

Why not save pressing the SHIFT Key. One less keystroke for the same milage.

Zipangu answered 29/7, 2013 at 17:23 Comment(1)
ha! best general reason, I've seen. – Bitter
B
0

Let's settle this argument for good


  1. Double quotes are OK
  2. Single quotes are OK
  3. No quotes are OK, as long as you don't have one of these characters:
    • space (obviously)
    • "<" or ">" (makes sense)
    • equal sign (not sure why but ok)
    • any kind of quote: double, single, backtick (again, makes sense)

Funny how some people, even today, stick to the "double quote only" standard which was never really a standard but a convention. Also, if your language has apostrophes as a natural element (English, anyone?) it's more convenient, but in my opinion, increasing the complexity of PHP string compositions is way too high a price to pay for this.

Brennen answered 17/3, 2021 at 12:50 Comment(2)
Attribute values are assigned with the equals character (=). Allowing this character as part of the value would obviously confuse it. – Indecisive
@manngo Sure, but there would be no harm in interpreting only the first one as separator, taking every further equal-sign as part of the value. – Brennen
S
-7

You should avoid quotes altogether.

In your example only one quoted attribute actually needed quotes.

<!-- best form -->
<a href=http://widget-site-example.com/example.html>
  <img src=http://widget-site-example.com/ross.jpg alt='Ross&#39;s Widget' />
</a>

If you do use quotes, there is no hard and fast rule, but I've seen most commonly single quotes, with double quotes on the inside if necessary.

Using double quotes won't pass some validators.

Shostakovich answered 17/10, 2014 at 18:56 Comment(1)
Quotes are required for the content to be valid html 4.01 – Unpen

© 2022 - 2024 β€” McMap. All rights reserved.