指定 SVG 作为背景图像并在 CSS 中设置 SVG 样式?
是否可以指定 SVG 作为背景图像,并在同一个 CSS 文件中设置 SVG 样式?
我非常习惯将 SVG 图像放置、缩放和剪切为单个文件或精灵,但我无法确定是否可以在将其设置为背景图像的同一个 CSS 文件中设置 SVG 样式。
在伪CSS中,我想执行以下操作来根据父元素的类改变简单形状的颜色:
element1 {
background-image(icon.svg);
}
element1.black .svg-pathclass {
fill: #000000;
}
element1.white .svg-pathclass {
fill: #ffffff;
}
显然,这假设SVG中的路径具有类.svg-pathclass,
这可能吗?
Is it possible to specify an SVG as a background image and ALSO style the SVG in the same CSS file?
I'm very comfortable placing, scaling and clipping SVG images as either single files or sprites, but I haven't been able to work out if it possible to style the SVG within the same CSS file as sets it as a background image.
In pseudo CSS i'd like to do the following to vary the colour of simple shape based on the class of the parent element:
element1 {
background-image(icon.svg);
}
element1.black .svg-pathclass {
fill: #000000;
}
element1.white .svg-pathclass {
fill: #ffffff;
}
obviously this assumes a path in the SVG having class .svg-pathclass
is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不,这是不可能的。 SVG 必须在一个文档中准备(可以是数据 URI 或外部引用的文件)然后然后在另一个文件中用作背景。
No, this is not possible. The SVG has to be prepared in one document (which may be a data URI or an externally referenced file) and then used as a background in another file.
实际上,对于那些可以在生产中使用预处理器的人来说,通过“内联”背景 SVG 和一些 SASS
mixins
(“切片”整个svg
乱码)来获得访问权限是可能的。到您想要通过 SASS 变量操作的部分。在您最初的场景中,您有一个元素
,
因此您需要一个
mixin/function
来为您内联 SVG。假设您想要控制fill
,因此:其中
$svg-content
变量是您的内容,不包括
元素(您希望从
mixin
内部访问该元素)并包装svg
标记,即:$svg-content = " "
这只需包含在内部传递的值中即可:
@include inline-svg(salmon, $svg-content);
总而言之,这是一个示例 SASS 代码:
我认为这里的可能性相当大(有也是该方法的局限性)。实际上,我将
SASS 映射
传递给我的mixin
,其中css
样式定义为key
、value 对,将整堆
css
样式注入到svg
的部分。
因此,这在技术上是可行的,但需要更多的复杂性,但是一旦完成此操作,您将获得在整个项目中重用此
mixin
的好处,这很酷。It's actually possible for those who can use preprocessors in production, by "inlining" background SVG, and bit of SASS
mixins
, which "slice" wholesvg
gibberish, to get access to parts you want to manipulate viaSASS variables
.In your original scenario you have an element
<div class="element1"></div>
,so you need a
mixin/function
which will inline SVG for you. Let's say you want to control of thefill
, so:where
$svg-content
variable is your<svg>
stuff excluding<style>
element (which you want to access from inside of themixin
) and wrappingsvg
tag,, ie:$svg-content = "<path .... />"
This just need to be included with values passed inside:
@include inline-svg(salmon, $svg-content);
To sum whole thing up, this is an example SASS code:
I think possibilities here are quite big (there are also limitations here to that approach). I actually pass a
SASS map
to mymixin
withcss
styles defined askey
,value
pair, to inject whole bunch ofcss
styles to the<style>
part ofsvg
.So it's technically possible, but require more compexity, but once you get this done, you'll get benefits of reusing this
mixin
throughout your project(s), which is cool .您可以使用
SVG
和CSS
遮罩重新创建此效果,然后使用普通CSS
来设置SVG
内部的样式>,甚至是background-image
您可以通过将
element
附加到DOM
并使用较低的 z 对其进行样式化来使用它来创建阴影-索引并设置不透明度。希望有帮助!
编辑:链接
You can use
SVG
andCSS
masks to recreate this effect, then use normalCSS
to style the inside of theSVG
, evenbackground-image
You can use this to create drop shadows by appending an
element
to theDOM
and styling that, with a lower z-index and setting an opacity.Hope that helps!
Edit: Links
通过单独的 CSS 类设置 SVG 样式对我来说不起作用,但内联样式可以:
注意:将
fill
属性指定为十六进制代码颜色不起作用,即使它在 '正常的 SVG,这就是我将其转换为 RGB 并以这种方式设置样式的原因。Styling the SVG via a separate CSS class didn't work for me, but inline styling did:
Note: specifying the
fill
prop as a hexcode colour didn't work, even though it did if in a 'normal' SVG, that's why I converted it to RGB and styled it that way.阅读所有这些答案让我想到了在 SASS (.scss) 中处理 SVG 的好方法,它编译 CSS,用于我的样式主题,并且还可以使
color
正常工作。对于我的用例,我希望(需要)使用content: url(..svg)
因为我的库支持 Font,现在通过使用content: $my-icon;
支持 Font > 在伪:before
中,这是唯一适用于 Font/SVG 的东西。首先是创建一个SASS函数来处理颜色的
urlencode
(因为content: url()
需要编码,所以#
需要变为%23
)。作为参考,我从其他人那里复制了这个函数 Gist。例如,这将采用#fafafa
并返回%23fafafa
,然后我可以在 SVG 的fill
中使用它。您也可以不使用它,只需记住将fill
属性中的#
更改为%23
即可。我的库有一些为主题样式定义的 SASS 变量,所以这就是我使用 Font 和/或 SVG 所得到的结果(我添加了
width
和display
但它可能在没有也)。最后,最终用户仍然可以将其与 Font Family(如 Font Awesome 4)
或 SVG(如 Font Awesome 5)一起
使用。 SVG 使用 SASS 变量,这很棒,因为这意味着我的库的任何用户 (Angular-Slickgrid)仍然可以使用 Font Awesome 4 图标(字体),而其他人也可以使用 SVG(如 Font Awesome 5),而无需安装 Font Family (
.eof
,.woff
, ...) 往往会很大,具体取决于库。最终结果仍然是一个编译后的 CSS 文件,其中包含用于加载 SVG 的content
以及 SVG 颜色的额外fill
属性。瞧!我得到了两者最好的:)
Reading all of these answers made me come up with this nice way of dealing with SVG in SASS (.scss), which compiles CSS, for my styling theme and also get the
color
working. For my use case, I want (need) to usecontent: url(..svg)
since my lib supports Font and now SVG by usingcontent: $my-icon;
in a pseudo:before
that is the only thing that works with both Font/SVG.First is to create a SASS function to deal with
urlencode
of the color (sincecontent: url()
needs to be encoded,#
needs to become%23
). For reference, I copied this function from someone else Gist. For example this will take#fafafa
and return%23fafafa
that I can then use infill
of the SVG. You could also go without it, just remember to change#
to%23
in thefill
property.My lib has a few SASS variables defined for theme styling so this is what I got working with both Font and/or SVG (I added
width
anddisplay
but it might work without too).and finally the end user can still use it with a Font Family (like Font Awesome 4)
or with SVG (like Font Awesome 5)
So at the end, I got both the Font & SVG working with SASS variables and that is great because that means that any users of my lib (Angular-Slickgrid) can still use Font Awesome 4 icons (font) while others could also use SVGs (like Font Awesome 5) without having to install the Font Family (
.eof
,.woff
, ...) which tend to be large depending on the lib. The end result is still a compiled CSS file withcontent
to load the SVG with an extrafill
property for the SVG color.and voilà!!! I get the best of both :)