简体   繁体   English

div background-image在Firefox中悬停时变为空白?

[英]div background-image goes blank on hover in Firefox?

I have an element that should change background-image when someone hovers over it. 我有一个元素,当有人将鼠标悬停在上面时,它应该更改background-image However, the background images are SVGs as data-uri s. 但是,背景图像是作为data-uri的SVG。

It works perfectly on Webkit browsers (Chrome, Safari, Opera) and works well on IE. 它在Webkit浏览器(Chrome,Safari,Opera)上完美运行,并且在IE上运行良好。

However, on Firefox, whenever I hover, the image simply goes blank. 但是,在Firefox上,每当我将鼠标悬停时,图像都会变成空白。 When I try and fiddle with it by inspecting the element and maybe changing the color or some other unrelated the style, the image magically appears. 当我尝试通过检查元素并可能更改颜色或其他不相关的样式来摆弄它时,图像会神奇地出现。

What is going wrong here and how can I fix it? 这里出了什么问题,我该如何解决?

This is my CSS: 这是我的CSS:

.info .comment{
    background-image: url('data:image/svg+xml;charset=utf-8,<svg%20xmlns%3D"http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg"%20fill%3D"%23D9D9D9"%20width%3D"25px"%20height%3D"25px">%0D%0A%20<path%20d%3D"M11.5%201C5.159441000000001%201%200%205.54683583%200%2011.1341066C0%2013.8432911%201.2135882000000038%2016.305879%203.1855000000000047%2018.1266402C3.3234999999999957%2018.2536543%203.4046764999999937%2018.4293122%203.4046764999999937%2018.6164553L3.4046764999999937%2024.1449483C3.2423234999999977%2024.7442118%203.9045882000000063%2025.2259197%204.426147099999994%2024.8887917L8.889499999999998%2021.2411889C9.039676%2021.118904%209.233823999999998%2021.0702603%209.425264999999996%2021.1006627C10.098353000000003%2021.210111%2010.791735000000003%2021.2682132%2011.5%2021.2682132C17.841234999999998%2021.2682132%2023%2016.722053%2023%2011.1341066C23%205.54683583%2017.841234999999998%201%2011.5%201"%2F>%0D%0A%0D%0A%0D%0A<%2Fsvg>');
    background-repeat: no-repeat;
    background-size: contain;
    width: 20px;
    height: 20px;
    color: #D9D9D9;
    display: inline-block;
}

.info:hover .comment, .info:visited .comment{
    background-image: url('data:image/svg+xml;charset=utf-8,<svg%20xmlns%3D"http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg"%20fill%3D"%23CCCCCC"%20width%3D"25px"%20height%3D"25px">%0D%0A%20<path%20d%3D"M11.5%201C5.159441000000001%201%200%205.54683583%200%2011.1341066C0%2013.8432911%201.2135882000000038%2016.305879%203.1855000000000047%2018.1266402C3.3234999999999957%2018.2536543%203.4046764999999937%2018.4293122%203.4046764999999937%2018.6164553L3.4046764999999937%2024.1449483C3.2423234999999977%2024.7442118%203.9045882000000063%2025.2259197%204.426147099999994%2024.8887917L8.889499999999998%2021.2411889C9.039676%2021.118904%209.233823999999998%2021.0702603%209.425264999999996%2021.1006627C10.098353000000003%2021.210111%2010.791735000000003%2021.2682132%2011.5%2021.2682132C17.841234999999998%2021.2682132%2023%2016.722053%2023%2011.1341066C23%205.54683583%2017.841234999999998%201%2011.5%201"%2F>%0D%0A%0D%0A%0D%0A<%2Fsvg>');
}

.info:hover {
color:#CCCCCC
}

and my HTML: 和我的HTML:

<a class="info" href="#">
            <div class="comment" role="img"></div>
            500
            </a>

This happens because order matters when using any pseudo-class . 发生这种情况是因为使用任何pseudo-class时顺序都很重要。 Firefox honors the CSS spec on :visited whereas webkit based browsers will allow since they are more loose when it comes to the spec . Firefox尊重:visited上的CSS规范 ,而基于Webkit的浏览器将允许使用,因为它们在规范方面更为宽松。

Example in codepen: Codepen中的示例:

http://codepen.io/jonathan/pen/GpVXMN http://codepen.io/jonathan/pen/GpVXMN

Same example in jsfiddle: jsfiddle中的相同示例:

http://jsfiddle.net/98assuba/1/ http://jsfiddle.net/98assuba/1/

You need to make sure you use the pseudo-class :link when using :visited . 使用:visited时,您需要确保使用伪类:link

Since the spec dictates that :link must come before :visited . 由于规范规定:link必须位于:visited之前。 So since you are adding it to the same CSS rule as your :hover , then you must add it to the default state of the .info .comment rule. 因此,由于要将其添加到与:hover相同的CSS规则中,因此必须将其添加到.info .comment规则的默认状态。

