can I make a reusable html code like a function?
(not really sure how to call it)
let's say that I have a large SVG with a css animation and that I want to use it multiple times inside of my html file (it can't be a php file . . . I know, that would solve it) can I make something like a function or definition of my pseudo code where I will define this chunk of code once, and everytime I need to place it down I will just use that variable/definition instead of copy-pasting it like 20 times?
(I know that I can create pseudo-class in which I can paste this svg via link in the content but I wouldn't be able to controll and animate separate parts of it)
15 Replies
use <use>
- SVG: Scalable Vector Graphics | MDN
The element takes nodes from within the SVG document, and duplicates them somewhere else.
The effect is the same as if the nodes were deeply cloned into a non-exposed DOM, then pasted where the use element is, much like cloned template elements.
ow can I just create svg element where I will just make one <use> element with that first svg's id?
*id of that first complex svg that I will define somewhere at the begining
yup pretty much
This is commonly used for SVG sprites too.
thanks a lot . . . I couldn't find it when I was looking for it
I think the animation should just apply to the use too
Least it is when I use JS to animate
thanks
may I ask you for your help in css animations and svg again?
I'm trying to do this: (attachment)
ease is applied in between every key frame rather than the whole animation, I would also like the shadow and gradient not to spin . . . do you know how to solve that?
and the letter, which willl be always different inside of that triangle should rotate as well
nevermind, screw that . . .
right now I have this:
@b1mind , could you tell me how to copy those svg elements with that <use> element so I can spin only that one which I'm hovering above only? thansks
Make a codepen and I'll take a look
Not at my PC atm but I would visually hide the main SVG with no animation then add the hover to the svgs with Use. Like I mentioned it would probably repeat the animation on all
Which is actually handy in it's own right
but how . . . in css I can go in class
svg:hover use
but I can't specify .triangle
and .circle
further onah right... cause you are targeting the elements in it.. 🤔
I would make just the triangle the svg xD
Animation is the one time I do use inline svg over sprites/images for this reason. It can be tricky
gotta run but I might think on this some. See if I come up with a slick solution.
This is where UI components come in really hand too xD
Cause today I would just whip this up as a Svelte Component and reuse that xD
well . . . i just get rid of that use component . . . found out that I don't need to define that <defs> element in every svg and it is much more simpler now . . . (it was more complex before but I gave up)
copying element of this size is ok
(but what a shame I cannot use php in this project, I would just create one echo function)
another great shit i just found out . . . iframes