在一次項目中,有一次要用到::selection僞元素,而後開發同窗問我,CSS中一個冒號和兩個冒號有神馬區別?css
這好像真的是個問題,或許不少前端同窗對此都有疑惑,查了些資料,證明了下兩個符號的區別,簡而言之:單冒號(:)用於CSS3僞類,雙冒號(::)用於CSS3僞元素。前端
W3C關於CSS3選擇器的規範中有一段描述: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等。web
那麼如今就能夠完整的回答標題中的問題了,對於CSS2以前已有的僞元素,好比:before,單冒號和雙冒號的寫法::before做用是同樣的。瀏覽器
因此,若是你的網站只須要兼容webkit、firefox、opera等瀏覽器,建議對於僞元素採用雙冒號的寫法,若是不得不兼容IE瀏覽器,仍是用CSS2的單冒號寫法比較安全。安全