DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML5教程
HTML5語義化標籤:div、section、article到底該用哪個
編輯:HTML5教程     

剛接觸 HTML5 時,很多人對新標籤會有一段適應期,尤其是 div、section、article 這三個,常常分不清各自的適用場合。本文把它們的規範定義與判斷方法一次講清楚。

div:沒有任何語義

規範原文寫得很直接:The div element has no special meaning at all.

這是我們見得最多、用得最多的一個標籤。它本身不具備語義,作用是作為佈局容器,以及樣式化或腳本的鉤子(hook)。

section:帶有主題的內容分塊

規範定義: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 element.

也就是說,當元素內容聚合起來更言之有物、適合被整體分發(syndicate)時,應該改用 article。

那 section 究竟什麼時候用?規範舉的例子是:文章的章節、標籤式對話框中的各個標籤頁、論文中編號的部分。一個網站首頁可以拆分為簡介、新聞、聯繫資訊等幾個 section。

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。一條實用準則是:只有當這段內容會被明確列進文件大綱(outline)時,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:獨立且完整的內容單元

規範定義: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 本身就是自成一體、可以獨立存在的。

規範列舉的適用場景包括:論壇帖子、雜誌或報紙文章、部落格文章、用戶提交的評論、互動組件或小工具,或任何其他獨立的內容項。

當 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>...</header>
<p>...</p>
<section>
<h1>Comments</h1>
<article>
<footer><p>Posted by: George Washington</p></footer>
<p>Yeah! Especially when talking about your lobbyist friends!</p>
</article>
<article>
<footer><p>Posted by: George Hammond</p></footer>
<p>Hey, you have the same first name as me.</p>
</article>
</section>
</article>

總結與判斷方法

div、section、article 三者,語義是從無到強遞增的:div 無語義,僅作樣式化或腳本化的鉤子;一段帶主題的內容用 section;而若這段內容能脫離上下文作為完整、獨立的存在,就用 article。

原則上能使用 article 的地方也可以用 section,但如果 article 更合適,就不要退而求其次用 section。nav 和 aside 同理,它們本質上也是特殊的 section,在更合適時同樣不應被 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 設計原理多讀幾遍,那是紛繁細節背後真正的主線。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved