:before和::before的区别
单冒号(:)用于CSS3伪类,双冒号(::)用于CSS3伪元素。
我们来看一下w3c上是怎么说的?
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 this specification.
翻译过来就是伪元素由双冒号和伪元素名称组成。双冒号是在当前规范中引入的,用于区分伪类和伪元素。不过浏览器需要同时支持旧的已经存在的伪元素写法,比如:first-line、:first-letter、:before、:after等,而新的在CSS3中引入的伪元素则不允许再支持旧的单冒号的写法。
那么现在就可以知道:before和::before的区别了。对于CSS2之前已有的伪元素,比如:before,单冒号和双冒号的写法::before作用是一样的。所以,如果你的网站只需要兼容webkit、firefox、opera等浏览器,建议对于伪元素采用双冒号的写法,如果不得不兼容IE浏览器,还是用CSS2的单冒号写法比较安全。