我应该使用单冒号(:)还是双冒号(::)用于前,后,第一个字母和第一行伪元素? [英] Should I use single colons (:) or double colons (::) for before, after, first-letter and first-line pseudo-elements?

查看:108
本文介绍了我应该使用单冒号(:)还是双冒号(::)用于前,后,第一个字母和第一行伪元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从MDN:


:: 符号在CSS 3以便在伪类和伪元素之间建立
区分。浏览器
也接受在CSS 2中引入的符号


如果符号将始终被CSS3浏览器接受,我应该使用它,因为它适用于旧的和新的浏览器?



或者我应该同时使用它们,用于旧浏览器, :: 不会总被接受?






strong>注意:我认为我的问题不是重复的,不是因为另一个问题询问所有伪元素的单个或双重符号;而我的问题只是关于在CSS2中定义的伪元素,而不是CSS3中定义的新元素,因为我已经知道,我必须使用 ::

对于值得的,选择器4现在明确要求 1 作者对所有伪元素使用双冒号,包括CSS1和CSS2伪元素,继续:


由于遗留约束,用户代理必须允许 :: before :: after :: first-line 字母伪元素用单个冒号而不是两个来写,类似于伪类语法。作者必须始终对这些伪元素使用双冒号语法。


这意味着如果你绝对需要传统的浏览器支持 - 唯一重要的浏览器是IE8和更旧版本,今天适当使用单冒号语法。如果不这样做,则为了与较新版本保持一致,应使用双冒号语法伪元素,其将仅接受双冒号。此外,使用单冒号语法是毫无意义的,例如,如果您要应用IE8 反正不支持的属性,例如 border- radius box-shadow :: before > :: after 伪元素。



我想相信选择器3至少 >这在其声明中的单冒号语法不适用于任何较新的伪元素,但是这种事情以黑色和白色表示从来没有伤害任何人,这是很好,知道即将到来的标准只是这样。 p>

此外,绝对没有理由一起使用这两个符号(例如:before,:after {...} :: before,:: after {...} ),因为没有浏览器支持新语法而不支持旧语法。






1 我说这完全知道它可能没有在问这个问题时提出这个问题 - 2013年5月WD 当然没有。


From MDN:

The :: notation was introduced in CSS 3 in order to establish a discrimination between pseudo-classes and pseudo-elements. Browsers also accept the notation : introduced in CSS 2.

If the notation : will always be accepted by CSS3 browsers, should I use it because it works on old and new browsers?

Or should I use both of them, : for old browsers and :: for new ones, because the notation : won't be always accepted?


Note: I think my question isn't a duplicate isn't a duplicate of Should I use single or double colon notation for pseudo-elements? because the other question asks about single vs double notation for ALL pseudo-elements; while my question is only about pseudo-elements defined in CSS2, not the new ones defined in CSS3, because I already know that with those I must use ::.

解决方案

For what it's worth, Selectors 4 now explicitly asks1 that authors use double colons for all pseudo-elements, including CSS1 and CSS2 pseudo-elements, going forward:

Due to legacy constraints, user agents must allow ::before, ::after, ::first-line, and ::first-letter pseudo-elements to be written with a single colon rather than two, similar to pseudo-class syntax. Authors must always use the double-colon syntax for these pseudo-elements.

This means that the only appropriate use of the single-colon syntax today is if you absolutely require legacy browser support — the only browser that matters here is IE8 and older. If you don't, you should use the double-colon syntax for the sake of consistency with newer pseudo-elements which will only accept double colons. Besides, it's quite pointless to use the single-colon syntax if, for instance, you're going to apply properties that aren't supported by IE8 anyway, such as border-radius or box-shadow, to your ::before and ::after pseudo-elements.

I'd like to believe that Selectors 3 at the very least implied this in its statement that the single-colon syntax does not apply to any newer pseudo-elements, but having this sort of thing stated in black and white never hurt anybody and it's good to know that the upcoming standard does just that.

Also, there is absolutely no reason to use both notations together (e.g. :before, :after { ... } ::before, ::after { ... }), as no browser in existence supports the new syntax without supporting the older one.


1 I say this fully aware that it probably didn't state this yet at the time this question was asked — the May 2013 WD certainly did not.

这篇关于我应该使用单冒号(:)还是双冒号(::)用于前,后,第一个字母和第一行伪元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