57

What are the advantages and disadvantages (if any) of making sure that all pages validate compared to having non-valid HTML that however works on all major browsers?

Also, is having valid HTML after Javascript executes just as important?

8
  • 5
    This doesn't answer your question but... placing a doctype on your page will put the browser in standards mode instead of quirks mode. Look up quirks mode to see what I mean. Jul 9, 2010 at 5:09
  • 1
    @Evan Plaice - Not any DOCTYPE though. Some DOCTYPES actually trigger quirks or almost standard modes. The HTML5 spec explains this in more detail.
    – luiscubal
    Jul 15, 2010 at 19:49
  • 1
    @luiscubal Is that new in HTML 5 because from en.wikipedia.org/wiki/Quirks_mode, it states "... if a full DOCTYPE is present the browser will use standards mode, and if it is absent the browser will use quirks mode.". Jul 16, 2010 at 0:26
  • @Evan Plaice Not sure about previous HTML versions, but the HTML5 specifically states what to do with ancient DOCTYPES: see whatwg.org/specs/web-apps/current-work/multipage/…
    – luiscubal
    Jul 16, 2010 at 1:02
  • 1
    @Evan Plaice In other words, "DTD HTML 2.0 Level 1" triggers quirks mode.
    – luiscubal
    Jul 16, 2010 at 1:02

17 Answers 17

45

I think it's definitely worth doing, but you should never be a slave to validation -- it's a fool's game.

http://www.codinghorror.com/blog/2009/03/html-validation-does-it-matter.html

  1. Validate your HTML. Know what it means to have valid HTML markup. Understand the tooling. More information is always better than less information. Why fly blind?

  2. Nobody cares if your HTML is valid. Except you. If you want to. Don't think for a second that producing perfectly valid HTML is more important than running your website, delivering features that delight your users, or getting the job done.

