Why Don’t you need to close tags in HTML5?


HTML5 is a much more forgiving when it comes to Mark-up than XHTML. With XHTML you needed to make sure that each opening HTML tag had it’s own closing tag. So you would always close an image tag like this:

<img alt="A pretty car" src="image.jpg" />

If you forgot to close the tag, like this:

<img alt="A pretty car" src="image.jpg" >

And you tried to validate the document then you would end up with an error like this:

An error message from the W3C Validator

However, try the same thing with the W3C HTML5 validator and you will discover that it’ doesn’t have an issue with it.

The HTML5 specification has standardised the way that browsers deal with the problem of malformed HTML5 so you don’t need to worry about it… unless of course you want to. You see, you can use XHTML5 which insists that every tag must be explicitly closed (amongst other things). In short the issue of people not correctly closing tags was so widespread that it was considered a better approach for all the browser manufacturers to agree on how they should deal with such issues rather than fighting an up hill battle to convince everyone to write XML based mark-up.

For what it is worth, I continue to close tags even though I don’t need to… it makes makes the OCD side of my personality happy.

Comments (11)

  1. Josh says:

    "makes makes the OCD side" … that an attempt at irony?

  2. OCD2 says:

    "that it' doesn't have" 🙂

  3. tkrause says:

    How to force XHTML5? Is there a special DOCTYPE for that?

  4. Chris says:

    So it promotes bad code. Glad it won't completely break, but sad it's not enforced.

  5. Bjørn Johansen says:

    @tkrause Same DOCTYPE, but serve it as application/xhtml+xml

  6. Steve Fenton says:

    It isn't sad that things aren't enforced. The Web is for everyone, not just developers. That's why the XHTML standards were rejected by so many people.

    There isn't a missing closing tag on the example given. In HTML tags such as img or br don't have a closing tag as they don't allow content. That actually makes a lot of sense: tags only have a closing tag if you can sensibly put something inside.

  7. szabi says:

    Bullshit. Write proper xml or stfu – unfortunately there are times in history, when the stupid win and the sane loose. Sanity – an uphill battle, yes.

  8. Typical trash from Microsoft. says:

    This is rubish. Try creating a div without Closing it nd see what that does to you layout

  9. rikkit says:

    Re: "Typical trash from Microsoft."

    Sad that you feel you need to troll here. You clearly didn't even read the article.

  10. thebeebs says:

    "Typical trash from Microsoft." – This stuff isn't from Microsoft… It's from me (So it should read: "Typical trash from Martin Beeby"). It's also from the HTML5 standard. Of course not closing divs in sites will cause you layout problems in older browser. However, since the HTML5 standard, browser vendors have been given direction as to what they should do with Malformed HTML. Moving forward not closing tags will "fail" consitantly across browsers.

  11. Alan says:

    Why??? I ask W3C, why to make HTML diferent from XML… thats a dumb move.

Skip to main content