简体   繁体   English

悬停另一个元素时更改SVG填充颜色

[英]Change SVG Fill Color on hover of another element

I've been researching for quite some time today on how to change an svg fill color on hover. 今天,我已经研究了很长一段时间,研究如何在悬停时更改svg填充颜色。 I've been able to make it work by using an object tag and linking to my CSS file so that I can style it externally. 通过使用对象标记并链接到我的CSS文件,我已经能够使其工作,以便可以在外部对其进行样式设置。 However I can only get this to work when I actually hover over the SVG itself. 但是,只有当我将鼠标悬停在SVG本身上时,我才能使此功能正常工作。 I have my object and a text label within an tag and would like to change the fill of the SVG and the color of the text label at the same time while hovering the . 我的标签中有我的对象和文本标签,并且想在悬停的同时更改SVG的填充和文本标签的颜色。 When I try targeting further up from the object it doesn't work. 当我尝试从该对象进一步向上定位时,它不起作用。 I believe I read that an SVG inside an object won't recognize any parent elements so I'm not sure if that's the problem. 我相信我读到对象内部的SVG无法识别任何父元素,因此我不确定这是否是问题。 Any help would be appreciated! 任何帮助,将不胜感激! Below is a snippet of my html. 以下是我的html的代码段。 I should point out the class "icon-md" is only defining the height and width of my SVG. 我应该指出,“ icon-md”类仅定义SVG的高度和宽度。

<div class="icon-button">
            <a href="#">
                <object type="image/svg+xml" class="icon-md" data="img/load-board.svg"></object><h3 class="icon-label">Load Board</h3>
            </a>
        </div>

Here is also the HTML of my SVG file. 这也是我的SVG文件的HTML。 I gave the actual path an id of "path" and that is what I am specifically targeting in my external CSS file. 我为实际路径指定了“ path”的ID,这就是我在外部CSS文件中专门针对的目标。

<?xml-stylesheet type="text/css" href="../stylesheets/main.css"?>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width="64" height="64">
<path id="path" style="text-indent:0;text-align:start;line-height:normal;text-transform:none;block-progression:tb;-inkscape-font-specification:Bitstream Vera Sans" d="M 5 5 L 5 6 L 5 26 L 5 27 L 6 27 L 26 27 L 27 27 L 27 26 L 27 6 L 27 5 L 26 5 L 6 5 L 5 5 z M 7 7 L 25 7 L 25 25 L 7 25 L 7 7 z M 10 10 L 10 15 L 15 15 L 15 10 L 10 10 z M 17 10 L 17 15 L 22 15 L 22 10 L 17 10 z M 10 17 L 10 22 L 15 22 L 15 17 L 10 17 z M 17 17 L 17 22 L 22 22 L 22 17 L 17 17 z" overflow="visible" font-family="Bitstream Vera Sans"/>
</svg>

Were you thinking of something like that ? 您是否在想类似的事情?

 div{ padding:10px; border:1px solid black; } div:hover svg path, div:hover{ fill:red; color:red; } 
 <div> This is plain text <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width="64" height="64"> <path id="path" style="text-indent:0;text-align:start;line-height:normal;text-transform:none;block-progression:tb;-inkscape-font-specification:Bitstream Vera Sans" d="M 5 5 L 5 6 L 5 26 L 5 27 L 6 27 L 26 27 L 27 27 L 27 26 L 27 6 L 27 5 L 26 5 L 6 5 L 5 5 z M 7 7 L 25 7 L 25 25 L 7 25 L 7 7 z M 10 10 L 10 15 L 15 15 L 15 10 L 10 10 z M 17 10 L 17 15 L 22 15 L 22 10 L 17 10 z M 10 17 L 10 22 L 15 22 L 15 17 L 10 17 z M 17 17 L 17 22 L 22 22 L 22 17 L 17 17 z" overflow="visible" font-family="Bitstream Vera Sans"/> </svg> </div> 

Have you tried to use the SVG inline, instead of having it as a source or data attribute on an element? 您是否尝试过内联使用SVG,而不是将其用作元素的源或数据属性?

EDITED with updated article: https://css-tricks.com/svg-use-with-external-reference-take-2/ 使用更新的文章进行编辑: https//css-tricks.com/svg-use-with-external-reference-take-2/

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

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