萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> XML 程式設計

HTML5 學習筆記(二):語義新元素 section、article、aside

<section> 元素

<section> 表示文檔或應用中的一個通用區塊,通常包含標題(<h1>-<h6>)與內容。可用於章節、標籤頁面板等。一個頁面可分為多個 section(如簡介、新聞、聯絡資訊)。

注意:若內容本身已構成獨立完整語義,應優先用 <article>;<section> 非通用容器,僅作為文檔大綱的明確節點,需樣式/腳本控制時仍建議用 <div>。

範例:蘋果官網片段

<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>

在 section 內可直接使用 <h1>,無需考慮階層深度。

範例:畢業典禮頁面

<!DOCTYPE Html>
<html>
<head>
 <title>Graduation Ceremony Summer 2022</title>
</head>
<body>
 <h1>Graduation</h1>
 <section>
 <h1>Ceremony</h1>
 <p>Opening Procession</p>
 <p>Speech by Validactorian</p>
 <p>Speech by Class President</p>
 <p>Presentation of Diplomas</p>
 <p>Closing Speech by Headmaster</p>
 </section>
 <section>
 <h1>Graduates</h1>
 <ul>
 <li>Molly Carpenter</li>
 <li>Anastasia Luccio</li>
 <li>Ebenezar McCoy</li>
 <li>Karrin Murphy</li>
 <li>Thomas Raith</li>
 <li>Susan Rodriguez</li>
 </ul>
 </section>
</body>
</html>

<article> 元素

<article> 代表文檔中獨立、完整的內容塊(如部落格文章、新聞報導),可獨立發布或重用。它是特殊的 <section>,語義更明確:通常包含標題(常置於 <header>)、內文,有時含 <footer>。巢狀 <article> 表示內容相關(如文章內的評論)。

<article>
<a href="https://www.apple.com">Safari 5 released</a><br />
7 Jun 2010. Just after the announcement of the new iPhone 4 at WWDC,
Apple announced the release of Safari 5 for Windows and Mac......
</article>

<aside> 元素

<aside> 表示頁面或文章的附屬資訊:引用、側邊欄、廣告、導航群組等。兩種用法:

  • 內嵌於 <article>:作為該文章的附屬資訊(如詞彙表、引用)。
  • 位於 <article> 之外:作為頁面/站點全域附屬資訊,典型為側邊欄(友情連結、廣告等)。

綜合範例

<body>
 <header>
 <h1>My Blog</h1>
 </header>
 <article>
 <h1>My Blog Post</h1>
 <p>Lorem ipsum dolor sit amet...</p>
 <aside>
 <h1>Glossary</h1>
 <dl>
 <dt>Lorem</dt>
 <dd>ipsum dolor sit amet</dd>
 </dl>
 </aside>
 </article>
 <aside>
 <h2>Blogroll</h2>
 <ul>
 <li><a href="#">My Friend</a></li>
 <li><a href="#">My Other Friend</a></li>
 <li><a href="#">My Best Friend</a></li>
 </ul>
 </aside>
</body>
copyright © 萬盛學電腦網 all rights reserved