<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Piyush</title>
    <description>The latest articles on DEV Community by Piyush (@kurosyss).</description>
    <link>https://dev.to/kurosyss</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4158366%2Fc45debe4-bbaa-4f1f-957b-04b4f22f8faa.jpg</url>
      <title>DEV Community: Piyush</title>
      <link>https://dev.to/kurosyss</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kurosyss"/>
    <language>en</language>
    <item>
      <title>Why You Should Stop Base64 Encoding SVGs in CSS (And What to Use Instead)</title>
      <dc:creator>Piyush</dc:creator>
      <pubDate>Fri, 02 Oct 2026 19:04:55 +0000</pubDate>
      <link>https://dev.to/kurosyss/why-you-should-stop-base64-encoding-svgs-in-css-and-what-to-use-instead-22gj</link>
      <guid>https://dev.to/kurosyss/why-you-should-stop-base64-encoding-svgs-in-css-and-what-to-use-instead-22gj</guid>
      <description>&lt;p&gt;If you've built any modern frontend project, chances are you've dropped an SVG directly into your stylesheet as a Base64 string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/* The classic 2 AM solution */&lt;/span&gt;
&lt;span class="nc"&gt;.icon&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background-image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sx"&gt;url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNCAyNCI+PHBhdGggZD0iTTEyIDJMMiA3bDEwIDUgMTAtNS0xMC01eiIvPjwvc3ZnPg==')&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works, it eliminates an extra HTTP request, and the icon renders. &lt;/p&gt;

&lt;p&gt;But down the line, this habit almost always comes back to bite you. Here is why Base64 encoding in CSS is an anti-pattern, and what modern frontend developers should be doing instead.&lt;/p&gt;




&lt;h3&gt;
  
  
  1. The 33% Byte Tax You Didn't Sign Up For
&lt;/h3&gt;

&lt;p&gt;Base64 works by taking 3 bytes of binary data and representing them with 4 ASCII characters. That mathematically introduces a &lt;strong&gt;~33% size inflation&lt;/strong&gt; right out of the gate.&lt;/p&gt;

&lt;p&gt;For raster images (PNG, JPEG), Base64 is sometimes tolerated for tiny placeholders. But SVGs are &lt;strong&gt;plain XML text&lt;/strong&gt;. Converting readable text into pseudo-random Base64 strings increases raw file weight without adding a single benefit.&lt;/p&gt;

&lt;p&gt;Furthermore, compression algorithms like Gzip and Brotli compress structured, repetitive text (like XML tags and attributes) far more efficiently than high-entropy Base64 strings.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. The Maintenance Nightmare (The "Color Tweak" Trap)
&lt;/h3&gt;

&lt;p&gt;Imagine your designer asks: &lt;em&gt;"Can we change that primary button icon from blue (#3b82f6) to indigo (#6366f1)?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;With Base64:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open Figma / Illustrator.&lt;/li&gt;
&lt;li&gt;Select the vector path and change the fill.&lt;/li&gt;
&lt;li&gt;Export SVG to disk.&lt;/li&gt;
&lt;li&gt;Run it through a Base64 converter.&lt;/li&gt;
&lt;li&gt;Paste a fresh 40-line string into your CSS.&lt;/li&gt;
&lt;li&gt;Commit and pray you didn't miss a character.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's 5 minutes wasted for a 1-character color tweak.&lt;/p&gt;




&lt;h3&gt;
  
  
  The Modern Fix: Plain UTF-8 URL Encoding
&lt;/h3&gt;

&lt;p&gt;Because SVGs are XML text, browsers understand them natively inside CSS &lt;code&gt;url()&lt;/code&gt;. You don't need Base64 encoding at all.&lt;/p&gt;

&lt;p&gt;You only need to escape a handful of characters that would conflict with CSS parser rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;#&lt;/code&gt; becomes &lt;code&gt;%23&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;%&lt;/code&gt; becomes &lt;code&gt;%25&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;"&lt;/code&gt; or &lt;code&gt;'&lt;/code&gt; handled properly&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;&lt;/code&gt; becomes &lt;code&gt;%3C&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;gt;&lt;/code&gt; becomes &lt;code&gt;%3E&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Look at how much cleaner and maintainable the CSS becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/* Readable, debuggable, and ~30% smaller */&lt;/span&gt;
&lt;span class="nc"&gt;.icon&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background-image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sx"&gt;url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='%236366f1' d='M12 2L2 7l10 5 10-5-10-5z'/%3E%3C/svg%3E")&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice &lt;code&gt;fill='%236366f1'&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;If your designer asks for that color change now, you just edit &lt;code&gt;%236366f1&lt;/code&gt; right in your code editor. No Figma, no re-exporting, no context switching.&lt;/p&gt;




&lt;h3&gt;
  
  
  🔥 Pro-Tip: Dynamic Colors with CSS &lt;code&gt;mask-image&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;If you want an SVG icon that changes color with text hover states without touching the SVG at all, use CSS masking instead of &lt;code&gt;background-image&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.dynamic-icon&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;inline-block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;24px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;24px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;currentColor&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;/* Inherits text color */&lt;/span&gt;
  &lt;span class="nl"&gt;-webkit-mask-image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sx"&gt;url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 2L2 7l10 5 10-5-10-5z'/%3E%3C/svg%3E")&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;mask-image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sx"&gt;url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 2L2 7l10 5 10-5-10-5z'/%3E%3C/svg%3E")&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;-webkit-mask-repeat&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;no-repeat&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;mask-repeat&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;no-repeat&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;-webkit-mask-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;contain&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;mask-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;contain&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.button&lt;/span&gt;&lt;span class="nd"&gt;:hover&lt;/span&gt; &lt;span class="nc"&gt;.dynamic-icon&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#ef4444&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;/* Now it instantly turns red on hover! */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This single technique gives you the flexibility of inline SVG icons with the performance and caching perks of CSS.&lt;/p&gt;




&lt;h3&gt;
  
  
  Instant Free Tool: SVG2CSS Studio
&lt;/h3&gt;

&lt;p&gt;Manually replacing &lt;code&gt;#&lt;/code&gt; with &lt;code&gt;%23&lt;/code&gt; and stripping editor junk (like &lt;code&gt;xml:space&lt;/code&gt;, Figma metadata, and unneeded namespaces) gets tedious when dealing with dozens of vectors.&lt;/p&gt;

&lt;p&gt;I put together a dedicated, zero-friction web tool for this exact workflow:&lt;/p&gt;

&lt;p&gt;👉 &lt;strong&gt;&lt;a href="https://svg2css.com" rel="noopener noreferrer"&gt;SVG2CSS Studio&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Highlights:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;100% Client-Side&lt;/strong&gt;: Processes everything in your browser's V8 engine. Zero server roundtrips, total privacy.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multiple Output Presets&lt;/strong&gt;: Copies raw CSS &lt;code&gt;background-image&lt;/code&gt;, dynamic CSS &lt;code&gt;mask-image&lt;/code&gt;, SCSS mixins, and Tailwind arbitrary values.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Automated Cleaner&lt;/strong&gt;: Cleans out useless editor comments and bloat attributes automatically.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Live Canvas Tester&lt;/strong&gt;: Lets you test repeat modes, sizing, scaling, and canvas colors in real-time before copying.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;What is your team's current approach to icons and vector backgrounds? Do you still rely on icon fonts, SVG sprite sheets, or CSS Data-URIs? Let's discuss below!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>css</category>
      <category>svg</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
