Why can't the

tag contain a

tag inside it?

前端 未结 5 2045
小蘑菇
小蘑菇 2020-11-21 22:09

As far as I know, this is right:

some words

But this is wrong:

&l

相关标签:
5条回答
  • 2020-11-21 22:49

    In short, it is impossible to place a <div> element inside a <p> in the DOM because the opening <div> tag will automatically close the <p> element.

    0 讨论(0)
  • 2020-11-21 22:57

    Because the div tag has higher precedence than the p tag. The p tag represents a paragraph tag whereas the div tag represents a document tag.

    You can write many paragraphs in a document tag, but you can't write a document in a paragraph. The same as a DOC file.

    0 讨论(0)
  • 2020-11-21 22:58

    An authoritative place to look for allowed containment relations is the HTML spec. See, for example, http://www.w3.org/TR/html4/sgml/dtd.html. It specifies which elements are block elements and which are inline. For those lists, search for the section marked "HTML content models".

    For the P element, it specifies the following, which indicates that P elements are only allowed to contain inline elements.

    <!ELEMENT P - O (%inline;)*            -- paragraph -->
    

    This is consistent with http://www.w3.org/TR/html401/struct/text.html#h-9.3.1, which says that the P element "cannot contain block-level elements (including P itself)."

    0 讨论(0)
  • 2020-11-21 23:01

    According to HTML5, the content model of div elements is flow content

    Most elements that are used in the body of documents and applications are categorized as flow content.

    That includes p elements, which can only be used where flow content is expected.

    Therefore, div elements can contain p elements.


    However, the content model of p elements is Phrasing content

    Phrasing content is the text of the document, as well as elements that mark up that text at the intra-paragraph level. Runs of phrasing content form paragraphs.

    That doesn't include div elements, which can only be used where flow content is expected.

    Therefore, p elements can't contain div elements.

    Since the end tag of p elements can be omitted when the p element is immediately followed by a div element (among others), the following

    <p>
      <div>some words</div>
    </p>
    

    is parsed as

    <p></p>
    <div>some words</div>
    </p>
    

    and the last </p> is an error.

    0 讨论(0)
  • 2020-11-21 23:01

    After the X HTML, the conventions has been changed, and now it's a mixture of conventions of XML and HTML, so that is why the second approach is wrong and the W3C validator accepts the things correct that are according to the standards and conventions.

    0 讨论(0)
提交回复
热议问题