`
小小猪馁
  • 浏览: 18935 次
  • 性别: Icon_minigender_2
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

HTML5 中 div section article 的区别

阅读更多

刚刚开始接触 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 就是适用的。

<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>

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 中是不是完整的。看这段内容脱离了所在的语境,是否还是完整的、独立的。

例子:

<article> <header> <h1>The Very First Rule of Life</h1> <p><time pubdate datetime="2009-10-09T14:28-08:00"></time></p> </header> <p>If there's a microphone anywhere near you, assume it's hot and sending whatever you're saying to the world. Seriously.</p> <p>...</p> <footer> <a href="?comments=1">Show comments...</a> </footer> </article>
<article> <header> <h1>The Very First Rule of Life</h1> <p><time pubdate datetime="2009-10-09T14:28-08:00"></time></p> </header> <p>If there's a microphone anywhere near you, assume it's hot and sending whatever you're saying to the world. Seriously.</p> <p>...</p> <section> <h1>Comments</h1> <article> <footer> <p>Posted by: George Washington</p> <p><time pubdate datetime="2009-10-10T19:10-08:00"></time></p> </footer> <p>Yeah! Especially when talking about your lobbyist friends!</p> </article> <article> <footer> <p>Posted by: George Hammond</p> <p><time pubdate datetime="2009-10-10T19:15-08:00"></time></p> </footer> <p>Hey, you have the same first name as me.</p> </article> </section> </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.” 一旦遇到冲突,最终用户优先,其次是作者,其次是实现者,其次标准制定者,最后才是理论上的完满。

 

http://blog.sina.com.cn/s/blog_8f7509310100vmqf.html

本文转自

分享到:
评论

相关推荐

    HTML5_中_div_section_article_的区别

    HTML5_中_div_section_article_的区别 div:本身没有任何语义,用作布局以及样式化或脚本的钩子(hook)。 section 表示一段专题性的内容,一般会带有标题。 article 是一个特殊的 section 标签,它比 section 具有更...

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

    尤其是对 div、section、article 这几个标签,实在弄不清楚应该使用在什么场合下。div HTML Spec:  The div element has no special meaning at all. 这个标签是我们见得最多、用得最多的一个标签。本身没有任何...

    HTML5中div、article、section的区别及使用介绍

    div, section article这三个标签的基本概念想必大家对其不是很了解下,语义是从无到有,逐渐增强,下面有一个简单的网页,供大家参考,希望可以对html5的结构有所熟悉

    HTML5中的Article和Section元素认识及使用

    HTML5带出了一系列新元素,并且将在未来被广泛应用。然而,有一些元素在使用时易被混淆,包括以下两个新元素:&lt;article&gt;和&lt;section&gt;。 最常被问起的问题是:在什么情况下我们应该使用这些元素?以及我们应该如何正确...

    Web前端基础:HTML5语义化标签.pptx

    HTML5语义化标签&lt;article&gt;示例&lt;article&gt; 文章标题&lt;/h1&gt; 这是一篇文章 &lt;article&gt;评论1...&lt;/article&gt; &lt;article&gt;评论2...&lt;/article&gt;&lt;/article&gt;HTML5语义化标签&lt;section&gt;标签&lt;section&gt;标签定义文档中的节(section、区段...

    Html5中文手册(程序员必备手册)

    26、&lt;div&gt; 定义文档中的一个部分。 27、&lt;dl&gt; 定义定义列表。 28、&lt;dt&gt; 定义一个定义列表里的项目。 29、&lt;embed&gt; 定义外部交互内容或插件。 30、&lt;fieldset&gt; 可将表单内的相关元素分组。 31、&lt;figcaption&gt; 定义 figure...

    解读html5关于html5的应用与认识

    • 不需要闭合标签 在 HTML5 中,空标签(如:br、img 和 input )并不需要闭合标签。 • 废弃的标签 下面这些标签并不被 HTML5 支持: &lt;acronym&gt;、&lt;applet&gt;、&lt;basefont&gt;、&lt;big&gt;、&lt;...

    详解HTML5布局和HTML5标签

    一、新的文档类型声明(DTD) ...结构标签:(块状元素) 有意义的div  &lt;article&gt; 标记定义一篇文章   标记定义一个页面或一个区域的头部  &lt;nav&gt; 标记定义导航链接  &lt;section&gt; 标记定义一个区域  &lt;aside&gt; 标

    从入门到精通HTML5——PDF——网盘链接

     第12章 HTML 5与HTML 4的区别 225  视频讲解:51分钟  12.1 语法的改变 226  12.1.1 HTML 5的语法变化 226  12.1.2 HTML 5中的标记方法 226  12.1.3 HTML 5语法中的3个要点 227  12.1.4 标签实例 228  12.2...

    不使用class和id进行网页布局的方法

    结合了富有新的语义化标记的HTML5,CSS3为web设计师们的网页提供了神一般的...div#news {} div.section {} div.article {} div.header {} div.content {} div.footer {} div.aside {} 我们再来看看基于HTML5的实例:

    html5的新增的标签和废除的标签简要概述

    例:HTML5中&lt;section&gt;……&lt;/section&gt;;HTML4中&lt;div&gt; ……&lt;/div&gt;。 article元素 表示页面中一块与上下文不相关的独立内容。比如一篇文章。 aside元素 表示article元素内容之外的、与article元素内容相关的辅助信息。 ...

    微信小程序-将html文件转为微信小程序用的wxml文件

    body,article,aside,ul,li,ol,caption,dd,dl,dt,footer,header,nav,section,table,thead,tbody,tr,td,th,h1,h2,h3,h4,h5,h6,div,p 将img转化为image 将a转化为navigator 将span,s,b,i,strong转化为text 安装...

    HTML5和CSS3价格表翻转切换动画特效.zip

    html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, ...

    html-interview-questions:100 多个 HTML5 面试问题

    HTML5 面试问题 ... 拉取请求受到高度赞赏。 目录 号码 问题 01. 02. 03. ... 解释块元素和行内元素的区别?... 什么时候应该使用section 、 div或article ? 17. 一个网页可以包含多个元素吗? 元素呢?

    HTML5中新标签和常用标签详解

    html5 新增的结构元素,有的经常使用到,有的用不上,虽然说它们的出现是更促进页面更有语义,更好的 seo,但其实当页面禁用样式后,它们的展现跟 div 是没撒差别,有同学可能会说,既然一样,又不影响页面的最终...

    HTML5之语义标签介绍

    在HTML5标准中,新加了几个用于增添页面语义的标签,这些标签有:article、section、nav和aside等。与别的大多数标签不 同,浏览器在解释渲染这些标签的时候仅仅把它作为普通的div块级元素来处理,不会添加任何额外...

    html入门到放弃笔记

    HTML中用于描述功能的符号称为"标记" 标记在使用时,用尖括号 "&lt;&gt;",标记的分类 1、封闭类型的标记 也称为 "双标记" , 必须成对出现 语法:&lt;标记&gt;内容标记&gt; Demo : 1、创建 p 标记 --&lt;p&gt;...&lt;/p&gt; 2、创建 ...

    CSS 语义化标记抛弃DIV标记

    曾经,设计师们经常会跟频繁使用基于table的没有任何语义的布局。...复制代码代码如下: &lt;div id=”news”&gt; &lt;div class=”section”&gt; &lt;div class=”article”&gt; &lt;div class=”header”&gt; &lt;h1&gt;Div Soup Demo

Global site tag (gtag.js) - Google Analytics