具有内联样式的 CSS 伪类 [英] CSS Pseudo-classes with inline styles

查看:40
本文介绍了具有内联样式的 CSS 伪类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以有使用内联样式的伪类?

Is it possible to have pseudo-classes using inline styles?

例子:

<a href="http://www.google.com" style="hover:text-decoration:none;">Google</a>

我知道上面的 HTML 不起作用,但有类似的东西吗?

I know the above HTML won't work but is there something similar that will?

附:我知道我应该使用外部样式表,我确实这样做了.我只是好奇这是否可以使用内联样式来完成.

P.S. I know I should use an external style sheet, and I do. I was just curious if this could be done using inline styles.

推荐答案

不,这是不可能的.在使用 CSS 的文档中,内联 style 属性只能包含属性声明;出现在样式表的每个规则集中的同一组语句.来自 样式属性规范:

No, this is not possible. In documents that make use of CSS, an inline style attribute can only contain property declarations; the same set of statements that appears in each ruleset in a stylesheet. From the Style Attributes spec:

style 属性的值必须与 CSS 内容的语法相匹配 声明块(不包括分隔大括号),其形式语法在下面的CSS 核心语法:

The value of the style attribute must match the syntax of the contents of a CSS declaration block (excluding the delimiting braces), whose formal grammar is given below in the terms and conventions of the CSS core grammar:

declaration-list
  : S* declaration? [ ';' S* declaration? ]*
  ;

不允许使用选择器(包括伪元素)、at-rules 或任何其他 CSS 构造.

Neither selectors (including pseudo-elements), nor at-rules, nor any other CSS construct are allowed.

将内联样式视为应用于某些匿名超级特定 ID 选择器的样式:这些样式仅适用于具有 style 属性的那个元素.(如果该元素具有该 ID,它们也优先于样式表中的 ID 选择器.)从技术上讲,它不是那样工作的;这只是为了帮助您理解为什么该属性不支持伪类或伪元素样式(它与伪类和伪元素如何提供无法表达的文档树的抽象有关文档语言).

Think of inline styles as the styles applied to some anonymous super-specific ID selector: those styles only apply to that one very element with the style attribute. (They take precedence over an ID selector in a stylesheet too, if that element has that ID.) Technically it doesn't work like that; this is just to help you understand why the attribute doesn't support pseudo-class or pseudo-element styles (it has more to do with how pseudo-classes and pseudo-elements provide abstractions of the document tree that can't be expressed in the document language).

请注意,内联样式与规则集中的选择器参与相同的级联,并且在级联中具有最高优先级(尽管有 !important).因此它们甚至优先于伪类状态.在内联样式中允许伪类或任何其他选择器可能会引入一个新的级联级别,并随之带来一组新的复杂性.

Note that inline styles participate in the same cascade as selectors in rule sets, and take highest precedence in the cascade (!important notwithstanding). So they take precedence even over pseudo-class states. Allowing pseudo-classes or any other selectors in inline styles would possibly introduce a new cascade level, and with it a new set of complications.

另请注意,样式属性规范的非常旧的修订版最初建议允许这样做,然而它被废弃了,大概是因为上面给出的原因,或者因为实施它不是一个可行的选择.

Note also that very old revisions of the Style Attributes spec did originally propose allowing this, however it was scrapped, presumably for the reason given above, or because implementing it was not a viable option.

这篇关于具有内联样式的 CSS 伪类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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