DEV Community

Cover image for A better way to use SVGs

A better way to use SVGs

Shubham Jain on April 04, 2021

It's 2021, and we are still not settled on the best way to SVGs. Inline SVGs offer all the benefits—being able to directly modify fill color, use C...
Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️ • Edited

Or, you know, just use <object>, which works great too :D

(or <use> inside an <svg> tag)

Collapse
 
rafibomb profile image
Rafi

Object works, but does not allow direct CSS styles to be applied.

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

Nope. <object> works kind of like putting <svg> in a Shadow-DOM so outside CSS won't affect the inside, but inheritance should still work so you should be able to pass information in through custom properties.

Collapse
 
hanneslim profile image
hanneslim

I am a big fan of using SVG's in Angular like html components:

selector: 'app-seats',
templateUrl: './seats.component.svg',
styleUrls: ['./seats.component.sass'],

Here is a project of mine where you can test the svg file:
nerd-corner.com/how-to-build-a-cus...

Collapse
 
jannikbuscha profile image
Jannik Buscha

can you provide a vue example? i tried the code but i got a IntersectionObserver is not defined error.

Collapse
 
shubhamjain profile image
Shubham Jain

Happy to help! Can you please share the code somewhere (Github, gist, JSFiddle)?

Collapse
 
jannikbuscha profile image
Jannik Buscha
Thread Thread
 
shubhamjain profile image
Shubham Jain

It seems in nuxt.js using external library (like even jQuery) takes some effort. THe easiest way I found was to include it in the head tag (kaloraat.com/articles/how-to-use-j...)

Here's the edited version: codesandbox.io/s/kind-johnson-1dfi...

Collapse
 
matjones profile image
Mat Jones
Collapse
 
shubhamjain profile image
Shubham Jain

Not the same thing. That's a code generator, and while it solves a few problems. I think creating components out of tiny SVG icons is not the best solution to deal with this.

Collapse
 
said_mounaim profile image
Said Mounaim

Niice ! Thank you

Collapse
 
idodav profile image
Ido David

Great library!

Collapse
 
hey_yogini profile image
Yogini Bende

I am facing issues of svg looking little blurry on safari on mobile. Hopefully this will solve that issue.

Collapse
 
mahmoudalfall profile image
mahmoud-alfall

can i use your library to modify svg gradient's stop points.
btw i don't know very much about svg coding