Languages
[Edit]
EN

CSS - change color of SVG images from <img> tag

3 points
Created by:
sienko
743

In this article, we would like to show you how to change the color of SVG images from <img> tag with CSS.

The solution used in the article changes SVG image color using trick: <img> element combined with filter style property.

Quick solution:

.blue {
    /* filter configuration comes from CSS filter generator - check NOTE below */
    filter: invert(12%) sepia(63%) saturate(6234%) hue-rotate(246deg) brightness(87%) contrast(156%);
}

Note: use the CSS filter generator to compute filter style property value that is necessary to change <img> element color.

Preview:

SVG images color changed using CSS.
SVG images color changed using CSS.

Practical example

In this example, we present how to change the color of the SVG icon from <img> tag using CSS filter property.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    img {
        width: 100px;
        height: 100px;
    }
    
    .red {
        filter: invert(13%) sepia(94%) saturate(7466%) hue-rotate(0deg) brightness(94%) contrast(115%);
    }

    .purple {
        filter: invert(5%) sepia(78%) saturate(6858%) hue-rotate(276deg) brightness(97%) contrast(101%);
    }

    .blue {
        filter: invert(12%) sepia(63%) saturate(6234%) hue-rotate(246deg) brightness(87%) contrast(156%);
    }

  </style>
</head>
<body>
  <img src="https://dirask.com/static/bucket/1632934722_loupe.svg" />
  <img class="red" src="https://dirask.com/static/bucket/1632934722_loupe.svg" />
  <img class="purple" src="https://dirask.com/static/bucket/1632934722_loupe.svg" />
  <img class="blue" src="https://dirask.com/static/bucket/1632934722_loupe.svg" />
</body>
</html>

 

See also

References

Alternative titles

  1. CSS - SVG images color changed using styles
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join