DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML5教程
用HTML5規範徹底分清 i、em、b、strong 的差別
編輯:HTML5教程     

HTML5 為了強化語義化,一口氣新增了大量標籤。而在所有容易混淆的標籤裡,i、em 與 b、strong 這兩組絕對排得上號。如果不認真翻一翻規範原文,還真不容易講清它們的界線。這道題也是前端面試的常客。本文直接回到最權威的源頭——W3C 的 HTML 規範文檔,把這四個元素逐一拆開來看。

先看直觀差異

如果不額外寫 CSS,單看瀏覽器的默認渲染:i 與 em 包裹的文字會以斜體呈現,b 與 strong 包裹的文字則會加粗。

渲染結果對比如下:

b 和 i 誕生之初,職責非常單純,就是表達「粗體」和「斜體」這兩種排版樣式。但進入 HTML5 時代,為了服務語義化,這兩個標籤也被賦予了明確含義,樣式反而變成了次要的事——畢竟外觀是 CSS 的工作。不過這四個標籤的語義各有側重,下面依序對照規範說明。

i 元素

規範原文:

The i element represents a span of text in an alternate voice or mood, or otherwise offset from the normal prose in a manner indicating a different quality of text, such as a taxonomic designation, a technical term, an idiomatic phrase from another language, transliteration, a thought, or a ship name in Western texts.

參考翻譯:i 元素表示一段語氣或語態與正文不同的文字,或者在某種程度上暗示這段文字具有別樣的性質,例如生物分類學名稱、專業術語、外來語習語、音譯詞、內心的想法,以及西方文本中船隻的名字。

示例:

// 分類學名稱
<p>The <i class="taxonomy">Felis silvestris catus</i> is cute.</p>

// 專業術語
<p>The term <i>prose content</i> is defined above.</p>

// 外來語習語
<p>There is a certain <i lang="fr">je ne sais quoi</i> in the air.</p>

實踐中建議為 i 標籤加上 class,用來表明使用它的意圖。這樣一來,日後若要調整這類文字的特殊樣式,就不必翻遍整份文檔去逐處排查。另外,用 i 之前不妨先想想是否有更合適的選擇:需要強調語氣應該用 em,需要標註術語定義則可以考慮 dfn。

em 元素

規範原文:

The em element represents stress emphasis of its contents.

The placement of stress emphasis changes the meaning of the sentence. The element thus forms an integral part of the content. The precise way in which stress is used in this way depends on the language.

參考翻譯:em 元素表示對其內容的強調。強調位置的不同會改變整句話的意思,因此這個元素是內容不可分割的一部分。具體如何通過重讀來傳達語氣,則取決於所用語言。

示例(類似中文裡重讀某個詞就換了意思):

// 一組完全沒有強調的陳述
<p>Cats are cute animals.</p>

// em 包住 Cats:強調可愛的是貓,而不是狗或其他動物
<p><em>Cats</em> are cute animals.</p>

// em 包住 are:強調這是不爭的事實,用來反駁那些說貓不可愛的人
<p>Cats <em>are</em> cute animals.</p>

// em 包住 cute:強調貓是「可愛」的,而非有人說的那樣刻薄討厭
<p>Cats are <em>cute</em> animals.</p>

// em 包住 animals:強調貓是動物,不是植物
<p>Cats are cute <em>animals</em>.</p>

規範末尾的註記:

The em element isn’t a generic "italics" element. Sometimes, text is intended to stand out from the rest of the paragraph, as if it was in a different mood or voice. For this, the i element is more appropriate.

The em element also isn’t intended to convey importance; for that purpose, the strong element is more appropriate.

參考翻譯:em 不是一個通用的「斜體」標籤。如果某段文字只是因為語態或語氣不同而需要從段落其餘部分中跳出來,用 i 更合適。em 也不是用來表達重要性的,那個場景應該交給 strong。

b 元素

規範原文:

The b element represents a span of text to which attention is being drawn for utilitarian purposes without conveying any extra importance and with no implication of an alternate voice or mood, such as key words in a document abstract, product names in a review, actionable words in interactive text-driven software, or an article lede.

參考翻譯:b 元素表示出於實用目的而引人注意的一段文字,它既不傳達額外的重要性,也不暗示不同的語態或語氣。典型場景包括摘要中的關鍵詞、評論裡的產品名、文本驅動型軟件中的可執行詞,以及文章的導語。

示例:

// 突出關鍵詞,但不強調重要性
<p>The <b>frobonitor</b> and <b>barbinator</b> components are fried.</p>

// 讓被包裹的詞在文字冒險場景中凸顯出來
<p>You enter a small room. Your <b>sword</b> glows brighter. A <b>rat</b> scurries past the corner wall.</p>

// 標註文章導語
<article>
  <h2>Kittens 'adopted' by pet rabbit</h2>
  <p><b class="lede">Six abandoned kittens have found an unexpected new mother figure — a pet rabbit.</b></p>
  <p>Veterinary nurse Melanie Humble took the three-week-old kittens to her Aberdeen home.</p>
  ...
</article>

規範建議:

The b element should be used as a last resort when no other element is more appropriate. In particular, headings should use the h1 to h6 elements, stress emphasis should use the em element, importance should be denoted with the strong element, and text marked or highlighted should use the mark element.

換句話說,b 是「實在沒有更合適的元素時」的最後選擇。標題該用 h1 到 h6,語氣強調該用 em,重要性該用 strong,被標記或高亮的文字該用 mark。

strong 元素

規範原文:

The strong element represents strong importance, seriousness, or urgency for its contents.

參考翻譯:strong 元素表示其內容具有強烈的重要性、嚴重性或緊急性。

場景一:重要性

可以用在標題、說明或段落中,用來標示被包裹文字的重要程度。

// 章節編號並不重要,章節名稱才重要
<h1>Chapter 1: <strong>The Praxis</strong></h1>

場景二:嚴重性

適合用來標記警告與警示。

<p><strong>Warning.</strong> This dungeon is dangerous.</p>

場景三:緊急性

適合用來標示需要被優先看到、盡快處理的內容。

<p>Welcome to Remy, the reminder system.</p>
<p>Your tasks for today:</p>
<ul>
  <li><p><strong>Turn off the oven.</strong></p></li>
  <li><p>Put out the trash.</p></li>
  <li><p>Do the laundry.</p></li>
</ul>

一句話記住四兄弟

對照規範,em 和 strong 的適用場景相當清晰,b 和 i 則稍微晦澀一些。我的理解是:

  1. em 會改變句子的含義指向,就像朗讀時重讀不同的字,整句話的意思就變了。
  2. strong 用來突出內容的重要性、嚴重性與緊急性。
  3. i 用來標示語態或性質不同的文字,有點像傳統排版裡那些用斜體處理的內容:技術術語、外來語習語等等。
  4. b 主要用於讓關鍵詞、產品名一類的文字在視覺上被看見。

b 的語義界定本身就比較模糊,所以規範才把它放在「最後手段」的位置,實戰中確實不建議優先考慮。

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