{"id":287092,"date":"2019-05-13T07:44:21","date_gmt":"2019-05-13T14:44:21","guid":{"rendered":"http:\/\/css-tricks.com\/?p=287092"},"modified":"2019-05-13T07:44:21","modified_gmt":"2019-05-13T14:44:21","slug":"change-color-of-svg-on-hover","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/change-color-of-svg-on-hover\/","title":{"rendered":"Change Color of SVG on Hover"},"content":{"rendered":"
There are a lot of different ways to use SVG<\/a>. Depending on which way, the tactic for recoloring that SVG in different states or conditions — Let’s look at the ways.<\/p>\n <\/p>\n Inline SVG is my favorite way to use SVG anyway<\/a>, in part because of how easy it is to access and style the SVG. <\/p>\n \n See the Pen If you’re used to working with icon fonts, one thing you might enjoy about them is how easy it is to change the color. You’re largely limited to a single color with icon fonts in a way that SVG isn’t, but still, it is appealingly easy to change that single color with There is such thing as an SVG sprite<\/a>, which is a group of SVGs turned into \n See the Pen You can still set the fill color from outside CSS rather easily this way, but there are caveats. <\/p>\n SVG can be set as a background image just like PNG, JPG, or whatever other graphics format. At this point, you’ve sort of given up on being able to change the \n See the Pen I don’t blame you if you’d rather not swap sources, so another possibility is to get gnarly with filters<\/a>. <\/p>\n \n See the Pen Trying to finagle the right filters to get the color right is tricky stuff. Fortunately, Barrett Sonntag made a tool<\/a> to calculate the filters for you! Turning black to red ends up a whacky combination like this: SVG also has \n See the Pen This way, the SVG is still in charge of essentially drawing the shape, but the color comes from the \n See the Pen This doesn’t change that much from above, but it does open up one interesting possibility: Using a variable for the internal fills. Here that is with Sass keeping the URLs as variables:<\/p>\n \n See the Pen :hover<\/code>,
:active<\/code>,
:focus<\/code>, class name change, etc. — is different. <\/p>\n
Inline SVG<\/h3>\n
\n bJXNyy<\/a> by Chris Coyier (@chriscoyier<\/a>)
\n on CodePen<\/a>.<\/span>\n<\/p>\ncolor<\/code>. Using inline SVG allows you to set the
fill<\/code>, which cascades to all the elements within the SVG, or you can fill each element separately if needed.<\/p>\n
SVG Symbol \/ Use<\/h3>\n
<symbol><\/code> elements such that any given icon can be referenced easily with a
<use><\/code> element. <\/p>\n
\n Use SVG Hovers<\/a> by Chris Coyier (@chriscoyier<\/a>)
\n on CodePen<\/a>.<\/span>\n<\/p>\n\n
<path><\/code>) can have no fill themselves. This allows the fill set from the parent SVG to cascade into the Shadow DOM created by
<use><\/code>. As soon as you have something like
<path fill=\"blue\" ... \/><\/code> in the
<symbol><\/code>, you’ve lost outside CSS control.<\/li>\n
fill<\/code> of individual elements cannot be controlled within the SVG like you could with inline SVG. This means you’re pretty firmly in single-color territory. That covers most use cases anyway, but still, a limitation nonetheless.<\/li>\n<\/ul>\n
SVG background images<\/h3>\n
fill<\/code>. One possibility, which I’d argue isn’t a particularly good one, is to have two versions of every icon, in the respective colors, and swap between them:<\/p>\n
\n Background SVG Hovers<\/a> by Chris Coyier (@chriscoyier<\/a>)
\n on CodePen<\/a>.<\/span>\n<\/p>\n
\n Background SVG Hovers with Filters<\/a> by Chris Coyier (@chriscoyier<\/a>)
\n on CodePen<\/a>.<\/span>\n<\/p>\ninvert(27%) sepia(51%) saturate(2878%) hue-rotate(346deg) brightness(104%) contrast(97%);<\/code>.<\/p>\n
object<\/code>, which is kinda neat in that it had a built-in fallback back in the day — although browser support is so good these days, I honestly have never used it. But if you’re using it, you would probably have to use this
filter<\/code> technique to swap color on hover.<\/p>\n
\n Background SVG Object Hovers<\/a> by Chris Coyier (@chriscoyier<\/a>)
\n on CodePen<\/a>.<\/span>\n<\/p>\nUse a mask instead of a background image<\/h3>\n
background-color<\/code> (or image! or gradient!) behind it rather than the SVG itself. <\/p>\n
\n Background SVG Hovers with Mask<\/a> by Chris Coyier (@chriscoyier<\/a>)
\n on CodePen<\/a>.<\/span>\n<\/p>\nSVG background images as data URLs<\/h3>\n
\n Background SVG Hovers with Data URL variables<\/a> by Chris Coyier (@chriscoyier<\/a>)
\n on CodePen<\/a>.<\/span>\n<\/p>\n