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

查看:535
本文介绍了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?

PS我知道我应该使用外部样式表,我做。我只是好奇,如果这可以使用内联样式。

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? ]*
  ;


选择器(包括伪元素) ,也不允许任何其他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).

请注意,内联样式与规则集中的选择器参与同级联,并且在级联中占据最高优先级(!重要)。因此,它们甚至优先于伪类状态。允许伪类或内联样式中的任何其他选择器可能会引入新的级联级别,并带来一组新的并发症。

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天全站免登陆