简体   繁体   English

我可以使用 CSS 更改 svg 路径的填充颜色吗?

[英]Can I change the fill color of an svg path with CSS?

I have the following code:我有以下代码:

 <span> <svg height="32" version="1.1" width="32" xmlns="http://www.w3.org/2000/svg" style="overflow: hidden; position: relative; left: -0.0166626px; top: -0.983337px;"> <desc></desc> <defs/> <path style="" fill="#333333" stroke="none" d="M15.985,5.972C8.422,5.972,2.289999999999999,10.049,2.289999999999999,15.078C2.289999999999999,17.955,4.302999999999999...............1,27.68,22.274Z"/> </svg>&nbsp; </span>

Is it possible to change the fill color of the SVG path with CSS or some other means without actually changing it inside the path tag?是否可以使用 CSS 或其他方式更改 SVG 路径的填充颜色,而无需在路径标签内实际更改它?

Yes, you can apply CSS to SVG, but you need to match the element, just as when styling HTML.是的,您可以将 CSS 应用于 SVG,但您需要匹配元素,就像设置 HTML 时一样。 If you just want to apply it to all SVG paths, you could use, for example:如果您只想将其应用于所有 SVG 路径,您可以使用,例如:

 ​path { fill: blue; }​

External CSS appears to override the path's fill attribute, at least in WebKit and Gecko-based browsers I tested.至少在我测试的基于 WebKit 和 Gecko 的浏览器中,外部 CSS 似乎覆盖了路径的fill属性。 Of course, if you write, say, <path style="fill: green"> then that will override external CSS as well.当然,如果你写,比如说, <path style="fill: green"> ,那么它也会覆盖外部 CSS。

if you want to change color by hovering in the element, try this:如果你想通过悬停在元素中来改变颜色,试试这个:

 path:hover{ fill:red; }

EDITED Apr-2021 2021 年 4 月编辑

If you go into the source code of an SVG file you can change the color fill by modifying the fill property.如果您将 go 放入 SVG 文件的源代码中,则可以通过修改填充属性来更改颜色填充。

 <svg fill="#3F6078" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> </svg>

Use your favorite text editor, open the SVG file and play around with it.使用您最喜欢的文本编辑器,打开 SVG 文件并使用它。

I figured out another way of changing the color from outside the SVG, and that is by importing the SVG content and removing the style rule where the fill is declared.我想出了另一种从 SVG 外部更改颜色的方法,即导入 SVG 内容并删除声明填充的样式规则。 And then controlling the fill from within my CSS style sheet.然后从我的 CSS 样式表中控制填充。

 <svg width="100%" height="100%" viewBox="0 0 167 134" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2;"><g id="Pic_icon"><path id="Pic_icon1" serif:id="Pic_icon" d="M166.667,0l-166.667,0l0,133.333l166.667,0l-0,-133.333Zm-158.276,70.89l32.771,-33.148l48.814,49.375l31.519,-19.575l36.781,32.176l-0,-91.3l-149.885,0l-0,62.472Zm84.426,-36.017c11.25,0 20.384,9.164 20.384,20.451c0,11.287 -9.134,20.451 -20.384,20.451c-11.25,0 -20.384,-9.164 -20.384,-20.451c0,-11.287 9.134,-20.451 20.384,-20.451Z" <:-- remove this -> style="fill;#08e1ea " -->/></g></svg>

Then inside my CSS file I go about:然后在我的 CSS 文件里面我 go 关于:

 svg{ fill: green; }

Now that You're removing the higher hierarchy fill declaration from the SVG the one outside takes control.既然您要从 SVG 中删除更高层次的填充声明,那么外部的一方就可以控制了。

This works for me with no problem at all.这对我来说完全没有问题。

