简体   繁体   English

悬停在父级上不会影响标记标记CSS

[英]Hover on parent does not affect mark tag CSS

How can I apply hover style from a parent container, such as div or anchor, to a mark tag? 如何将父容器(如div或锚)中的悬停样式应用于标记标记? I know this is default behavior for other elements nested inside a parent like span. 我知道这是嵌套在父类中的其他元素的默认行为,如span。 However, it is not working for me when I use a mark tag. 但是,当我使用标记标记时,它对我不起作用。

Here are three examples, 1) div with mark, 2) anchor with mark, 3) anchor with span. 这里有三个例子,1)带有标记的div,2)带有标记的锚,3)带有跨度的锚。 If you hover the parent, but outside the child, it changes the span, but not the mark. 如果您将父项悬停在子项之外,则会更改范围,但不会更改标记。

<div>
  Outside div <mark>inside mark</mark>
</div>

<a>
  Outside anchor <mark>inside mark</mark>
</a>
<br>
<a>
  Outside anchor <span>inside span</span>
</a>

https://jsfiddle.net/ku6drqt5/ https://jsfiddle.net/ku6drqt5/

I would love to know what is special about the mark tag that is preventing this from working, and how I might correct it. 我很想知道阻止它工作的标记标记有什么特别之处,以及我如何纠正它。 Otherwise, simple workarounds are welcome. 否则,欢迎简单的解决方法。

Note: I am using Chrome v48. 注意:我使用的是Chrome v48。

It appears that the color has to be specifically set on a mark element as it's not directly inherited. 看来颜色必须专门设置在mark元素上,因为它不是直接继承的。

Per Joseph 's comment 按照约瑟夫的评论

...the default browser/user-agent color for the mark element is set as black ... mark元素的默认浏览器/用户代理color设置为black

Just as the default background color is yellow *. 正如默认背景颜色为yellow *。

* this appears be the agreed defaults for Chrome, Firefox and IE although I cannot locate any requirement for this to be the case. *这似乎是Chrome,Firefox和IE的约定默认值,但我找不到任何要求

So when you change the parent:hover color, the user agent styling is still more specific. 因此,当您更改父级:悬停颜色时,用户代理样式仍然更具体。 <span> does not have a color set, so it inherits the color specified on parent:hover <span>没有颜色集,因此它继承了parent:hover上指定的颜色

A workaround 解决方法

 .parent:hover { color: red; } .parent:hover mark { color: currentColor; /* or inherit as noted in the other answer */ } 
 <div class="parent"> Outside div <mark>inside mark</mark> </div> <a class="parent"> Outside anchor <mark>inside mark</mark> </a> <br> <a class="parent"> Outside anchor <span>inside span</span> </a> 


Depending on your requirement you might like to add this to your CSS reset. 根据您的要求,您可能希望将其添加到CSS重置中。

mark {
  color:inherit;
}

JSfiddle 的jsfiddle

This depends on how your user agent (browser) styles the <mark> element. 这取决于您的用户代理(浏览器)如何设置<mark>元素的样式。 Chrome has these defaults: Chrome有以下默认值:

background-color: yellow;
color: black;

The easiest way to reset this, is through adding this to your css: 重置此功能的最简单方法是将此添加到您的css:

.parent:hover mark {
    background-color: inherit;
    color: inherit;
}

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

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