You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using SVG in CSS backgrounds allows you to use CSS's powerful background sizing and position properties. This makes sizing SVGs much simpler because the image easily scales to the size of your element. Plus you don't have SVG cluttering up your markup.
There are also some nice performance benefits over inline SVG. An SVG in a background image can be cached. Using image sprites and embedding SVG as a data URI can also improve performance.
内嵌 SVG 对性能优化也是有帮助的。它是可以被缓存的。并且通过雪碧图或者 data URI 内嵌 SVG 也是可以提升性能的。
CSS masks
This is my favorite method, but your browser support must be very progressive. With the mask property you create a mask that is applied to an element. Everywhere the mask is opaque, or solid, the underlying image shows through. Where it’s transparent, the underlying image is masked out, or hidden. The syntax for a CSS mask-image is similar to background-image.
Here I'm setting an SVG as the mask. The fill of the icon in the SVG doesn't matter because it masks the background layer which is the color red. Therefore, the result is a red icon. For webkit, I'm using the prefix.
There are a bunch of properties related to mask, such as mask-position, mask-repeat, and mask-size, which align with CSS background properties. You can use the mask shorthand like the background shorthand:
You can apply Photoshop-like effects to DOM elements with CSS filters. Filters can be chained, with each filter acting on the result of its predecessor.
In this example, the icon has a pure red fill set in the SVG. The hue-rotate filter rotates the hue 220 degrees around the rgb color wheel. This makes the icon blue. The algorithm for hue-rotation isn't extremely accurate, so although the output should be pure blue it’s a little off. One way to fix this and boost up the color to full saturation is to add a saturation filter. In the example, I added an arbitrarily large value of five to the chain, which increases the saturation by 500%.
By chaining filters together, you can make many colored icons from one colored icon input. With different combinations of hue-rotate, invert, brightness, and saturation filters, I've created the ROYGBIV rainbow spectrum and some other basic colors like cyan and magenta.
This technique isn't super intuitive. It will probably require some trial and error to find the colors you need, especially since the algorithms aren't perfect. In the future, it would be fantastic if CSS filters could also work in HSL space, as they would be much more intuitive.
CSS filters are supported well in most browsers. IE has them listed as "In Development", which means they are in active development and should be added to IE soon.
As Chris Coyier demonstrated in his article, Probably Don't Base-64 SVG, properly formatted, you can pop SVG XML right into your CSS. Using this technique, and a bit of Sass magic, I've created some functions that allow you to dynamically change the color.
With this setup, add the path coordinates of your SVG to a Sass map with a key and invoke the function with your parameters. You call the function with the icon's name, which corresponds to the key in the map. You can pass in arguments for color, stroke-color, and stroke-width. You can also do other fun stuff like pass in any SVG valid CSS, as I did with the stroke-dasharray in the last example.
Creating sprites for SVG uses the same concepts as normal image sprites. The sprite contains all of the versions of an image. By manipulating the background-size and background-position in your CSS, you choose which version to display. Here's how to set up an SVG sprite:
Use the xmnls namespace attribute or the SVG won't work in your background. Also include the xmlns:xlink attribute if using links, which I am in the use tags. The width and height need to be large enough to contain all of the images. In the example I'm using twenty-four 24x24 icons stacked vertically, so my SVG dimensions are 24x576 (24 times 24 = 576).
仅仅用 xmnls 命名空间,SVG 并不能正常工作。因为我用到了 use 标签,所以还需要添加 xmlns:xlink。宽、高要足够大用来容纳所有的图片。在示例中,有24个24 x 24的 icon 垂直排列,所以我的 SVG 尺寸是24 x 576(24 x 24 = 576)。
I'm using the symbol and use tags to keep the file size low. Define the icon shape as a symbol and place it in a defs block. Give it an id. Employ the use tag to stamp out versions of this symbol. In the use tag, the y coordinate is adjusted so the icons stack on top of each other at 24 unit increments. The fill attribute colors the icon. Here's a demo with a bunch of different colored icons defined using this technique.
我用标签 symbol 和 use 保证文件足够小。通过 symbol 定义了一个 icon 的图形并把它放在 defs 块中。设置一个 id,标签 use 通过 id 来引用它。在 use 中,可以通过属性 y 来控制显示哪个 icon。属性 fill 决定了 icon 的颜色。下面是一个利用这项技术实现的Demo。
I'm using Sass here to make the background positioning easier.
我用到了 Sass 这样处理 background-position 更加容易。
This technique works great everywhere SVG is supported. Because you need to manually create and add new icon versions to the sprite, it’s pretty inflexible.
Coloring SVGs in CSS Background Images
如何设置 CSS 背景图中的 SVG 的颜色
在
CSS
背景中我喜欢用SVG
,但是,这会导致你无法用CSS
很好改变它们的颜色。围绕这个问题这有几个方法。CSS
背景中的SVG
CSS
背景中的SVG
也可以通过background-size
和background-position
很好的控制。相对图片,SVG
更容易�控制大小。而且,SVG
标签并不会混入CSS
中。内嵌
SVG
对性能优化也是有帮助的。它是可以被缓存的。并且通过雪碧图或者data URI
内嵌SVG
也是可以提升性能的。CSS masks
这是我最喜欢的方法,但是需要浏览器的支持。通过
mask
属性创建一个遮罩层,应用于某些元素。通过控制它的透明度来控制被遮罩元素的显示隐藏。如果,遮罩是透明的,被盖住的元素就会显示,否则会隐藏。mask-image
的语法类似background-image
。我用
SVG
设置了一个遮罩。无需关心SVG
是什么颜色,最终的颜色是由background
决定的。以上代码,最终结果是红色的。为了兼容webkit
,我用了前缀。你的背景可以是任何你想设置的,比如:纯色、图片、渐变。
Demo1
有一些列属性是和
mask
相关的,比如:mask-position
、mask-repeat
和mask-size
。这些属性和background
属性是一样的,你可以使用类似background
的简写语法。mask
得到了大部分浏览器的支持。IE 的团队也在考虑中。Firefox 当前是不支持的外部SVG
的。(译者注:随着时间的推移对
mask
的支持情况会有改善,最终以具体浏览器实现为准,也可以关注 Caniuse)CSS filters
像 Photoshop 那样在
CSS
中也可以使用filter
。filter
的值是链式的,滤镜的效果是跟具体的值的前后顺序有关的。(译者注:这一点和
transform
一样)在这个示例中,
SVG
是纯红色的。filter: hue-rotate(220deg)
代表:色调在RGB色盘上转旋220度。这样,icon
就变成了蓝色。hue-rotate
的算法并不是那么准确,所以输出的蓝色是有色差的。通过设置saturation
属性调整颜色的饱和度是可以修复的。在示例中,我添加了一个足大的值,它可以提到5倍的饱和度。通过组合不同的
filter
属性值,你可以变换不同颜色的icon
。通过组合hue-rotate
、invert
、brightness
和saturation
,我创建了一个彩虹光谱:ROYGBIV 和一些基本颜色,比如:青色和洋红。(译者注:ROYGBIV 下面 Demo 中第一行色值的简称。)
Demo2
创建灰色的过滤器非常简单。你只需添加一个
grayscale
属性,并通过brightness
调整亮度。这个技巧并不是那么直观。直到你经过不断尝试过后,才能确定你需要的颜色,尤其是算法也不是那么完美。将来,如果
CSS filter
也能支持 HSL,那就完美了。大多数浏览器都支持
CSS filter
。IE也在考虑之中。(译者注:具体支持情况,请以最新为准)
Data URI
正如 Chris Coyier 博客中说的,千万不要把 SVG Base64 格式化,你只需要把 SVG 代码片段正确的放入 CSS 中即可。利用这项技术配合 Sass ,我创建一个 Sass 函数,可以动态的改变
icon
的颜色。Demo3
通过这个方法,你可以在
Sass
中添加SVG
,并设置一个key
然后传入参数调用方法。你可以通过icon
的名字调用函数,这个名字是和key
对应的。在参数中你可以传入color
、stroke-color
和stroke-width
。你还可以做一些有趣的处理,比如:我在最后一个示例中就用到了stroke-dasharray
。通过正确的 URL 编码,IE中也是可以正常运行的。显然,想正确使用相当的复杂。
SVG Background Sprite
创建 SVG 雪碧图和图片雪碧图一样。一张图片包含了所有的
icon
。通过控制background-size
和background-position
,来决定显示那一个。以下是设置 SVG 雪碧图的方法。Demo4
仅仅用
xmnls
命名空间,SVG
并不能正常工作。因为我用到了use
标签,所以还需要添加xmlns:xlink
。宽、高要足够大用来容纳所有的图片。在示例中,有24个24 x 24的icon
垂直排列,所以我的SVG
尺寸是24 x 576(24 x 24 = 576)。我用标签
symbol
和use
保证文件足够小。通过symbol
定义了一个icon
的图形并把它放在defs
块中。设置一个id
,标签use
通过id
来引用它。在use
中,可以通过属性y
来控制显示哪个icon
。属性fill
决定了icon
的颜色。下面是一个利用这项技术实现的Demo。Demo5
我用到了
Sass
这样处理background-position
更加容易。任何支持
SVG
的地方,这项技术都能很好的工作。因为你需要手动的管理所有的icon
,这就导致非常的不灵活。这就是我能想到的所有方法。你或许还有更好的方法。
The text was updated successfully, but these errors were encountered: