4

In IE10, I create the following web page, and get an unmatched tag error:

<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
    <p>
        <div>
            &nbsp;
        </div>
    </p>
</body>
</html>

I can find no problem with this code. I have narrowed it to having a div inside a paragraph as what is triggering the problem. IE10 reports:

HTML1509: Unmatched end tag.
test.php, line 12 character 2

and is pointing to the paragraph close token. An earlier me might have just ignored it. But I can't make myself do that any more.

I have searched this site for something similar, but have found only legitimate syntax errors; valid explanations for the error, in other words. I know IE is brain-dead, but even post-Bill MSFT can't be silly enough to let a bug like that one, through (can they?). So I am assuming it's me, until proven otherwise. Possibly some subtlety of HTML5 I haven't grokked yet...

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • 2
    See http://stackoverflow.com/questions/10763780/putting-div-inside-p-is-adding-an-extra-p – Michael Berkowski Jun 29 '13 at 20:26
  • 2
    If you validate your markup in http://validator.w3.org/check, you'll see the error there too. So for once, it is IE being correctly strict, whereas most other browsers will probably just silently fix your markup to make it valid. – Michael Berkowski Jun 29 '13 at 20:28
  • 1
    Probably IE 10 tries to fix your page by closing the `

    ` before the `

    `, but isn't smart enough to re-open a new `

    ` after the `

    `
    – Gus Jun 29 '13 at 20:28
  • 1
    @Gus It's not a matter of smartness, IE attempts to closely follow the current standards. Other browsers just consume whatever crap we write, contrary to IE. – Rob W Jun 29 '13 at 20:33
  • Oh my. I see it now. It's been a misunderstanding on my own part. For years... Woof. I did not realize the p tag was restricted in that way. But, thinking on it, that makes (some) sense. – Phil - Computer Whisperer Jun 29 '13 at 20:36
  • 1
    @RobW Browser fixup code has been a bane from the start, promoting sloppy coding and bloating the browser rendering code. If it were actually enforcing the rules it would say
    not allowed in

    . However since this has almost the same effect as actually enforcing the standard I'm happy to see it. Just be nice if the message made sense

    – Gus Jun 29 '13 at 20:37

1 Answers1

16

You can't have DIV (or any other block element) in a P unless you serve your markup as application/xhtml+xml. All pages served as text/html are parsed by HTML parser (in modern browsers, including IE10, it's HTML5 parser, regardless the doctype). In HTML, the content model of P element is 'phrasing content' (HTML5) or 'inline' (HTML4), and its end tag is optional in all versions of HTML. So the parser implicitly closes the P element before the opening tag of any block element. And in all browsers the DIV element will be not the child of P, but the sibling, and CSS selectors like p > div will never work for pages served as text/html.

Ates Goral
  • 137,716
  • 26
  • 137
  • 190
Ilya Streltsyn
  • 13,076
  • 2
  • 37
  • 57