溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

HTML5中div和section以及article的區(qū)別是什么

發(fā)布時(shí)間:2020-09-10 11:03:03 來(lái)源:億速云 閱讀:235 作者:小新 欄目:web開(kāi)發(fā)

這篇文章主要介紹了HTML5中div和section以及article的區(qū)別是什么,具有一定借鑒價(jià)值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。

剛剛開(kāi)始接觸 HTML5 時(shí),對(duì)它的標(biāo)簽很不適應(yīng),甚至一度有點(diǎn)反感。尤其是對(duì) div、section、article 這幾個(gè)標(biāo)簽,實(shí)在弄不清楚應(yīng)該使用在什么場(chǎng)合下。
div

HTML Spec:

The p element has no special meaning at all.

這個(gè)標(biāo)簽是我們見(jiàn)得最多、用得最多的一個(gè)標(biāo)簽。本身沒(méi)有任何語(yǔ)義,用作布局以及樣式化或腳本的鉤子(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 的無(wú)語(yǔ)義相對(duì),簡(jiǎn)單地說(shuō) section 就是帶有語(yǔ)義的 div了,但是千萬(wàn)不要覺(jué)得真得這么簡(jiǎn)單。section 表示一段專題性的內(nèi)容,一般會(huì)帶有標(biāo)題??吹竭@里,我們也許會(huì)想到,那么一篇博客文章,或者一條單獨(dú)的評(píng)論豈不是正好可以用 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.

當(dāng)元素內(nèi)容聚合起來(lái)更加言之有物時(shí),應(yīng)該使用 article 來(lái)替換 section 。

那么,section 應(yīng)該什么時(shí)候用呢?再接著看:

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 應(yīng)用的典型場(chǎng)景有文章的章節(jié)、標(biāo)簽對(duì)話框中的標(biāo)簽頁(yè)、或者論文中有編號(hào)的部分。一個(gè)網(wǎng)站的主頁(yè)可以分成簡(jiǎn)介、新聞和聯(lián)系信息等幾部分。其實(shí)我對(duì)這里傳達(dá)信息很感興趣,因?yàn)楦杏X(jué) section 和下面要介紹的 artilce 更加適用于模塊化應(yīng)用,這個(gè)話題以后會(huì)出篇專門的文章來(lái)討論,這里暫時(shí)略過(guò)。

要注意,W3C 還警告說(shuō):

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 p 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 不僅僅是一個(gè)普通的容器標(biāo)簽。當(dāng)一個(gè)標(biāo)簽只是為了樣式化或者方便腳本使用時(shí),應(yīng)該使用 p 。一般來(lái)說(shuō),當(dāng)元素內(nèi)容明確地出現(xiàn)在文檔大綱中時(shí),section 就是適用的。

<article>
    <hgroup> <h2>Apples</h2> <h3>Tasty, delicious fruit!</h3> </hgroup>
    <p>The apple is the pomaceous fruit of the apple tree.</p>
    <section>
        <h2>Red Delicious</h2>
        <p>These bright red apples are the most common found in many supermarkets.</p>
    </section>
    <section>
        <h2>Granny Smith</h2>
        <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 是一個(gè)特殊的 section 標(biāo)簽,它比 section 具有更明確的語(yǔ)義, 它代表一個(gè)獨(dú)立的、完整的相關(guān)內(nèi)容塊。一般來(lái)說(shuō), article 會(huì)有標(biāo)題部分(通常包含在 header 內(nèi)),有時(shí)也會(huì) 包含 footer 。雖然 section 也是帶有主題性的一塊內(nèi)容,但是無(wú)論從結(jié)構(gòu)上還是內(nèi)容上來(lái)說(shuō),article 本身就是獨(dú)立的、完整的。

HTML Spec 中接著又列舉了一些 article 適用的場(chǎng)景。

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.

當(dāng) article 內(nèi)嵌 article 時(shí),原則上來(lái)說(shuō),內(nèi)部的 article 的內(nèi)容是和外層的 article 內(nèi)容是相關(guān)的。例如,一篇博客文章中,包含用戶提交的評(píng)論的 article 就應(yīng)該潛逃在包含博客文章 article 之中。

問(wèn)題是怎么才算“完整的獨(dú)立內(nèi)容”?有個(gè)最簡(jiǎn)單的判斷方法是看這段內(nèi)容在 RSS feed 中是不是完整的??催@段內(nèi)容脫離了所在的語(yǔ)境,是否還是完整的、獨(dú)立的。

例子:

<article>
    <header>
        <h2>The Very First Rule of Life</h2>
        <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>
        <h2>The Very First Rule of Life</h2>
        <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>
        <h2>Comments</h2>
        <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>

總結(jié)

p section article ,語(yǔ)義是從無(wú)到有,逐漸增強(qiáng)的。p 無(wú)任何語(yǔ)義,僅僅用作樣式化或者腳本化的鉤子(hook),對(duì)于一段主題性的內(nèi)容,則就適用 section,而假如這段內(nèi)容可以脫離上下文,作為完整的獨(dú)立存在的一段內(nèi)容,則就適用 article。原則上來(lái)說(shuō),能使用 article 的時(shí)候,也是可以使用 section 的,但是實(shí)際上,假如使用 article 更合適,那么就不要使用 section 。nav 和 aside 的使用也是如此,這兩個(gè)標(biāo)簽也是特殊的 section,在使用 nav 和 aside 更合適的情況下,也不要使用 section 了。

對(duì)于 p 和 section、 article 以及其他標(biāo)簽的區(qū)分比較簡(jiǎn)單。對(duì)于 section 和 article 的區(qū)分乍看比較難,其實(shí)重點(diǎn)就是看看這段內(nèi)容脫離了整體是不是還能作為一個(gè)完整的、獨(dú)立的內(nèi)容而存在,這里面的重點(diǎn)又在完整身上。因?yàn)槠鋵?shí)說(shuō)起來(lái) section 包含的內(nèi)容也能算作獨(dú)立的一塊,但是它只能算是組成整體的一部分,article 才是一個(gè)完整的整體。

因?yàn)槠鋵?shí)有些時(shí)候每個(gè)人都有自己的看法,所以難免有難于決斷的時(shí)候,怎么辦?

在 HTML5 設(shè)計(jì)原理 中,有一條是專門用來(lái)解決類似情況的:

最終用戶優(yōu)先(Priority of Constituencies)

“In case of conflict, consider users over authors over implementors over specifiers over theoretical purity.” 一旦遇到?jīng)_突,最終用戶優(yōu)先,其次是作者,其次是實(shí)現(xiàn)者,其次標(biāo)準(zhǔn)制定者,最后才是理論上的完滿。

推薦各位多讀幾遍 HTML5 設(shè)計(jì)原理,這才是紛繁世界背后的最終奧義。

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享HTML5中div和section以及article的區(qū)別是什么內(nèi)容對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,遇到問(wèn)題就找億速云,詳細(xì)的解決方法等著你來(lái)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI