作为主要部分的部分

问题描述:

使用section元素作为页面的主要内容部分在语义上是否正确?


作为主要部分的部分

大致没有。

HTML5Doctor says

我们已经做错了,是用部分包裹的内容,以它的样式,或者从导航,标题划分主要内容区域,页脚等。这些是div的工作,而不是部分。

自写了这篇文章以来,已经发明了<main>元素,为您的确切用例。

+0

[我会说](http://stackoverflow.com/a/14894715/1591669):宽泛地说,是的。否则,你不可能有一个单独的'header' /'footer',它只属于主内容而不是整个页面。 – unor 2013-02-15 12:46:03

您可以,但我鼓励您使用新的main元素!

例如

<main role="main"> 

</main> 

the main elementfurther examples on using the main element

你可以说,只要你有一个标题(h1-h6)不是一个切片元素的标题暗示使用section

所以这两个文件在语义上是等价的:

1)

<body> 
    <h1>John's cool site</h1> 
    <nav>…</nav> 
    <h2>A nice article</h2> <!--- main content starts with this heading --> 
    <p>I like bees.</p> 
</body> <!-- main content ends at the end of the document --> 

2)

<body> 
    <h1>John's cool site</h1> 
    <nav>…</nav> 
    <section><!--- main content starts with this opening tag --> 
    <h2>A nice article</h2> <!-- now you could use h1 here, too --> 
    <p>I like bees.</p> 
    </section><!-- main content ends with this closing tag --> 
</body> 

所以,是的,你可以使用section为主要内容(在很多情况下, article会更合适,但)。您可以使用section代替标题内容组(除非articlenavaside当然应该使用)。

只要您对所有这些标题内容组使用分段元素,就可以在任何地方使用h1

甚至有一些边缘情况下,您需要明确使用主要内容的切片元素。也就是说,如果您的网页包含的内容不属于主要内容的一部分,并且不适合headerfooteraddress(或aside/nav)。如果您在这种情况下不使用section(或article),则此内容仍属于主要内容。

更(我想甚至可以说,非常)常见的例子,你应该使用section/article为主要内容将是一个页面,您需要一个单独的header/footer为主要内容,例如博客帖子页面:整个页面可能有一个footer(其中包含有关整个博客的信息)。现在,如果我们不会为主要内容(→博客文章)使用分段元素,那么我们不能单独使用footer(包含作者信息,标签,类别等)。)只是为了那篇文章。

<body> 
    <h1>John's blog</h1> 
    <article> 
    <h1>My first blog post</h1> 
    <p>…</p> 
    <footer> <!-- applies to the blog post only (article) --> 
     Tags: introduction, aboutme 
    </footer> 
    </article> 
    <footer> <!-- applies to the whole page (body) --> 
    Blog of John Doe — 2013 
    <p><small>All content licensed under Creative Commons</small></p> 
    </footer> 
</body> 

所以我建议:如果有疑问,显式地使用section/article一个页面的主要内容。