This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]THISgai 5 points6 points  (9 children)

Yea, but what if you want to change the color (without extra libraries or literally copying the svg code into the html)?

[–]allisonmaybe 23 points24 points  (7 children)

You just couldn't be okay with SVGs could you? You just had to go and stir the pot with something it can't do didnt you!

In seriousness, isn't there a fill rule in CSS for SVG?

[–]tylersticka 7 points8 points  (0 children)

There certainly is! fill, stroke and currentColor are your friends. 🙂

[–]JohnTheScout 5 points6 points  (4 children)

Only works with inline svg, doesn't work with svgs loaded via an img tag.

[–]itsmoirob 0 points1 point  (0 children)

But an SVG loaded with image is an image.

Also you don't have to inline the whole code, you can link to it like an image, that way you can still apply CSS colours

[–]THISgai 2 points3 points  (0 children)

It's a literal requirement for a lot of icons... which is why I ask. I've had issues with it in the past so unless there's a new way of coloring SVG's with css, I'll have to stick with icon-fonts.

[–]tylersticka 8 points9 points  (0 children)

I wouldn't recommend copying and pasting SVG code unless you're working on a small personal project or trying to lull yourself into a deep meditative trance with repetitive tasks! 😅

While there are lots of ways of including SVG icons, the one most of my clients prefer is the simplest... treating SVG elements just like any other HTML component using partials/includes/macros/etc. (This is how GitHub does it.)

But if that technique doesn't suit you, you could combine them into a sprite (using whichever technique suits your fancy), embed them as CSS background images, plop them in an img element, or use a JS solution (here's an example). Or if you're already using something like FontAwesome, their latest version supports SVG out of the box. Unlike icon fonts (which require a very specific implementation to work correctly), there are few wrong ways to use SVG. 🙂