So add the line .info:link .comment to the same CSS rule as .info .comment 因此,将行.info:link .comment添加到与.info .comment相同的CSS规则中

And move .info:visited .comment before .info:hover .comment 并将.info:visited .comment移到.info:visited .comment .info:hover .comment之前

.info:link .comment, /* add this line */
.info .comment{
     background-image: url('data:image/svg+xml;charset=utf-8,<svg%20xmlns%3D"http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg"%20fill%3D"%23D9D9D9"%20width%3D"25px"%20height%3D"25px">%0D%0A%20<path%20d%3D"M11.5%201C5.159441000000001%201%200%205.54683583%200%2011.1341066C0%2013.8432911%201.2135882000000038%2016.305879%203.1855000000000047%2018.1266402C3.3234999999999957%2018.2536543%203.4046764999999937%2018.4293122%203.4046764999999937%2018.6164553L3.4046764999999937%2024.1449483C3.2423234999999977%2024.7442118%203.9045882000000063%2025.2259197%204.426147099999994%2024.8887917L8.889499999999998%2021.2411889C9.039676%2021.118904%209.233823999999998%2021.0702603%209.425264999999996%2021.1006627C10.098353000000003%2021.210111%2010.791735000000003%2021.2682132%2011.5%2021.2682132C17.841234999999998%2021.2682132%2023%2016.722053%2023%2011.1341066C23%205.54683583%2017.841234999999998%201%2011.5%201"%2F>%0D%0A%0D%0A%0D%0A<%2Fsvg>');
     background-repeat: no-repeat;
     background-size: contain;
     width: 20px;
     height: 20px;
     color: #D9D9D9;
     display: inline-block;
}

.info:visited .comment, /* add this before :hover */
.info:hover .comment{
     background-image: url('data:image/svg+xml;charset=utf-8,<svg%20xmlns%3D"http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg"%20fill%3D"%23CCCCCC"%20width%3D"25px"%20height%3D"25px">%0D%0A%20<path%20d%3D"M11.5%201C5.159441000000001%201%200%205.54683583%200%2011.1341066C0%2013.8432911%201.2135882000000038%2016.305879%203.1855000000000047%2018.1266402C3.3234999999999957%2018.2536543%203.4046764999999937%2018.4293122%203.4046764999999937%2018.6164553L3.4046764999999937%2024.1449483C3.2423234999999977%2024.7442118%203.9045882000000063%2025.2259197%204.426147099999994%2024.8887917L8.889499999999998%2021.2411889C9.039676%2021.118904%209.233823999999998%2021.0702603%209.425264999999996%2021.1006627C10.098353000000003%2021.210111%2010.791735000000003%2021.2682132%2011.5%2021.2682132C17.841234999999998%2021.2682132%2023%2016.722053%2023%2011.1341066C23%205.54683583%2017.841234999999998%201%2011.5%201"%2F>%0D%0A%0D%0A%0D%0A<%2Fsvg>');
}

.info:hover {
     color:#CCCCCC
}

Taken from the Firefox MDN CSS reference docs for :visited : 取自Firefox MDN CSS参考文档:visited

https://developer.mozilla.org/en-US/docs/Web/CSS/:visited https://developer.mozilla.org/zh-CN/docs/Web/CSS/:visited

The :visited CSS pseudo-class lets you select only links that have been visited. :visited CSS伪类可让您仅选择已访问的链接。 This style may be overridden by any other link-related pseudo-classes, that is :link, :hover, and :active, appearing in subsequent rules. 此样式可以被出现在后续规则中的任何其他与链接相关的伪类(即:link,:hover和:active)覆盖。 In order to style appropriately links, you need to put the :visited rule after the :link rule but before the other ones, defined in the LVHA-order: :link — :visited — :hover — :active. 为了给链接设置适当的样式,您需要将:visited规则放在:link规则之后,再放在其他规则之前,在LVHA-order中定义::link-:visited-:hover —:active。

And the W3C Spec for psuedo-class :visited 以及psuedo级的W3C规范:visited

http://www.w3.org/TR/CSS2/selector.html#link-pseudo-classes http://www.w3.org/TR/CSS2/selector.html#link-pseudo-classes

So remember to always place the pseudo-class's in a specific order (even if on a different rule for the same element): 因此,请记住始终以特定顺序放置伪类(即使对于同一元素使用不同的规则):

LVHA order: :link — :visited — :hover — :active LVHA命令::link-:visited-:hover-:active

In other words you could remember the order like this: 换句话说,您可以记住这样的顺序:

L ord V ader's H andle formerly A nakin 大号 ORD V阿德之H andle前身 NAKIN

Once you use the proper order for pseudo-classes, the browser will render properly consistently. 对伪类使用正确的顺序后,浏览器将正确一致地呈现。

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

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