Using Hex Values for SVG in Firefox: A Guide for Web Developers

Dhruv Singhal
2 min readMay 6, 2023

--

Have you ever tried using a hex value for an SVG in Firefox and found that it doesn’t work as expected? If so, don’t worry, you’re not alone! Many developers have faced this issue and wondered how to make it work.

In this article, we’ll explore why using a hex value for SVG in Firefox doesn’t work and how to solve this issue.

Why Doesn’t Using a Hex Value for SVG in Firefox Work?

When using a hex value for an SVG in Firefox, you may have noticed that the color doesn’t render as expected. This is because ‘#’ in URLs starts a fragment identifier, and Firefox interprets it as such.

To make it work, you need to use the escaped value of the ‘#’ character, which is ‘%23’. By doing this, Firefox will recognize it as a hex value and render the color correctly.

How to Use a Hex Value for SVG in Firefox

To use a hex value for SVG in Firefox, you need to replace ‘#’ with ‘%23’ in the SVG code. Here’s an example:

background: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='20' height='10'><path fill='%23FF0000' d='M 0,10 H 20 L 10,0 Z' /></svg>") repeat-x;

As you can see, we replaced ‘#’ with ‘%23’ in the fill attribute of the SVG path.

In case you’re wondering what this code does, it’s a CSS property that sets the background image of an element to an SVG with a red triangle.

Final Thoughts

Using a hex value for SVG in Firefox is not as straightforward as it is in other browsers. By replacing ‘#’ with ‘%23’, you can solve this issue and make it work as expected.

We hope this article has helped you understand why using a hex value for SVG in Firefox doesn’t work and how to solve it. If you have any questions or comments, please feel free to leave them below.

If you found this article helpful, please like and share it with others. Don’t forget to follow me to stay up-to-date on my latest articles. Thank you for reading!

--

--

Dhruv Singhal
Dhruv Singhal

Written by Dhruv Singhal

Data engineer with expertise in PySpark, SQL, Flask. Skilled in Databricks, Snowflake, and Datafactory. Published articles. Passionate about tech and games.

No responses yet