简体   繁体   English

CSS 具有内联 styles 的伪类

[英]CSS Pseudo-classes with inline styles

Is it possible to have pseudo-classes using inline styles?是否可以使用内联 styles 来创建伪类?


Example:例子:

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

I know the above HTML won't work but is there something similar that will?我知道上面的 HTML 不起作用但是有类似的东西吗?

PS I know I should use an external style sheet, and I do. PS 我知道我应该使用外部样式表,我确实这样做了。 I was just curious if this could be done using inline styles.我只是好奇这是否可以使用内联 styles 来完成。

No, this is not possible.不,这是不可能的。 In documents that make use of CSS, an inline style attribute can only contain property declarations;在使用 CSS 的文档中,内联style属性只能包含属性声明; the same set of statements that appears in each ruleset in a stylesheet.出现在样式表的每个规则集中的同一组语句。 From the Style Attributes spec :样式属性规范

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 : style 属性的值必须匹配 CSS声明块内容的语法(不包括分隔大括号),其形式语法在CSS 核心语法的术语和约定中给出:

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

Neither selectors (including pseudo-elements), nor at-rules, nor any other CSS construct are allowed.选择器(包括伪元素)、at-rules 和任何其他 CSS 构造都是不允许的。

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.将内联样式视为应用于某些匿名超特定 ID 选择器的样式:这些样式仅适用于具有style属性的那个元素。 (They take precedence over an ID selector in a stylesheet too, if that element has that ID.) Technically it doesn't work like that; (如果该元素具有该 ID,它们也优先于样式表中的 ID 选择器。)从技术上讲,它不是那样工作的; 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).请注意,内联样式与规则集中的选择器参与相同的级联,并且在级联中具有最高优先级(尽管有!important )。 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.另请注意,样式属性规范的非常旧的修订版最初确实建议允许 this ,但是它被废弃了,大概是由于上面给出的原因,或者因为实现它不是一个可行的选择。

Not CSS, but inline:不是 CSS,而是内联:

<a href="#" 
   onmouseover = "this.style.textDecoration = 'none'"
   onmouseout  = "this.style.textDecoration = 'underline'">Hello</a>

See example →查看示例 →

Rather than needing inline you could use Internal CSS而不是需要内联,您可以使用内部 CSS

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

You could have:你可以有:

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

You could try https://hacss.io :你可以试试https://hacss.io

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

Demo 演示

或者你可以简单地在内联css中试试这个

<textarea style="::placeholder{color:white}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM