HTML and CSS Reference
In-Depth Information
2.7 Validating HTML5
Problem
You want to identify and eliminate coding errors in your HTML.
Solution
Validating your code is one of the key best practices in web development and is a great
first step in eliminating coding errors. While validation doesn't guarantee that your
web page will look or behave like you expect, it does guarantee that any unexpected
behavior is not due to a mismatched tag or unrecognized CSS selector.
Why validate? Fortunately (or unfortunately, depending on your position on web
standards), most web browsers try to compensate for invalid coding. So, even a
horrendous collection of tag soup consisting of mismatched tags and improper nesting
will probably look okay in most browsers. However, different browsers handle errors
differently.
That means that if your code isn't valid, a browser can try to compensate for errors in
unexpected ways. Additionally, mobile browsers are less forgiving of erroneous code
(they simply don't have the resources to compensate for errors the way that desktop
browsers can). So, an invalid page that renders fine on your desktop browser may break
on your mobile phone. In light of modern web technologies, it is becoming increasingly
important to validate your code and eliminate errors. In doing so, you go one step
further in ensuring that your web page renders as expected across multiple browsing
environments.
Fortunately, validating for HTML5 is not much different than validating XHTML/
HTML4. Indeed, you may find that creating a valid HTML5 document is much easier,
as the HTML5 specification, in general, is much more lenient than its predecessors. If
you want to capitalize your HTML tags, go ahead; the HTML will still validate. If you're
too lazy to close out your li tags, you need not feel guilty for doing so. The validator
won't chastise you—it will leave that job up to your colleagues.
So, let's get on to the business of validating our code. Currently, there are two popular
online validators that you can use:
• The W3C's Unicorn unified validator (shown in Figure 2-8 ), which performs a
variety of checks and can validate HTML5 and CSS3: http://validator.w3.org/uni
corn/
• The Validator.nu HTML5 Validator: http://html5.validator.nu/
 
Search WWH ::




Custom Search