語義元素清楚地向瀏覽器和開發(fā)者描述其意義。
非語義元素的例子:<div> 和 <span> - 無法提供關(guān)于其內(nèi)容的信息。
語義元素的例子:<form>、<table> 以及 <img> - 清晰地定義其內(nèi)容。
Yes | Yes | Yes | Yes | Yes |
所有現(xiàn)代瀏覽器均支持 HTML5 語義元素。
此外,您可以“幫助”老式瀏覽器處理“未知元素”。
在 HTML5 瀏覽器支持這一章學(xué)習(xí)更多知識。
許多網(wǎng)站包含了指示導(dǎo)航、頁眉以及頁腳的 HTML 代碼,例如這些:<div id="nav"> <div class="header"> <div id="footer">。
HTML5 提供了定義頁面不同部分的新語義元素:
<section> 元素定義文檔中的節(jié)。
根據(jù) W3C 的 HTML 文獻:“節(jié)(section)是有主題的內(nèi)容組,通常具有標題”。
可以將網(wǎng)站首頁劃分為簡介、內(nèi)容、聯(lián)系信息等節(jié)。
<section> <h1>WWF</h1> <p>The World Wide Fund for Nature (WWF) is....</p> </section>
<article> 元素規(guī)定獨立的自包含內(nèi)容。
文檔有其自身的意義,并且可以獨立于網(wǎng)站其他內(nèi)容進行閱讀。
<article> 元素的應(yīng)用場景:
<article> <h1>What Does WWF Do?</h1> <p>WWF's mission is to stop the degradation of our planet's natural environment, and build a future in which humans live in harmony with nature.</p> </article>
在 HTML5 標準中,<article> 元素定義完整的相關(guān)元素自包含塊。
<section> 元素被定義為相關(guān)元素塊。
我們能夠使用該定義來決定如何嵌套元素嗎?不,我們不能!
在因特網(wǎng)上,您會發(fā)現(xiàn) <section> 元素包含 <article> 元素的 HTML 頁面,還有 <article> 元素包含 <sections> 元素的頁面。
您還會發(fā)現(xiàn) <section> 元素包含 <section> 元素,同時 <article> 元素包含 <article> 元素。
<header> 元素為文檔或節(jié)規(guī)定頁眉。
<header> 元素應(yīng)該被用作介紹性內(nèi)容的容器。
一個文檔中可以有多個 <header> 元素。
下例為一篇文章定義了頁眉:
<article> <header> <h1>What Does WWF Do?</h1> <p>WWF's mission:</p> </header> <p>WWF's mission is to stop the degradation of our planet's natural environment, and build a future in which humans live in harmony with nature.</p> </article>
<footer> 元素為文檔或節(jié)規(guī)定頁腳。
<footer> 元素應(yīng)該提供有關(guān)其包含元素的信息。
頁腳通常包含文檔的作者、版權(quán)信息、使用條款鏈接、聯(lián)系信息等等。
您可以在一個文檔中使用多個 <footer> 元素。
<footer> <p>Posted by: Hege Refsnes</p> <p>Contact information: <a href="mailto:someone@example.com"> someone@example.com</a>.</p> </footer>
<nav> 元素定義導(dǎo)航鏈接集合。
<nav> 元素旨在定義大型的導(dǎo)航鏈接塊。不過,并非文檔中所有鏈接都應(yīng)該位于 <nav> 元素中!
<nav> <a href="/html/">HTML</a> | <a href="/css/">CSS</a> | <a href="/js/">JavaScript</a> | <a href="/jquery/">jQuery</a> </nav>
<aside> 元素頁面主內(nèi)容之外的某些內(nèi)容(比如側(cè)欄)。
aside 內(nèi)容應(yīng)該與周圍內(nèi)容相關(guān)。
<p>My family and I visited The Epcot center this summer.</p> <aside> <h4>Epcot Center</h4> <p>The Epcot Center is a theme park in Disney World, Florida.</p> </aside>
在書籍和報紙中,與圖片搭配的標題很常見。
標題(caption)的作用是為圖片添加可見的解釋。
通過 HTML5,圖片和標題能夠被組合在 <figure> 元素中:
<figure> <img src="pic_mountain.jpg" alt="The Pulpit Rock" width="304" height="228"> <figcaption>Fig1. - The Pulpit Pock, Norway.</figcaption> </figure>
<img> 元素定義圖像,<figcaption> 元素定義標題。
如果使用 HTML4 的話,開發(fā)者會使用他們喜愛的屬性名來設(shè)置頁面元素的樣式:
header, top, bottom, footer, menu, navigation, main, container, content, article, sidebar, topnav, ...
如此,瀏覽器便無法識別正確的網(wǎng)頁內(nèi)容。
而通過 HTML5 元素,比如:<header> <footer> <nav> <section> <article>,此問題迎刃而解。
根據(jù) W3C,語義網(wǎng):
“允許跨應(yīng)用程序、企業(yè)和團體對數(shù)據(jù)進行分享和重用。”
下面列出了以字母順序排列的 HTML5 新語義元素。
這些鏈接指向完整的 HTML 參考手冊。
標簽 | 描述 |
---|---|
<article> | 定義文章。 |
<aside> | 定義頁面內(nèi)容以外的內(nèi)容。 |
<details> | 定義用戶能夠查看或隱藏的額外細節(jié)。 |
<figcaption> | 定義 <figure> 元素的標題。 |
<figure> | 規(guī)定自包含內(nèi)容,比如圖示、圖表、照片、代碼清單等。 |
<footer> | 定義文檔或節(jié)的頁腳。 |
<header> | 規(guī)定文檔或節(jié)的頁眉。 |
<main> | 規(guī)定文檔的主內(nèi)容。 |
<mark> | 定義重要的或強調(diào)的文本。 |
<nav> | 定義導(dǎo)航鏈接。 |
<section> | 定義文檔中的節(jié)。 |
<summary> | 定義 <details> 元素的可見標題。 |
<time> | 定義日期/時間。 |
如對本文有疑問,請?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會為你解答!! 點擊進入論壇