6 HTML5 Validation Errors and How to Fix Them

All web designers should validate their site using W3C Markup Validation Service. It’s very easy to use. Simply put the link to your website in the box (or use the file upload or direct input options) and press “Check.” The service will then scan your markup to find any errors according to the doctype you selected.

Here are a few examples of validation errors and how to fix them. Some may be specific to HTML5 but others may apply to multiple doctypes.

1. Rel Types

WordPress automatically adds rel=”category tag” to some of the links on your site. Unfortunately, the HTML5 standard has recently ruled that only registered link type extensions are allowed in rel keywords. If you want your site to validate, you need to change or remove these rel keywords!

Add this code to your functions.php file in your theme’s directory:

add_filter( 'the_category', 'add_nofollow_cat' ); 
function add_nofollow_cat( $text ) {
$text = str_replace('rel="category tag"', "", $text); return $text;
}

Code Source

This will replace rel=”category tag” with rel=”nofollow”.

2. Lightbox

Lightbox

If you use Lightbox your code will not validate for HTML5. In order to trigger the lightbox effect you have to embed rel=”lightbox” into your link. See the problem? It’s the same issue with rel keywords as above, but the solution is a little different.

Here, the solution is to replace “rel” with “class”. So when you want to trigger lightbox, use this code:

<a href="images/image-1.jpg" class="lightbox" title="my caption">image #1</a>

Notice how rel=”lightbox” has been changed to class=”lightbox”. Unfortunately this is only half of the job. We also have to edit the JavaScript file to switch the trigger from “rel” to “class”.

When fixing this problem myself, I just opened up my lightbox source code, did a control+F for “rel” and replaced it with “class.” This should work perfectly fine. The solution also works with other similar add ons (such as FancyBox). For more specific FancyBox instructions you can refer to this entry from PaceToScreen.

The one problem I still have is that changing from rel to class seems to disable the grouping feature, which links images together and allows you to navigate through them by pressing the “Next” and “Previous” buttons. If you find a solution, please post it here!

UPDATE: It seems that with Lightbox v2.51 grouping now works when changing from rel to class! Special thanks to the commenters ^_^

3. StumbleUpon Badges

When adding my own social media links to my site, I noticed that the code for StumbleUpon’s badge was not validating. Here is the code they provide:

<!-- Place this tag where you want the su badge to render -->
<su:badge layout="1"></su:badge>

<!-- Place this snippet wherever appropriate --> 
 <script type="text/javascript"> 
 (function() { 
     var li = document.createElement('script'); li.type = 'text/javascript'; li.async = true; 
      li.src = 'https://platform.stumbleupon.com/1/widgets.js'; 
      var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(li, s); 
 })(); 
 </script>

Now the JavaScript is fine, but the HTML that renders the badge generates this W3C error:

Element su:badge not allowed as child of element body in this context. (Suppressing further errors from this subtree.)

What’s the solution? Personally I was a little dumbfounded. I didn’t even know where to begin with fixing this. Luckily, my boyfriend came to the rescue! You can leave the JavaScript alone (starting at “Place this snippit wherever appropriate”) but change:

<su:badge layout="1"></su:badge>
to:
<iframe width="74" height="18" style="overflow: hidden; margin: 0pt; padding: 0pt; border: 0pt none;" src="http://www.stumbleupon.com/badge/embed/1/?url=<?php echo rawurlencode("http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']); ?>" id="iframe-stmblpn-widget-1"></iframe>

You may have to make a few adjustments (for example, to the “width” and “height” attributes) if you use a different button style. In this example I’m using the first badge (“layout 1”).

4. HTML Entities

Now this is a very common error that is not specific to HTML5, but luckily it’s very easy to fix! Many people forget to convert their special characters to their entity name. The most common un-converted symbol is easily ampersand. Instead of just typing out “&” use the following code in your webpages:

&amp;

For a full list of symbols and their HTML entity equivalent visit W3Schools’s Entity List.

5. Self-Closing Tags

