Change color of css 'inline svg' dynamically

Hi! I've been tearing my hair out with this for a while now. I have an css inline svg which I want to change the fill color off dynamically in css. The codepen is here: https://codepen.io/stefanh-at/pen/QWJGZKd Some restrictions I have for this: - I can't use filter because the background-color can't change and i have children which can't change either - The svg has to be inline. I can't use a separate file or use an HTML <svg> tag - I need to be able to change the fill color of the icon using either css custom properties or currentColor - I can't use a clip-path to cut out the background since I need to put another icon in a different (dynamic) location - I can't use a ::before or ::after pseudo element Already looked into a lot of avenues so here's what doesn't work: - Using fill='currentColor' doesn't work. This only applies to <svg> tags which I can't use in my markup Yeah this is quite the restricting case but if there is some way which I've missed to recolor the svg, this would all be solved. Hope someone can help!
23 Replies
b1mind
b1mind•2y ago
Just fyi, inline is <svg> also why can't you use it? I'm kinda lost on that part. What you are doing now is setting a background image to a div... You are not going to be able to be dynamic that way
StefanH
StefanHOP•2y ago
Yeah it seems to be impossible
b1mind
b1mind•2y ago
Yea but again why can't you just have a real inline svg?
StefanH
StefanHOP•2y ago
I'm trying to add an icon to an input I think i may have actually found a much better way to create my input but still wanted to see if somebody knew some black magic way to recolor this svg
Azyrum
Azyrum•2y ago
https://codepen.io/Vasco-Gama/pen/GRwNYMJ with inline you can just do this
b1mind
b1mind•2y ago
Yea that or use custom props with fallback to current color
StefanH
StefanHOP•2y ago
yeah with an svg tag it's easy but i can't use one
b1mind
b1mind•2y ago
fill="var(--fill, currentColor)" (this works great for use:# and sprites)
StefanH
StefanHOP•2y ago
i think i actually found a cool way to do what i want with clip paths so i sort of solved my problem but would be cool if it was possible this way
b1mind
b1mind•2y ago
Well if you need further help its best to show us the bigger picture. Lots of ways to do things 😄
StefanH
StefanHOP•2y ago
it would be possible to use filter to change the color if only i didn't have that background-color to worry about... or i could use filter on a ::before pseudo element which avoid tinting the bg
b1mind
b1mind•2y ago
but why does it need to be a bg image?
StefanH
StefanHOP•2y ago
I want clean markup :)
b1mind
b1mind•2y ago
Again you need to show us the bigger picture and why you are piggon holing your self ok that is just silly ... sorry not sorry xD
StefanH
StefanHOP•2y ago
Yeah :)
b1mind
b1mind•2y ago
use a sprite then
StefanH
StefanHOP•2y ago
I'm challenging myself here
Azyrum
Azyrum•2y ago
You making a form?
b1mind
b1mind•2y ago
put the inline up top, then use <use> or you can use extrenal either way you can have the power that way, and "clean markup" https://24ways.org/2014/an-overview-of-svg-sprite-creation-techniques/ You can do what I said above and have some amazing control using custom props/currentColor
StefanH
StefanHOP•2y ago
I'm making a switch like this and wanted to have two icons on the left and right
StefanH
StefanHOP•2y ago
checkmark is revealed when you check the box
StefanH
StefanHOP•2y ago
Okay I managed to get what i wanted using animated clip-paths
b1mind
b1mind•2y ago
Looks great gj!
Want results from more Discord servers?
Add your server