div、section、article 的区别

作者:糖伴西红柿 来源:前端观察 时间:2011-02-26 15:39:00 

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>

标签:div,html5,section,article
0
投稿

猜你喜欢

  • Pyspark读取parquet数据过程解析

    2022-01-21 13:33:38
  • Python 合并拼接字符串的方法

    2022-05-26 08:58:08
  • python制作填词游戏步骤详解

    2023-03-09 19:46:47
  • Python的Web框架Django介绍与安装方法

    2021-03-06 05:46:12
  • 15款Python编辑器的优缺点,别再问我“选什么编辑器”啦

    2021-10-06 12:37:55
  • python调用API接口实现登陆短信验证

    2021-03-13 00:04:03
  • python人工智能tensorflow函数tensorboard使用方法

    2021-04-21 14:52:46
  • Python容器类型转换的3种方法实例

    2022-06-03 13:32:32
  • python画图系列之个性化显示x轴区段文字的实例

    2023-01-16 06:15:24
  • python pycharm最新版本激活码(永久有效)附python安装教程

    2022-08-16 14:08:56
  • python笔记:mysql、redis操作方法

    2024-01-15 02:16:46
  • Mysql存储过程如何实现历史数据迁移

    2024-01-24 13:02:33
  • 详解Appium+Python之生成html测试报告

    2022-12-21 22:38:51
  • python实现希尔密码加密的示例代码

    2022-06-16 07:55:31
  • focus 进 textarea 元素后光标位置的修复

    2008-09-27 13:27:00
  • Go json自定义Unmarshal避免判断nil示例详解

    2024-01-29 18:44:24
  • Linux下python制作名片示例

    2022-06-07 00:29:33
  • 详解Django中的FBV和CBV对比分析

    2023-04-30 13:27:38
  • Python实现的排列组合、破解密码算法示例

    2022-01-03 10:42:29
  • 详解Node.js 中使用 ECDSA 签名遇到的坑

    2024-05-08 09:36:01
  • asp之家 网络编程 m.aspxhome.com