This actually only applies when using XHTML, but it’s usually a good habit to get into. XHTML demands that all tags be closed. In other doctypes it is perfectly acceptable to add a line break with:

<br>

Or display an image with:

<img src="http://www.mysite.com/myimage.jpg" alt="My Image">

However, in XHTML, these would be considered unclosed tags. How do we fix them? Like so!

<br />
<img src="http://www.mysite.com/myimage.jpg" alt="My Image" />

Other common self-closing tags include (but are not limited to) meta, input, area, and hr.

6. Facebook Open Graph

Facebook Open Graph

Facebook’s Open Graph protocol is an awesome tool that allows you to control the appearance of content that gets “liked” from your site on Facebook. Unfortunately, the meta syntax that makes this work does not validate in HTML5 and probably never will (here’s hoping…). This was a huge problem with my own site, as it was the only thing keeping it from validating 100% and I was annoyed that something like this was holding me back. After scouring the internet for a workaround, I finally found one!

<?php
	function is_facebook(){
		if(!(stristr($_SERVER["HTTP_USER_AGENT"],'facebook') === FALSE)) {
			return true;
		}
	}
?>
<!DOCTYPE html>
<html dir="ltr" lang="en-US"<?php if(is_facebook()){echo ' xmlns:fb="http://www.facebook.com/2008/fbml" xmlns:og="http://opengraphprotocol.org/schema/"';}?>>
<head>
	<title><?php bloginfo('name'); ?></title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<?php if(is_facebook()){ ?>
		<meta property="og:title" content="<?php single_post_title(''); ?>"/>
		<meta property="og:type" content="article"/>
		<meta property="og:image" content="<?php echo wp_get_attachment_thumb_url( get_post_thumbnail_id( $post->ID ) ) ?>"/>
		<meta property="og:url" content="http://<?php echo $_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];?>"/>
		<meta property="og:site_name" content="<?php bloginfo('name'); ?>"/>
		<meta property="fb:appid" content="<?=$your_fb_app_id?>"/>
	<?php }?>
</head>

Original Source (I slightly modified it)

I omitted the description field so that Facebook will pick up the website’s meta description, which is uniquely generated for each of my blog posts. This is particularly useful if you’re using a WordPress plugin such as the All in One SEO Pack.

Don’t forget you can also test how your page will appear on Facebook when “liked” using Facebook’s open graph debugger.

Unfortunately you cannot use this method if you cache your pages (unless your cache is really advanced and makes an exception for the Facebook user agent)

Have any extra tips?

What validation errors have you encountered and how did you fix them? Share here!

Don't miss my next post!

Sign up to get my blog posts sent directly to your inbox (plus exclusive store discounts!).

You might like these

