Selecting all links except hovered one CSS only

后端 未结 3 1773
轮回少年
轮回少年 2021-01-22 14:15

I\'m trying to make a CSS selector that matches all links except the hovered one. Naturally I though of using the ~ operator to catch around elements:



        
3条回答
  •  我在风中等你
    2021-01-22 14:55

    The following selector matches all links except a hovered link:

    a[href]:not(:hover)
    

    When no link is hovered, this matches all links, which logically satisfies the requirement.

    Note that a matches all a elements, including ... (outdated, but works) and ... (valid, mentioned in HTML5 specs). Using a[href] restricts us to a elements that have href attribute, i.e. to links.

    If you actually meant to ask for a selector that matches all links except a hovered link if there is a hovered link and no element otherwise, then there is no CSS solution (but there are JavaScript solutions).

提交回复
热议问题