Search code examples
htmlsemantic-markup

Are divs allowed within <section> and <article> elements?


Is it ok to use, for example:

<section>
  <div class="container">
    <div class="column">
      <h2>Perm page title</h2>
      <p>Page body copy.... </p>
    </div class="column">
    <div class="column">
      <iframe><!-- YT video --></iframe>
    </div class="column">
  </div>
</section>

<article>
  <div class="container">
    <div class="column">
      <h2>Blog post title</h2>
      <p>Post body copy.... </p>
    </div class="column">
    <div class="column">
      <iframe><!-- YT video --></iframe>
    </div class="column">
  </div>
</article>

Is it ok to use divs inside section and article like this or should section and article only contain h2 p etc?


Solution

  • As per the specs (W3C) (here) and (here):

    For, article:

    Permitted contents: Zero or more style elements, followed by flow content

    and for section:

    Content model: Flow content.

    And again, as per the specs (W3C) (this) and (this), div is "flow content", hence answer to your question is yes, it is ok to use div in this context.


    Further down, the following exerpts from the same specs are worth noting:

    The section element represents a generic section of a document or application. A section, in this context, is a thematic grouping of content. The theme of each section should be identified, typically by including a heading (h1-h6 element) as a child of the section element.

    In your example, you are not using h2 as a child of the section element but is rather wrapped by divs. This may not be semantic.

    And then, specifically this (as it pertains to the example in your question):

    Authors are encouraged to use the article element instead of the section element when it would make sense to syndicate the contents of the element.

    The section element is not a generic container element. When an element is needed only for styling purposes or as a convenience for scripting, authors are encouraged to use the div element instead. A general rule is that the section element is appropriate only if the element's contents would be listed explicitly in the document's outline.

    Bottom-line: If you are not using section semantically, then div is a recommended choice.