29 comments

  1. Thank you SO SO much
    …the user agent check before serving meta is a brilliant idea!
    And works fine 🙂

    Original code will send opengraph meta to facebook useragent, exclude the rest. I modified a bit, to that it will exclude W3C_Validator user agent, and serve opengraph meta to all the rest
    Using W3 total Cache i think i can also exclude there W3C_Validator from receiving cached pages, so for everyone else cache should work as intended

    1. I just discovered that issue mylesf. Unfortunately I can’t fix it until tomorrow. It’s only affecting gallery plugins at this time and it’s due to the way Lightbox Plus modifies the page content during output. The gallery plugins output image separately from WordPress’s image output and there is a class that is not being added. If you have a way to add custom classes to your gallery output add class= imagebox and it should work. I’m going to remove the need for the class probably tomorrow or Monday. Thanks for letting me know.

  2. Can you explain this line please?
    Can you explain those links?

    <html dir="ltr" lang="en-US">

    1. dir=”ltr” is a way of defining your document and saying that your text should be displayed from “left to right.” The alternative is dir=”rtl” (right to left).

      The lang=”en-US” is defining the content as American English. Alternatives would be lang=”fr” for French, and lang=”es” for Spanish.

      Defining these elements help search engines and web browsers better understand and render your website.

      Does that answer your question? 🙂

    2. Hi there, First: thanks for your hard work. I’ve been using Lightbox for a short time now, but I think it’s a great puglin and it adds some extra class to my site. Since the most recent update it’s not working anymore though. I updated WordPress to 2.8, then lightbox to 1.5. As gallery-program I’m using laziest gallery. It worked great before the updates and now I don’t get a nice box anymore when viewing photo’s. If you like, you could check it at my site under Foto’s . There are some galleries there. I reset all settings, then checked the right boxes again. I checked the gallery-script, everything is still installed. Could this problem have to do with your most recent update? Sincerely, Michiel

  3. Nice article. I had also achieved HTML5 validation using tip 1

    Another tip to add to the list is if in the head section you have:
    //wp_get_archives(‘type=monthly&format=link’);
    This outputs rel=”archives” which breaks HTML5 validation so I do not use it.

    Tip 5 is not actually true. HTML5 does not have to be well-formed unless you are using XHTML5 by adding the XHTML namespace to the html tag. In fact, many HTML5 warriors actively encourage we drop all closing tags (even p tags, etc.), don’t quote attributes, drop MIME types (use simply <script>…</script> and <style>…</style>) but I still like to stay well-formed because I believe the more instructions with give to the browser, the faster it can render, out-weighing the benefits of reduced band-width when using sloppy mark-up. I also pass my HTML documents through XML parsers and transform with XSLT, both of which require us to be well-formed.

    Data Attribute Fix
    The Digg code requires a rev attribute but this is not valid HTML5. A new all encompassing attribute in HTML5 is data-*, where the wildcard * can be anything you like. Digg needs to honour this attribute so that we can use data-rev=”value” instead of rev=”value”. Now everyone is happy.

    WordPress could also use this solution for some of its controversial attributes, such as rel=”category tag”, which fails HTML5 validation (Your tip 1). Instead they could do the right thing and use rel=”tag” data-category=”products”.

    HTML5 and CSS3 are the future. It’s not just about mark-up and presentation. We have all the new and exciting APIs. Validating HTML5 is the first step we need to take and the sooner nearly every CMS package take this on board, the faster we can all move forward.

    Thank goodness for saviors and good HTML5 disciples like yourself. Well done.

    1. Thanks so much for your great comments 🙂 I’ll edit #5 – Self-Closing Tags. Fail on my part!

      Hopefully WordPress and social media buttons will be more HTML5-friendly in the future!

  4. Maybe the problem comes from cliconft between the Lightbox script and script for menu. But as the Lightbox is popular,so I think there’re maybe some problems with the menu script. I’m not sure, but you can use Firebug to debug the JS.

  5. Lightbox 2 – do the same as your work around but change rel=”lightbox[1]” for
    data-lightbox=”lightbox[1]” and change the relevant “rel” in lightbox.js, but do this manually as it will also change pRELoader (caps for emphasis of what can be changed by auto search)

    I am using the latest version of Lightbox 2.51 I think and doing this allows the grouping or gallery to work and validates

      1. I’m using Lightbox v2.51.
        After changing in lightbox.js all rel to class I don’t get any problems with grouping.
        eg.

        1. a href=images/large/am11.jpg
          class=lightbox[Ann]

          img src=images/thumb/am11sma.jpg
          style= width:60;height:150;
          alt=Ann15

          Hope this helps.

      2. ashley, did you get lightbox 2.51 to work with grouping after changing rel to class

  6. After looking into a handful of the blog articles on your web page, I honestly appreciate your technique of writing a blog.

    I added it to my bookmark website list and will be checking back soon.
    Take a look at my website too and let me know how you
    feel.

  7. Can’t seem to make the lightbox work even though I have removed all the Rel’s for classes. Is there something that I am missing?

    Thanks

  8. Hello, I found this article because I am trying to get my site to validate and the stupid stumbleupon is not working. I tried your work around and it just created a bunch of new errors. This was not helpful to me at all. Please revise and check it out for your self.

Recent Posts

    Random Posts