CSS Pseudo-classes with inline styles

前端 未结 4 1229
日久生厌
日久生厌 2020-11-22 01:24

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


Example:



        
相关标签:
4条回答
  • 2020-11-22 02:00

    Rather than needing inline you could use Internal 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>
    
    0 讨论(0)
  • 2020-11-22 02:01

    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:

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

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

    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.

    0 讨论(0)
  • 2020-11-22 02:12

    Not CSS, but inline:

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

    See example →

    0 讨论(0)
  • 2020-11-22 02:17

    You could try https://hacss.io:

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

    Demo

    0 讨论(0)
提交回复
热议问题