HTML5 教程学习 - section元素标签

w3c-html5-logo-viiiix根据W3C最新定义规范:

section元素标签用来表现普通的文档内容或应用区块。一个section通常由内容及其标题组成...但section元素标签并非一个普通的容器元素;当一个容器需要被直接定义样式或通过脚本定义行为时,推荐使用div元素而非section。

下面的代码展示了一个最简单并且典型的section:

<section>
<h1>Any level of heading</h1>
rest of the content
</section>

通常不推荐为那些没有标题的内容使用<section>;我们可以使用HTML5轮廓工具来检查我们的页面中是否有不包含标题部分的<section>容器。

下面的范例来自W3C官方;这一篇关于苹果的短文,其中包括两个简短的section:

<article>
<hgroup>
<h1>Apples</h1>
<h2>Tasty, delicious fruit!</h2>
</hgroup>
<p>The apple is the pomaceous fruit of the apple tree.</p>
<section>
<h1>Red Delicious</h1>
<p>These bright red apples are the most common found in many
supermarkets.</p>
</section>
<section>
<h1>Granny Smith</h1>
<p>These juicy, green apples make a great filling for
apple pies.</p>
</section>
</article>

我们可以看到,虽然文章有正式的h1与h2,但是在<section>中,仍然可以为这个内容部分使用h1来作为标题。

下面是几条关于使用<section>的禁忌:

  • 不要将<section>作为用来设置样式行为的“钩子”容器,那是<div>的工作。
  • 如果<article>、<aside><nav>更符合状况,不要使用<section>。
  • 不要为没有标题的内容区块使用<section>。

译者:Viiiix7210

原文参考:html5doctor.comW3C HTML5 规范


收藏与分享

添加评论


Security code
换一张图