CSS3:伪类前的冒号和两个冒号区别


  在一次 名目中,有一次要用到::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等 。

  那么现在就 可以 完全的 答复 题目中的问题了,关于CSS2之前已有的伪元素, 比方:before,单冒号和双冒号的写法::before作用是一样的 。

  所以,假如你的网站 惟独求兼容webkit、firefox、opera等阅读器, 提议关于伪元素采纳双冒号的写法,假如只能兼容IE阅读器,还是用CSS2的单冒号写法 比较安全 。