详解HTML5中div和section以及article的区别


Posted in HTML / CSS onJuly 14, 2015

刚刚开始接触 HTML5 时,对它的标签很不适应,甚至一度有点反感。尤其是对 div、section、article 这几个标签,实在弄不清楚应该使用在什么场合下。
div

HTML Spec:

    The div element has no special meaning at all.

这个标签是我们见得最多、用得最多的一个标签。本身没有任何语义,用作布局以及样式化或脚本的钩子(hook)。
section

HTML Spec: “The section element represents a generic section of a document or application. A section, in this context, is a thematic grouping of content, typically with a heading.”

与 div 的无语义相对,简单地说 section 就是带有语义的 div 了,但是千万不要觉得真得这么简单。section 表示一段专题性的内容,一般会带有标题。看到这里,我们也许会想到,那么一篇博客文章,或者一条单独的评论岂不是正好可以用 section 吗?接着看:

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

当元素内容聚合起来更加言之有物时,应该使用 article 来替换 section 。

那么,section 应该什么时候用呢?再接着看:

    Examples of sections would be chapters, the various tabbed pages in a tabbed dialog box, or the numbered sections of a thesis. A Web site’s home page could be split into sections for an introduction, news items, and contact information.

section 应用的典型场景有文章的章节、标签对话框中的标签页、或者论文中有编号的部分。一个网站的主页可以分成简介、新闻和联系信息等几部分。其实我对这里传达信息很感兴趣,因为感觉 section 和下面要介绍的 artilce 更加适用于模块化应用,这个话题以后会出篇专门的文章来讨论,这里暂时略过。

要注意,W3C 还警告说:

    The section element is not a generic container element. When an element is needed 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.”

section 不仅仅是一个普通的容器标签。当一个标签只是为了样式化或者方便脚本使用时,应该使用 div 。一般来说,当元素内容明确地出现在文档大纲中时,section 就是适用的。

XML/HTML Code复制内容到剪贴板
  1. <article>     
  2.     <hgroup> <h1>Apples</h1> <h2>Tasty, delicious fruit!</h2> </hgroup>  
  3.     <p>The apple is the pomaceous fruit of the apple tree.</p>    
  4.     <section>    
  5.         <h1>Red Delicious</h1>    
  6.         <p>These bright red apples are the most common found in many supermarkets.</p>    
  7.     </section>    
  8.     <section>    
  9.         <h1>Granny Smith</h1>  
  10.         <p>These juicy, green apples make a great filling for apple pies.</p>    
  11.     </section>    
  12. </article>    

article

HTML Spec:

    The article element represents a self-contained composition in a document, page, application, or site and that is, in principle, independently distributable or reusable, e.g. in syndication.

article 是一个特殊的 section 标签,它比 section 具有更明确的语义, 它代表一个独立的、完整的相关内容块。一般来说, article 会有标题部分(通常包含在 header 内),有时也会 包含 footer 。虽然 section 也是带有主题性的一块内容,但是无论从结构上还是内容上来说,article 本身就是独立的、完整的。

HTML Spec 中接着又列举了一些 article 适用的场景。

    This could be a forum post, a magazine or newspaper article, a blog entry, a user-submitted comment, an interactive widget or gadget, or any other independent item of content.

当 article 内嵌 article 时,原则上来说,内部的 article 的内容是和外层的 article 内容是相关的。例如,一篇博客文章中,包含用户提交的评论的 article 就应该潜逃在包含博客文章 article 之中。

问题是怎么才算“完整的独立内容”?有个最简单的判断方法是看这段内容在 RSS feed 中是不是完整的。看这段内容脱离了所在的语境,是否还是完整的、独立的。

例子:

XML/HTML Code复制内容到剪贴板
  1. <article>     
  2.     <header>    
  3.         <h1>The Very First Rule of Life</h1>    
  4.         <p><time pubdate datetime="2009-10-09T14:28-08:00"></time></p>  
  5.     </header>    
  6.     <p>If there's a microphone anywhere near you, assume it's hot and sending whatever you're saying to the world. Seriously.</p>    
  7.     <p>...</p>    
  8.     <footer>  
  9.         <a href="?comments=1">Show comments...</a>  
  10.     </footer>    
  11. </article>  
  12.   
  13. <article>     
  14.     <header>    
  15.         <h1>The Very First Rule of Life</h1>  
  16.         <p><time pubdate datetime="2009-10-09T14:28-08:00"></time></p>    
  17.     </header>  
  18.     <p>If there's a microphone anywhere near you, assume it's hot and sending whatever you're saying to the world. Seriously.</p>    
  19.     <p>...</p>    
  20.     <section>  
  21.         <h1>Comments</h1>    
  22.         <article>  
  23.             <footer>  
  24.                 <p>Posted by: George Washington</p>  
  25.                 <p><time pubdate datetime="2009-10-10T19:10-08:00"></time></p>    
  26.             </footer>  
  27.             <p>Yeah! Especially when talking about your lobbyist friends!</p>    
  28.         </article>    
  29.           <article>  
  30.             <footer>    
  31.                 <p>Posted by: George Hammond</p>  
  32.                 <p><time pubdate datetime="2009-10-10T19:15-08:00"></time></p>  
  33.             </footer>  
  34.             <p>Hey, you have the same first name as me.</p>  
  35.         </article>    
  36.     </section>    
  37. </article>    

总结

