DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
CSS3 中偽類單冒號與偽元素雙冒號的區別
編輯:CSS詳解     

核心提示:某次專案裡要用到 ::selection 偽元素,開發同事便問我:CSS 中一個冒號和兩個冒號到底有什麼區別?

這還真不是個小問題,或許不少前端同學都有過同樣的疑惑。查閱資料並驗證後,結論很簡潔:單冒號(:)用於 CSS3 偽類,雙冒號(::)用於 CSS3 偽元素。

W3C 關於 CSS3 選擇器的規範中有這麼一段描述:

A pseudo-element is made of two colons (::) followed by the name of the pseudo-element. This :: notation is introduced by the current document in order to establish a discrimination between pseudo-classes and pseudo-elements. For compatibility with existing style sheets, user agents must also accept the previous one-colon notation for pseudo-elements introduced in CSS levels 1 and 2 (namely, :first-line, :first-letter, :before and :after). This compatibility is not allowed for the new pseudo-elements introduced in CSS level 3.

簡單翻譯一下:偽元素由雙冒號加上偽元素名稱組成。雙冒號是當前規範引入的寫法,目的是區分偽類與偽元素。但為了兼容既有樣式,瀏覽器仍需支持舊的單冒號寫法,比如 :first-line、:first-letter、:before、:after 等;而 CSS3 新增的偽元素則不允許這種兼容。

於是我們就能完整回答標題中的問題了:對於 CSS2 之前就已存在的偽元素(如 :before),單冒號與雙冒號的寫法效果是一樣的。

所以,如果你的網站只需兼容 WebKit、Firefox、Opera 等瀏覽器,建議對偽元素採用雙冒號寫法;若不得不兼容 IE,那麼沿用 CSS2 的單冒號寫法會更保險。

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