rotate
, underline
or add text-shadow
.
symbol
tag and give it an id
.
<svg xmlns="http://www.w3.org/2000/svg" style="display: none"> <symbol id="my-first-icon" viewBox="0 0 20 20"> <title>my-first-icon</title> <path d="..." /> </symbol> </svg>
use
element.
<svg> <use xlink:href="#my-first-icon" /> </svg>
xlink:href
attribute - this is the link between your icon and the original SVG image.
xlink:href
is an obsolete SVG attribute. Even if most browsers still support it, you should use href
instead. But the fact is that some browsers, such as Safari, do not support links to SVG resources through the href
attribute, so you still need to specify xlink:href
.
color
property does not affect SVG icons: you must use the fill
attribute to specify a color. This means that they do not inherit the parent color of the text, but you can still stylize them through CSS.
<svg class="icon"> <use xlink:href="#my-first-icon" /> </svg>
.icon { width: 100px; height: 100px; fill: red; }
<svg class="icon icon-red"> <use xlink:href="#my-first-icon" /> </svg> <svg class="icon icon-blue"> <use xlink:href="#my-first-icon" /> </svg>
.icon { width: 100px; height: 100px; } .icon-red { fill: red; } .icon-blue { fill: blue; }
<svg xmlns="http://www.w3.org/2000/svg" style="display: none"> <symbol id="my-first-icon" viewBox="0 0 20 20"> <title>my-first-icon</title> <path class="path1" d="..." /> <path class="path2" d="..." /> <path class="path3" d="..." /> </symbol> </svg> <svg class="icon icon-colors"> <use xlink:href="#my-first-icon" /> </svg>
.icon-colors .path1 { fill: red; } .icon-colors .path2 { fill: green; } .icon-colors .path3 { fill: blue; }
.path1
, .path2
and .path3
as if they were embedded in .icon-colors
, but technically it is not. use
element is not a placeholder, which is replaced by a specific SVG. This is the link that copies the contents of what it points to in the shadow DOM .
body
, all text on the page will inherit this color until it is overridden. The ancestor does not know the children, but the inherited properties are still transmitted.
fill
property. Look again and you will see that the class in which we defined this color fill
added to the icon instances, not to its definition. So we were able to get multi-colored copies of one source.
fill
attribute that we can inherit.
.parent { --custom-property: red; color: var(--custom-property); }
.parent
children will have red text.
.parent { --custom-property: red; } .child { color: var(--custom-property); }
.child
embedded in .parent
will have red text.
fill
attribute for each part of the path
in the definition of our SVG icon and set them to different CSS variables. Then we assign them different colors.
<svg xmlns="http://www.w3.org/2000/svg" style="display: none"> <symbol id="my-first-icon" viewBox="0 0 20 20"> <title>my-first-icon</title> <path fill="var(--color-1)" d="..." /> <path fill="var(--color-2)" d="..." /> <path fill="var(--color-3)" d="..." /> </symbol> </svg> <svg class="icon icon-colors"> <use xlink:href="#my-first-icon" /> </svg>
.icon-colors { --color-1: #c13127; --color-2: #ef5b49; --color-3: #cacaea; }
<svg class="icon icon-colors-alt"> <use xlink:href="#my-first-icon" /> </svg>
.icon-colors-alt { --color-1: brown; --color-2: yellow; --color-3: pink; }
fill
: in this case, CSS variables are not defined, the definition of the fill
property will be used.
.icon-monochrome { fill: grey; }
fill
property will work because the fill
attribute of the source SVG is set with undefined CSS variable values.
#ff0000
, you call the variable --red
. Semantic is the name of the variable according to its purpose: if you use the color #ff0000
for the cup handle, you call the variable - --cup-handle-color
.
#ff0000
can be used for other things besides the cup handle. The CSS variable --red
can --red
be used for other parts of the icon, which should be red. After all, this is how utility-first CSS works, and it’s good .
--cup-handle-color
, such as - --cup-handle-color
, is more appropriate in our case. When you need to change the color of some part of the icon, you know exactly what and how you need to override it. The class name will remain relevant no matter what color you have assigned.
:root
. This will keep them all in one place and “share” similar colors. :root
has the lowest specificity, so it is easy to override it.
:root { --color-1: red; --color-2: green; --color-3: blue; --color-4: var(--color-1); } .icon-colors-alt { --color-1: brown; --color-2: yellow; --color-3: pink; --color-4: orange; }
:root
. But more importantly, this method does not allow you to change your CSS variables , so you cannot reuse the same names.
--fill-color
. It's simple, understandable and allows you to use this name for all monochrome icons. If I define all the variables in the selector :root
, I will not be able to have several --fill-color
variables. I will be forced to define --fill-color-1
, --fill-color-2
or use namespaces such as --star-fill-color
, --cup-fill-color
.
var()
function, which you use to assign a CSS variable to a property, can take the default value as the second argument.
<svg xmlns="http://www.w3.org/2000/svg" style="display: none"> <symbol id="my-first-icon" viewBox="0 0 20 20"> <title>my-first-icon</title> <path fill="var(--color-1, red)" d="..." /> <path fill="var(--color-2, blue)" d="..." /> <path fill="var(--color-3, green)" d="..." /> </symbol> </svg>
--color-1
, --color-2
and --color-3
, the icon will use the default values ​​set for each path
. This solves the global definition problem that we have when using :root
, but be careful: you now have a default value and it does its job . Thus, you can no longer write only one fill
property to make the icon monochrome. You need to assign a color to each CSS variable used in the icon, separately.
fill
property with a backup color. If CSS variables are supported, this declaration will not be taken into account. If not, it will work.
@mixin
.
@mixin icon-colors($fallback: black) { fill: $fallback; @content; }
.cup { @include icon-colors() { --cup-color: red; --smoke-color: grey; }; } .cup-alt { @include icon-colors(green) { --cup-color: green; --smoke-color: grey; }; }
mixin
via @content
optional. If you do this outside, the compiled CSS will be the same. But it may be helpful to keep it all in one place.
Source: https://habr.com/ru/post/348194/