you put this css for svg circle.你把这个 ZC7A62​​8CBA22E28EB17B5F5C6AE2A266AZ 放在 svg 圈子里。

 svg:hover circle{ fill: #F6831D; stroke-dashoffset: 0; stroke-dasharray: 700; stroke-width: 2; }

Put in all your svg:放入所有 svg:

 fill="var(--svgcolor)"

In Css:在 Css 中:

 :root { --svgcolor: tomato; }

To use pseudo-classes:要使用伪类:

 span.github:hover { --svgcolor:aquamarine; }

Explanation解释

root = html page.根 = html 页面。
--svgcolor = a variable. --svgcolor = 一个变量。
span.github = selecting a span element with a class github, a svg icon inside and assigning pseudo-class hover. span.github = selecting a span element with a class github, a svg icon inside and assigning pseudo-class hover.

I came across an amazing resource on css-tricks: https://css-tricks.com/using-svg/我遇到了一个关于 css-tricks 的惊人资源: https://css-tricks.com/using-svg/

There are a handful of solutions explained there.那里解释了一些解决方案。

I preferred the one that required minimal edits to the source svg, and also didn't require it to be embedded into the html document.我更喜欢需要对源 svg 进行最少编辑的那个,也不需要将它嵌入到 html 文档中。 This option utilizes the <object> tag.此选项使用<object>标签。


Add the svg file into your html using <object> ;使用<object>将 svg 文件添加到 html 中; I also declared html attributes width and height .我还声明了 html 属性widthheight Using these width and heights the svg document does not get scaled, I worked around that using a css transform: scale(...) statement for the svg tag in my associated svg css file. Using these width and heights the svg document does not get scaled, I worked around that using a ZC7A62​​8CBA22E28EB17B5F5C6AE2A266AZ transform: scale(...) statement for the svg tag in my associated svg ZC7A62​​8CBA22E28EB17B5F5C6AE2A266AZ file.

 <object type="image/svg+xml" data="myfile.svg" width="64" height="64"></object>

Create a css file to attach to your svn document.创建 ZC7A62​​8CBA22E28EB17B5F5C6AE2A266AZ 文件以附加到 svn 文档。 My source svg path was scaled to 16px, I upscaled it to 64 with a factor of four.我的源 svg 路径被缩放到 16 像素,我将它放大到 64,因子为 4。 It only had one path so I did not need to select it more specifically, however the path had a fill attribute so I had to use IMPORTANT to force the css to take precedent.它只有一条路径,所以我不需要更具体地说 select 它,但是路径有一个填充属性,所以我不得不使用IMPORTANT来强制 ZC7A62​​8CBA22E28EB17B5F5C6AE2A266AZ 采取先例。

 #svg2 { width: 64px; height: 64px; transform: scale(4); } path { fill: #333;IMPORTANT }

Edit your target svg file, before the opening <svg tag, to include a stylesheet;编辑您的目标 svg 文件,在开始<svg标记之前,包含一个样式表; Note that the href is relative to the svg file url.请注意,href 与 svg 文件 url 相关。

 <?xml-stylesheet type="text/css" href="myfile.css"?>

It's possible to change the path fill color of the svg.可以更改 svg 的路径填充颜色。 See below for the CSS snippet: CSS 片段见下文:

  1. To apply the color for all the path: svg > path{ fill: red }为所有路径应用颜色: svg > path{ fill: red }

  2. To apply for the first d path: svg > path:nth-of-type(1){ fill: green }申请第一个d路径: svg > path:nth-of-type(1){ fill: green }

  3. To apply for the second d path: svg > path:nth-of-type(2){ fill: green}申请第二个d路径: svg > path:nth-of-type(2){ fill: green}

  4. To apply for the different d path, change only the path number:申请不同的d路径,只改变路径号:
    svg > path:nth-of-type(${path_number}){ fill: green}

  5. To support the CSS in Angular 2 to 8, use the encapsulation concept:要支持 Angular 2 到 8 中的 CSS,请使用封装概念:

 :host::ng-deep svg path:nth-of-type(1){ fill:red; }

You can use this syntax but it will require some changes in the SVG file.您可以使用此语法,但需要对 SVG 文件进行一些更改。 And remove any fill/stroke from the SVG itself.并从 SVG 本身移除任何填充/描边。

icon.svg图标.svg

 <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1"> <,-- use symbol instead of defs and g. must add viewBox on symbol just copy yhe viewbox from the svg tag itself must add id on symbol --> <symbol id="location" viewBox="0 0 430.114 430.114"> <,-- add all the icon's paths and shapes here --> <path d="M356.208.107.051c-1.531-5.738-4.64-11.852-6.94-17,205C321.746,23.704,261,611.0,213.055,0 C148,054.0,76.463,43.586,66.905.133,427v18.355c0,0.766,0.264,7.647,0.639.11,089c5.358,42.816,39.143,88.32,64.375.131,136 c27.146,45.873,55.314,90.999,83.221.136.106c17,208-29.436.34,354-59.259.51.17-87.933c4,583-8.415.9,903-16.825.14.491-24.857 c3,058-5.348.8,9-10.696.11.569-15.672c27,145-49.699.70,838-99.782.70.838-149.104v-20,262 C363.209,126.938,356.581,108.204,356.208.107,051z M214.245.199,193c-19.107.0-40.021-9.554-50.344-35.939 c-1.538-4.2-1.414-12.617-1.414-13.388v-11,852c0-33.636.28,56-48.932.53.406-48,932c30,588.0,54.245,24.472,54.245.55,06 C270.138,174.729,244.833,199.193,214.245 199 193z"/> </symbol>

icon.html图标.html

 <svg><use xlink:href="file_path/location.svg#location"></use></svg>

If you use a path, you can set the stroke color: I used for hover effect here:如果使用路径,可以设置描边颜色:我这里用于hover效果:

 svg:hover path { stroke: blue; }

You should be able to modify dimensions, color or other properties with:您应该能够通过以下方式修改尺寸、颜色或其他属性:

 :host::ng-deep svg {... }

this is a simple solution that works for me:这是一个对我有用的简单解决方案:

  • put the svg in a div tag with "id"将 svg 放在带有“id”的 div 标签中

then然后

#id-div svg g { fill: #3366FF; }

To change any SVGs color Add the SVG image using an tag.要更改任何 SVG 颜色,请使用标签添加 SVG 图像。

 <img src="dotted-arrow.svg" class="filter-green"/>

To filter to a specific color, use the following Codepen(Click Here to open codepen) to convert a hex color code to a CSS filter: For example, output for #00EE00 is要过滤到特定颜色,请使用以下 Codepen(单击此处打开 codepen)将十六进制颜色代码转换为 CSS 过滤器:例如,#00EE00 的 output 是

filter: invert(42%) sepia(93%) saturate(1352%) hue-rotate(87deg) brightness(119%) contrast(119%);

Add the CSS filter into this class.将 CSS 滤波器添加到此 class 中。

 .filter-green{ filter: invert(48%) sepia(79%) saturate(2476%) hue-rotate(86deg) brightness(118%) contrast(119%); }

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

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