Should I write HTML with single or double quotes?
By point of personal preference, I am always encouraging students to be consistent with their usage of capitalization, indentations and quotation mark usage. Code should be as clean as possible and consistency throughout is an easy way to catch small errors or discrepancies.
In some cases, however, you can actually find valid reasoning behind those personal preferences; in the case of All Caps vs. Sentence Case vs. lower case, different servers and languages perceive filenames and code entities as uniquely different items depending on the casing (See this About.com article for some good file naming recommendations).
When deciding whether to use single and double quotes in HTML files, you'll find that either option will validate perfectly (you can either validate to see or check out the spec) AND depending on the development team you end up working with or the open-source library your project implements, chances are that you will come across a variety of uses. For example, I personally always close my HTML attributes values with double quotes, but Google's popular web font service provides embed code that uses single quotes. Neither option is wrong and you could mix and match happily if desired.
Here's the "however" point: When working with some server-side languages (my example is from PHP, but I'm sure this matters for at least a few others), there are times when you would want to purposefully use single quotes over double quotes and choosing between the two actually has important meaning.
echo "<a href="$link" id="$id">$urlText</a>";
In this example, I am using the PHP echo command to add the HTML markup I have to the page, hence the outside double quotes. The markup I want to add also uses double quotes, per my personal preference. PHP won't understand the above statement and even if it did, it is really easy for me to get confused as to what's happening in this statement after a few hours of coding.
echo "<a href=\"$link\" id=\"$id\">$urlText</a>";
If all I am worried about is PHP properly running, I could escape each of my internal double quotes and then run my statement. It would make PHP happy, but you can see in the above example how clumsy this could get as you continue to build strings throughout your web pages and applications.
echo "<a href='$link' id='$id'>$urlText</a>";
In the above option, we'll swap out the internal double quotes for singles and get something both functional and cleaner. PHP is happy and if you are consistent, it will become an easy way to spot errors in your PHP commands that involve HTML markup.
The Bottom Line
If you aren't coding with anything more advanced than HTML and CSS, then you aren't going to hit any problems when choosing single over double quotes or mixing and matching. But if you plan to go beyond these simple front-end languages, begin now to make a good distinction in your usage. For me, that means sticking with double quotes for attribute value definitions and only switching as required.
- Open in Textmate
- Open in Textmate (uploaded by DRA Studio)Had a really fun lunch meeting with one of the WDD (Web Design & Development and) degree course directors yesterday. It is so enjoyable being able to sit around and talk about web standards, markup and CSS syntax. Yes, Courtney, that is really nerdy … but still, so much ...
- Snow Leopard & Perl – Having 64-bit babies
- Sadly, one of my favorite lil' Mac tools, Validator, which is perfect for validating XML files against custom Document Type Declarations (DTDs), is currently not working with Snow Leopard, or more specifically, Snow Leopard's implementation of 64-bit Perl. There is an easy way have 10.6 load the 32-bit version of Perl, but unfortunately, even after doing ...
- Astro Boy (2009)
- 3 out of 5 butt-deployed machine gunsThe other movie I got to enjoy on my double-feature cheap theater Sunday was Astro Boy and even though I wasn't crazy about the overall execution of the film, there were some really great pieces. I loved all of the character designs by Imagi and most of the voice ...
- Passing the time
- In case you have forgotten, be sure and head back over to Orisinal for some silly fun Flash gaming. Every single one is a beautiful masterpiece. ...
- Restore to Defaults
- One only has to sit in on my DFP Icon lecture, perused my Flickr uploads tagged osx, or (for you privileged few) use my Mac for a few minutes to realize that I am very particular AND very protective of the way my Finder windows look. I have nice sidebar separators, several essential tool bar ...