標籤

最近正在學習html5,剛接觸html5,感受有點不適應,由於有一些標籤改變了,特別是div, section article這三個標籤,查了一些資料,也試着用html5和css3佈局網頁,稍微有點頭緒了,下邊還有一個我剛剛佈局好的一個簡單的網頁,供你們參考,先看一下,最起碼內心對html5的結構有些概念。css

divhtml

HTML Spec: 「The div element has no special meaning at all.」html5

這個標籤是咱們見得最多、用得最多的一個標籤。自己沒有任何語義,用做佈局以及樣式化或腳本的鉤子(hook)。css3

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.」 app

與 div 的無語義相對,簡單地說 section 就是帶有語義的 div 了,可是千萬不要以爲真得這麼簡單。section 表示一段專題性的內容,通常會帶有標題。看到這裏,咱們也許會想到,那麼一篇博客文章,或者一條單獨的評論豈不是正好能夠用 section 嗎?接着看: ide

「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 應該何時用呢?再接着看: post

「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
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 中是否是完整的。看這段內容脫離了所在的語境,是否仍是完整的、獨立的。

 

總結:
div section article ,語義是從無到有,逐漸加強的。div 無任何語義,僅僅用做樣式化或者腳本化的鉤子(hook),對於一段主題性的內容,則就適用 section,而假如這段內容能夠脫離上下文,做爲完整的獨立存在的一段內容,則就適用 article。原則上來講,能使用 article 的時候,也是可使用 section 的,可是實際上,假如使用 article 更合適,那麼就不要使用 section 。nav 和 aside 的使用也是如此,這兩個標籤也是特殊的 section,在使用 nav 和 aside 更合適的狀況下,也不要使用 section 了。

對於 div 和 section、 article 以及其餘標籤的區分比較簡單。對於 section 和 article 的區分乍看比較難,其實重點就是看看這段內容脫離了總體是否是還能做爲一個完整的、獨立的內容而存在,這裏面的重點又在完整身上。由於其實提及來 section 包含的內容也能算做獨立的一塊,可是它只能算是組成總體的一部分,article 纔是一個完整的總體。

相關文章
相關標籤/搜索