5
  • 3
    I gotta second this. I have seen lots of issues with javascript libraries that can be blamed on invalid HTML. Multiple nested forms and illegally closed tags being major offenders. Like Jeff says don't be a slave, but don't complain when jQuery doesn't work because your page isn't valid HTML (XHTML, HTML 5 or whatever you pick as a doctype). Jul 17, 2010 at 4:22
  • @Jeff Atwood: can't I agree more when you say "Nobody cares if your HTML is valid. Except you." Sad but true, customers really do not care. Jul 23, 2011 at 15:37
  • @MarcoDemaio Why is it sad? As a client and an end-user, I am more concerned about whether or not the site works on all browsers (most of which aren't standards compliant to begin with) than whether or not it validates. Valid HTML really doesn't matter. Google, Facebook, Twitter, this site, etc. no relevant site has valid markup. Why? Because valid HTML does nothing but bloat the page and increase your bandwidth costs. Nov 30, 2011 at 0:45
  • The same thing goes for perfectly indented markup. This is even more useless, it's a 100% waste of bandwidth and has no practical use whatsoever. Nov 30, 2011 at 0:49
  • @NullUserException: I think it's sad because I found out validated websites usually renders much better on all browsers. See my comment to Alan's answer: webmasters.stackexchange.com/a/373/1429 Validating a website saved to me and still saves me a huge amount of time. About the perfect indented markup I never heard of specs about it. I might like to indent by 3 spaces, and you might like to indent by one. Dec 10, 2011 at 15:54
32

I consider valid HTML a worthwhile goal, but do not see it as the be-all and end-all of building good websites.

The trick is, your markup may be perfectly valid, yet it may not be semantic - e.g. using tables for layout or navigation. There's a difference between valid code and semantic code.

On another note, if you use advertising or external scripts, they may insert their own markup which has a chance to really mess with your own.

22

I think it's worth it, as I've caught many markup and logic errors by seeking validation. It's one of those "necessary but not sufficient" things. Valid markup, like code that compiles (or checks out via JSlint) free of errors, warnings, and hints, is a good first step in getting it right.

1
  • +1 totally agree on this one. Validating pages saves a huge amount of time running after JS and how-things-are-displaied bugs that seem so mysterious, and are only due to a worng or not closed HTML tag. Moreover with tools like FF addon Html Validator [addons.mozilla.org/en-US/firefox/addon/html-validator/] it's strightforward to validate all your pages locally. Jul 23, 2011 at 15:42
9

The big plus of valid HTML is that your page is then more accessible to things other than "major browsers". All of the "major browsers" have endless workarounds to deal with all the invalid junk which populates the WWW. However, sticking to valid HTML helps, for example, if someone is using a browser for the visually impaired, or accessing your pages off-line, etc.

8

Validation in of itself isn't so critical, since few browsers are 100% compliant and the spec isn't 100% clear on how to interpret the rules.

However being valid HTML puts you in a better position to adapt and improve your site. As the standards move, they will typically migrate forward and if you new site is valid, then updating to support the latest thing should be easier.

Bottom, being valid makes it easier to stay on top of the game and be as compatible as possible with the widest audience.

4

The best approach is to learn which invalid HTML is bad, and which invalid HTML does not matter.

For example, forgetting to close a <div> tag is very bad, because your layout will almost certainly screw up in one or more browsers.

However, using <br> instead of <br /> in XHTML does not matter - all browsers will interpret both as a line break without problems. Using the target attribute on links is invalid, but the worst case scenario is that the browser doesn't open the link in a new window.

3
  • target is valid in transitional XHTML, and only masochists use strict. Omitting the closing slash will make your page invalid XML, which will probably confuse screen scrapers. If you choose to use XHTML, your page should be valid XML at the least.
    – Tgr
    Jul 18, 2010 at 8:00
  • 1
    @Tgr: Funny, I thought masochists preferred non-standards mode. Even transitional doctypes have their problems (using "almost standards" mode etc) Jul 18, 2010 at 15:16
  • 1
    I'd argue that Strict is essential - why choose to run the risk of deprecated code and quirks mode. There is no cost to using Strict, other than that it encourages you to know more about your preferred markup version.
    – CJM
    Nov 2, 2010 at 11:22
3

When running the validator, you'll need to examine the errors it gives you on a case-by-case basis. Is validation important? To me, yes, it's very important. But is it a requirement? No.

Things like using the same ID multiple times (instead of a class), putting block-level elements inside inline-level elements (usually these elements don't fit this way semantically either), missing alt attributes on images (poor accessibility for the impaired), are all important. Things like unknown attributes on tags are NOT important. At all. Javascript frameworks like Dojo or that awful Meebo social-media bar use custom attributes as hooks, and the HTML spec states that these are allowed, and that any unknown attribute is to be ignored. The validator doesn't ignore them, though, it throws errors. These errors can be ignored.

When validating, do not just assume that if you have errors you're doing it wrong. Semantics are vastly more important, and it just so happens that valid HTML is more-often-than-not the natural result of having proper semantics.

1
  • I agree - validate your web page, but in some circumstances, you may choose to ignore the warnings, as long as you know why they are there
    – Casebash
    Jul 10, 2010 at 1:47
3

One reason to test your site for valid HTML is that it ensures that the search engine spiders will be able to fully index and determine the meaning of your pages. If they cannot do so due to malformed HTML (which the major browsers may work around for historical reasons) then you are potentially limiting your search engine rankings.

There has also been speculation that while the major search engines do a good job of dealing with malformed HTML, they may also assign page quality "points" for validity, further affecting your ability to rank as high as your content deserves.

3
  • 2
    Google has stated categorically that invalid HTML has no effect on rankings. However I can see the case where HTML is so malformed that the actual content on the page cannot be read by spiders - although in this case it's almost certain that browsers would start exhibiting rendering problems. Jul 18, 2010 at 15:23
  • @DisgruntledGoat You're right, here's a reference for that: youtube.com/watch?v=FPBACTS-tyg
    – JasonBirch
    Jul 18, 2010 at 19:17
  • @DisgruntledGoat Obviously... Google itself is full of invalid HTML, and I remember they said they really don't care and that it's a good thing to have invalid HTML if that means faster load times. Nov 30, 2011 at 0:55
3

I really don't think it matters anymore. I used to be a slave to validation, now I rarely ever check it. Perhaps I got burned out from making sure my site was valid, or perhaps I just didn't care anymore because nobody else will. I can guarantee 99.9% of our visitors don't even know what it is as well as even care if they did. Future browser software might, but when that day comes, I'll worry about it then.

2

Validation is useful because it can help you spot some hard-to-catch errors such as

<input name=foo value=<?php echo htmlspecialchars($_GET['foo']); ?> />

or unpredictable browser behavior (for example, putting block elements in an a can sometimes break in ugly ways in Firefox).

2

A point nobody has yet mentioned is that invalid HTML can cause slower rendering times whilst the browser is trying to make sense of the non standard HTML when displaying.

2
  • I would downvote this if I could. I highly doubt this has any observable effect; I would be more concerned with valid markup bloating the page and requiring more time to load (especially on slower/mobile connections). Nov 30, 2011 at 0:51
  • @NullUserExceptions: I don't think the point made by BradB deserves a -1. Maybe difficult to prove, but a browser that needs to sort out and fix inside an HTML mess might take a bit more than a well formatted valid HTML page with no errors in it. Why don't you provide an answer to this question showing us a good example of a over bloated page due to HTML validation abuse. I can't think at how a valid HTML page could be so overbloated compared to the same page with invalid HTML code. Dec 10, 2011 at 16:03
1

there is no disadvantage of having valid html. there is a reason why there is a spec in the first place and why a lot of effort is being put into the spec to define how things should work.

basically, all you gain is to meet the specs. which in turn means, programs written to read html (browsers, bots) can not blame YOU for not meeting the specs if something goes wrong. and some of these programs give you extrapoints (higher ranking in search engines if the bot reports "meets the spec"). if you meet the specs you will be caught much less by surprise if some browsers do not render broken html the way you think it should.

so, to meet the specs and write valid html is good for you, no disadvantages at all.

4
  • Hum, which search engines do you get higher rankings in if you meet the spec?
    – delete
    Jul 9, 2010 at 7:35
  • 2
    The disadvantage would be the additional development time you spend making sure that all your code meets the spec. Although this cost is generally minimal, it should still be addressed as a disadvantage.
    – chatche
    Jul 9, 2010 at 18:33
  • @kinopiko: If there are any, it is none of the major ones (Google, Yahoo, Bing, Ask). Having a complete mess of code that even a seasoned (human) web developer cannot read will probably hinder you, but using some "illegal" attributes has absolutely zero effect on rankings. Jul 18, 2010 at 15:20
  • That's the problem with the validation terminology. You're either valid or you're not. There are no degrees. Broken HTML (e.g. unclosed tags, misplaced/missing structural tags, etc.) is invalid and hurts SEO, but most people aren't talking about that when they say "validation". A novice might want to use a validator to make sure they haven't made any of those novice mistakes, but a professional developer doesn't need to since their code is already "valid enough" so to speak in term of SEO. Oct 17, 2010 at 10:02
1

Some HTML validation errors can cause non-obvious layout issues (e.g. wrongly nested/unclosed tags), JavaScript bugs (e.g. using an id more than once), and issues for some users (e.g. not including a meaningful or blank alt attribute on images).

If all our pages validate, that’s a nice automated check you can do to rule out sources of errors. If you leave some validation errors in because you know they’re not causing any harm, your check is no longer automated: you have to look at each error, and remember that it’s okay. Personally, I prefer it when computers reduce the amount of work I have to do rather than increase it.

1

One point no one's mentioned is future browser developments. Though all today's browsers handle invalid markup relatively well, that may not always be the case.

Browser makers in the future will be ensuring their browsers work to HTML/XHTML standards, so this is what web developers should be hitting as well. Just because a particular bit of invalid markup works now doesn't guarantee it will work in future browsers.

3
  • I have to say I wonder if that is true.
    – delete
    Jul 17, 2010 at 12:17
  • 2
    Yeah I can't see any browser ever dropping support for the <font> tag or its ilk. Jul 18, 2010 at 15:17
  • I don't see what the issue is - support for deprecated or invalid markup might change in future. Over-looking the imperfect implementation of(X)HTML in most browsers, surely you are going to be safer sticking with valid markup. There is no cost associated with valid markup, other than simply knowing what you are doing.
    – CJM
    Nov 2, 2010 at 11:25
1

Validity helps you avoid incompatibilities and helps keep code maintainable. Browsers recover from markup errors, but sometimes in very unintuitive ways.


  • DTD-based (HTML4, XHTML1 @ W3C) — Might not be worth it. DTD is primitive and, e.g., cannot check validity of most attributes. You'll mostly get hard to understand errors about entities and nesting.

  • HTML5 validatorYes. Definitely. HTML5 is more pragmatic and allows some harmless constructs that used to be errors. OTOH Henri's validator is much more thorough and better at discovering real problems.


Validity of JS-generated code might matter, as browsers operate on DOM, regardless how it was created. If you use document.write(), then you even have to take care to get syntax correct (it goes through same parser as page source).

1

Even if your HTML works on all major browsers, it's still worth doing as it sometimes can cause problems with search engine crawlers like googlebot. For example see this:

http://www.codeproject.com/KB/server-management/Google_Indexing_Problem.aspx

1

Google and Bing do not, have not and will never use CSS or HTML validation as a ranking factor.

The majority of websites have dozens to hundreds of errors and you need not worry about them because all search engines care about is how the page renders. Just ensure your website renders correctly in all major browsers and Google's Fetch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.