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

来源:网络时间:2011-04-22 13:01:57
  在一次项目中,有一次要用到::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的单冒号写法比较安全
文章内容来源于网络,不代表本站立场,若侵犯到您的权益,可联系我们删除。(本站为非盈利性质网站) 联系邮箱:rjfawu@163.com
多特网友 2013-03-14 08:47:26 回复
不知道谁复制谁的,怎么网上的都是这个答案,有没有不一样的??有没有真正自己理解的写出来
多特网友 2012-09-17 14:32:52 回复
没有事例演示,说的不够清楚
多特网友 2012-09-17 14:32:52 回复
没有事例演示,说的不够清楚
多特网友 2013-03-14 08:47:26 回复
不知道谁复制谁的,怎么网上的都是这个答案,有没有不一样的??有没有真正自己理解的写出来