div section article ,语义是从无到有,逐渐增强的。div 无任何语义,仅仅用作样式化或者脚本化的钩子(hook),对于一段主题性的内容,则就适用 section,而假如这段内容可以脱离上下文,作为完整的独立存在的一段内容,则就适用 article。原则上来说,能使用 article 的时候,也是可以使用 section 的,但是实际上,假如使用 article 更合适,那么就不要使用 section 。nav 和 aside 的使用也是如此,这两个标签也是特殊的 section,在使用 nav 和 aside 更合适的情况下,也不要使用 section 了。

对于 div 和 section、 article 以及其他标签的区分比较简单。对于 section 和 article 的区分乍看比较难,其实重点就是看看这段内容脱离了整体是不是还能作为一个完整的、独立的内容而存在,这里面的重点又在完整身上。因为其实说起来 section 包含的内容也能算作独立的一块,但是它只能算是组成整体的一部分,article 才是一个完整的整体。

因为其实有些时候每个人都有自己的看法,所以难免有难于决断的时候,怎么办?

在 HTML5 设计原理 中,有一条是专门用来解决类似情况的:

最终用户优先(Priority of Constituencies)

“In case of conflict, consider users over authors over implementors over specifiers over theoretical purity.” 一旦遇到冲突,最终用户优先,其次是作者,其次是实现者,其次标准制定者,最后才是理论上的完满。

推荐各位多读几遍 HTML5 设计原理,这才是纷繁世界背后的最终奥义。

HTML / CSS 相关文章推荐
css3实现图片遮罩效果鼠标hover以后出现文字
Nov 05 HTML / CSS
使用css3制作登录表单的步骤
Apr 07 HTML / CSS
CSS3按钮鼠标悬浮实现光圈效果源码
Sep 11 HTML / CSS
CSS3动画:5种预载动画效果实例
Apr 05 HTML / CSS
css3媒体查询中device-width和width的区别详解
Mar 27 HTML / CSS
Html5移动端获奖无缝滚动动画实现示例
Jun 25 HTML / CSS
HTML5 常用语法一览(列举不支持的属性)
Jan 26 HTML / CSS
HTML5 Canvas阴影使用方法实例演示
Aug 02 HTML / CSS
基于HTML5代码实现折叠菜单附源码下载
Nov 27 HTML / CSS
全面解析HTML5中的标准属性与自定义属性
Feb 18 HTML / CSS
HTML5新特性之type=file文件上传功能
Feb 02 HTML / CSS
html5 canvas绘制网络字体的常用方法
Aug 26 HTML / CSS
一些常用的HTML5模式(pattern) 总结
Jul 14 #HTML / CSS
针对HTML5的Web Worker使用攻略
Jul 12 #HTML / CSS
极简的HTML5模版
Jul 09 #HTML / CSS
深入探究HTML5的History API
Jul 09 #HTML / CSS
HTML5中Localstorage的使用教程
Jul 09 #HTML / CSS
HTML5到底会有什么发展?HTML5的前景展望
Jul 07 #HTML / CSS
HTML5新增的8类INPUT输入类型介绍
Jul 06 #HTML / CSS
You might like
一个图形显示IP的PHP程序代码
2007/10/19 PHP
php at(@)符号的用法简介
2009/07/11 PHP
PHP中的插件机制原理和实例
2014/07/08 PHP
PHP实现自动登入google play下载app report的方法
2014/09/23 PHP
PHP读取CURL模拟登录时生成Cookie文件的方法
2014/11/04 PHP
老生常谈PHP面向对象之解释器模式
2017/05/17 PHP
javascript学习笔记(二) js一些基本概念
2012/06/18 Javascript
jQuery 数据缓存模块进化史详细介绍
2012/11/19 Javascript
jquery分页插件jpaginate在IE中不兼容问题
2014/04/22 Javascript
DOM操作一些常用的属性汇总
2015/03/13 Javascript
javascript表单处理具体实现代码(表单、链接、按钮)
2016/05/07 Javascript
JavaScript中判断数据类型的方法总结
2016/05/24 Javascript
JS使用eval()动态创建变量的方法
2016/06/03 Javascript
js创建数组的简单方法
2016/07/27 Javascript
vue2.0嵌套路由实现豆瓣电影分页功能(附demo)
2017/03/13 Javascript
关于vue v-for 循环问题(一行显示四个,每一行的最右边那个计算属性)
2018/09/04 Javascript
Node.JS用纯JavaScript生成图片或滑块式验证码功能
2019/09/12 Javascript
js实现可爱的气泡特效
2020/09/05 Javascript
[01:09]模型精美,特效酷炫!TI9不朽宝藏Ⅰ鉴赏
2019/05/10 DOTA
遗传算法python版
2018/03/19 Python
python 实现敏感词过滤的方法
2019/01/21 Python
Python标准库shutil模块使用方法解析
2020/03/10 Python
Django serializer优化类视图的实现示例
2020/07/16 Python
HMV日本官网:全球知名的音乐、DVD和电脑游戏零售巨头
2016/08/13 全球购物
威尔逊皮革:Wilsons Leather
2018/12/07 全球购物
Koral官方网站:女性时尚运动服
2019/04/10 全球购物
幼师求职自荐信范文
2014/01/26 职场文书
元旦晚会邀请函
2014/01/27 职场文书
励志演讲稿范文
2014/04/29 职场文书
机关门卫的岗位职责
2014/04/29 职场文书
日语专业求职信
2014/07/04 职场文书
学校领导干部民主生活会整改方案
2014/09/29 职场文书
上班迟到检讨书300字
2014/10/18 职场文书
迎客户欢迎词三篇
2019/09/27 职场文书
在HTML中引入CSS的几种方式介绍
2021/12/06 HTML / CSS
GTX1650super好不好 gtx1650super显卡属于什么级别
2022/04/08 数码科技