1 min read

How to Change SVG Color Automatically

Table of Contents

Background

During my journey to develop this site, I want to create my own logo from a .png file. Unfortunately, since I have a light and dark mode, the logo image is not adapt properly.

Solution

To resolve this issue, I’m converting the image into svg format then add fill="currentColor" attribute inside the <svg> tag. The fill attribute sets the color of the inside of an element.

<svg width="600" height="220" xmlns="http://www.w3.org/2000/svg">
  <rect width="150" height="100" fill="currentColor" />
</svg>

Example

Before

After