Why âConditional Commentsâ Are Bad, Repeat: Bad
Published on Feb 1, 2007 (updated Feb 5, 2024), filed under development, html, css, maintainability (feed). (Share this on Mastodon or Bluesky?)
This and many other posts are also available as a pretty, well-behaved ebook: On Web Development.
This post is not as clear as it could be (and the tone is odd). Please refer to more recent posts on Conditional Comments, most notably To Be Clear (on Conditional Comments and Resets).
âConditional Commentsâ (CC) are inadvisable to use. They contradict the goal of separating structure from presentation, and because of that they will hurt you one day.
But first things first. For those lucky developers who donât know what Conditional Comments are, please see Microsoftâs âspecificationâ or refer to the following code snippet. Itâs an HTML extract showing a comment that contains a link
to a specific IE style sheet.
<!--[if IE 5]>
<link rel="stylesheet" href="ie5.css" type="text/css">
<![endif]-->
Rarely is that the only style sheet included in a document; usually, CC users include their âregularâ style sheets and then one if not more style sheets to address Internet Explorer needs.
Whatâs Wrong With Conditional Comments?
Two things.
While Conditional Comments are syntactically valid, they are not standards-compliant: âInformation that appears between comments has no special meaning,â and comments âwill be ignored by the parser.â
(Interestingly, that is exactly where all other implementations except Microsoftâs Internet Explorer are correct: They just ignore comments.)
The standardization process was weak, at best. That is natural for proprietary solutions, and Conditional Comments are no exception. While you think they help you, they will eat you at some point, sinceâŚ
Conditional Comments guarantee your HTML to be changed, because they mix to-be-separated structure and presentation.
No matter how you use CC, it will be necessary to adjust the HTML of each and every document or template once you focus on other Internet Explorer versions. This happens even if youâre the most visionary master of web development and thereâs otherwise no need to update the structure of your documents. Remember that you strive for consistent separation of structure and formatting.
Normally, your next redesign would require you to update your style sheetsâeven better, your style sheet, singularâ, but now you entered the lottery and will not only need to maintain additional style sheets, no, youâre likely also forced to update all these IE comment queries.
Itâs easier and also more âpragmaticââif thatâs whatâs desiredâto use the naughtiest (yet valid) CSS hacks and filters and workarounds. At least they live in style sheets, at least they only force you to do work when it comes to one file type.
Tips
Set guardrails and taboos for your work, and these should include âno Conditional Comments.â Communicate that. Explain why they donât help. I know companies where work results are desired (and have already been promised to clients) that you cannot otherwise accomplish, but solve this problem at the root, ask your team members if these results are useful, and instead rely on graceful degradation.
â§ Apologies for this somewhat cluttered guerrilla post, but I wanted to make this point.
Update (February 2, 2007)
For clarification: When I talk about using âthe naughtiest CSS hacks,â I recommend them over Conditional Comments (my IE character escape filter post includes a few notes concerning the use of hacks). Use them scarcely, use them wisely, but donât forget that theyâre a different storyâCSS âhacksâ usually depend on parsing bugs, syntax errors, or missing support for certain spec ingredients, while Conditional Comments have been intentionally created to violate the HTML specification.
Generally, thereâs only one case where Conditional Comments might not become a problem: When youâre a one-man-show whoâs responsible for HTML and CSS maintenance. In other cases, you wonât only force yourself butâworseâteam members to update HTML templates and style sheets, and even work on a file level (removing ie5.css, renaming ie.css, creating ie8b.css). A quick âsearch and replaceâ wonât do when CC changes become necessary.
Donât get lured. Conditional Comments appear safe, they look valid, but thatâs an illusion, and theyâre bad.
About Me
Iâm Jens (long: Jens Oliver Meiert), and Iâm a web developer, manager, and author. Iâve been working as a technical lead and engineering manager for companies youâve never heard of and companies you use every day, Iâm an occasional contributor to web standards (like HTML, CSS, WCAG), and I write and review books for OâReilly and Frontend Dogma.
I love trying things, not only in web development and engineering management, but also in other areas like philosophy. Here on meiert.com I share some of my experiences and views. (I value you being critical, interpreting charitably, and giving feedback.)