How to convert SVG code to base64 encoded data URIs (2024)

In this article, we'll explain how to convert SVG icons to base64 string and use them with data URIs.

What is a base64 encoded Data URI #

A data URI is a way to include a resource, e.g., an SVG image, without needing to make an HTTP request.

For example, this is how you would use an SVG with data URI to set the background image of an element in CSS:

.my-element {background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 16 16'%3E%3Cg stroke-width='1' fill='none' stroke='%23372929' stroke-miterlimit='10' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolygon points='15.5,13.5 8,1.5 0.5,13.5 '%3E%3C/polygon%3E%3C/g%3E%3C/svg%3E");}

where the string "data:image/svg+xml,..." is the encoded SVG code with data URI.

Data URIs generally require the data to be converted to base64, which has the downside of increasing the size of the data itself.

Luckily, base64 encoding is not required for SVG, but you'll still need to make changes to the original SVG code to use it as data URI.

Working with SVG icons?Take your designs to the next level with the Nucleo icons →

Create a data URI from a SVG #

Using an SVG as a data URI requires several steps to follow, for example: specify the data format (e.g., data:image/svg+xml), replace all double quotes (") with single quotes ('), replace special characters (e.g., replace '<' with '%3C').

Alternatively, you can use tools like Nucleo to automatically generate the data URI for you: import your SVG icons by dragging them over the app, then select an icon > right-click > Copy Data URI. This will copy the SVG icon as data URI to your clipboard.

How to convert SVG code to base64 encoded data URIs (1)

By default, the app will copy the data URI ready to be used as CSS background-image:

background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 16 16'%3E%3Cg stroke-width='1' fill='none' stroke='%23372929' stroke-miterlimit='10' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolygon points='15.5,13.5 8,1.5 0.5,13.5 '%3E%3C/polygon%3E%3C/g%3E%3C/svg%3E");

If you only need the data URI, head over to the app Settings page (Account > Settings or press ⌘, on Mac or Ctrl, on Windows) and uncheck the 'Copy Data URI as CSS background' option:

How to convert SVG code to base64 encoded data URIs (2)

Why use SVG with data URI #

Data URI allows you to include a resource without having to make an external HTTP request. For SVG icons, this is especially useful if you want to use the icon without having to create an actual .svg file to be hosted, for example, on a server.

When using icons as data URI, a nice trick is setting the CSS mask-image property (rather than background-image) and changing the color of the icon with the background-color property:

.my-element {background-color: red; // icon is redmask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 16 16'%3E%3Cg stroke-width='1' fill='none' stroke='%23372929' stroke-miterlimit='10' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolygon points='15.5,13.5 8,1.5 0.5,13.5 '%3E%3C/polygon%3E%3C/g%3E%3C/svg%3E");}

The end! Check our documentation tutorials page for more articles on working with icons.

How to convert SVG code to base64 encoded data URIs (2024)

FAQs

How to convert SVG code to base64 encoded data URIs? ›

var encodedData = window. btoa(s); Just prepend the data URL intro i.e. data:image/svg+xml;base64, and there you have it.

How to convert SVG code to Base64? ›

var encodedData = window. btoa(s); Just prepend the data URL intro i.e. data:image/svg+xml;base64, and there you have it.

How do I convert SVG to data URI? ›

Create a data URI from a SVG

Alternatively, you can use tools like Nucleo to automatically generate the data URI for you: import your SVG icons by dragging them over the app, then select an icon > right-click > Copy Data URI. This will copy the SVG icon as data URI to your clipboard.

How to convert URI to Base64? ›

Encode "uri" to Base64 format. Simply enter your data then push the encode button. To encode binaries (like images, documents, etc.) use the file upload form a little further down on this page.

What is data URI with Base64 encoded data? ›

If the data is Base64-encoded, then the data part may contain only valid Base64 characters. Note that Base64-encoded data: URIs use the standard Base64 character set (with ' + ' and ' / ' as characters 62 and 63) rather than the so-called "URL-safe Base64" character set.

How do I convert SVG to code? ›

How to convert SVG image to code. SVG image is in fact a code itself. You can easily convert code to images and vice versa simply by opening SVG images in any text editor. Or alternatively, you can paste SVG code to the text editor and save the file with the .svg extension.

Is there an SVG converter? ›

The Adobe Express SVG converter is fast, free, and easy to use. But what sets our SVG converter apart from other tools is that our converter is powered by Adobe Illustrator, which allows you to make professional quality SVG files in seconds.

How to convert an image to data URI? ›

Online image to Data URI conversion tool

They can be used both in HTML, where the image data is placed inside the src attribute or in a CSS file as a background image. This tool lets you select one of both ways or just a raw data URI string without the additional code.

How to convert SVG to base64 in React? ›

  1. // Short script to encode our SVG in base64.
  2. // This can be reversed using window.atob('base64')
  3. var code = document. getElementById('code');
  4. var demo = document. getElementById('demo');
  5. var svg = document. getElementsByTagName('svg')[0];
  6. // Convert the SVG node to HTML.
  7. var div = document. createElement('div');

How to make Base64 encoding? ›

The steps followed by the base64 algorithm include:
  1. Count the number of characters in a string.
  2. If it's not a multiple of three, pad with a special character, i.e., “=” to make it a multiple of three.
  3. Encode the string in ASCII format.
  4. Now, it will convert the ASCII to binary format, 8 bit each.
Dec 16, 2022

Why do we convert an image to Base64? ›

Base64 is a method of encoding binary data, such as images, into a text format that can be easily embedded in web pages and applications. By converting images to Base64, you can streamline your media, reduce server requests, and enhance security by avoiding external image loading.

What is convert to Base64? ›

Base64 transforms any input into an encoding that only uses alphanumeric characters, + , / and the = as a padding character. The base64 is a binary to a text encoding scheme that represents binary data in an ASCII string format. base64 is designed to carry data stored in binary format across the channels.

Why encode file to Base64? ›

Base64 is also widely used for sending e-mail attachments, because SMTP – in its original form – was designed to transport 7-bit ASCII characters only. Encoding an attachment as Base64 before sending, and then decoding when received, assures older SMTP servers will not interfere with the attachment.

Why encode using Base64? ›

Fundamentally, Base64 is used to encode binary data as printable text. This allows you to transport binary over protocols or mediums that cannot handle binary data formats and require simple text. [ Download now: A sysadmin's guide to Bash scripting. ] Base64 uses 6-bit characters grouped into 24-bit sequences.

Should I encode URI? ›

Use encodeURI if you have a whole URL. However, if you only have a portion of a URL, encodeURIComponent is the way to go.

How to extract code from SVG file? ›

Once you have your SVG file, you can open it in a text editor such as Sublime Text or Atom. From here, you can simply copy the code and paste it into your HTML document. Remember to add the appropriate < p >,< b > and < u > tags where applicable.

How to convert SVG into string? ›

You can use XMLSerializer to convert elements to a string. SVG is a DOM element, so you can simply use outerHTML attribute of SVG Elements to get the serialized HTML.

How to convert SVG to Base64 in React? ›

  1. // Short script to encode our SVG in base64.
  2. // This can be reversed using window.atob('base64')
  3. var code = document. getElementById('code');
  4. var demo = document. getElementById('demo');
  5. var svg = document. getElementsByTagName('svg')[0];
  6. // Convert the SVG node to HTML.
  7. var div = document. createElement('div');

How to encode certificate to Base64? ›

Convert customer's certificate to Base64 with steps below:
  1. Open Certificate.
  2. Click on 'Details' Tab.
  3. Click on 'Copy to File...'
  4. Click Next.
  5. Select Base-64 encoded X.509.
  6. Click Next.
  7. Type in File Name.
  8. Click Next.

Top Articles
Certificate of Mailing
Why Do People Love Apple SO Much? Let’s Find Out | Wyzowl
Kathleen Hixson Leaked
Craigslist Monterrey Ca
Monthly Forecast Accuweather
라이키 유출
Palace Pizza Joplin
AB Solutions Portal | Login
Mikayla Campinos Videos: A Deep Dive Into The Rising Star
Cube Combination Wiki Roblox
Osrs Blessed Axe
Skylar Vox Bra Size
Bc Hyundai Tupelo Ms
Dutchess Cleaners Boardman Ohio
I Wanna Dance with Somebody : séances à Paris et en Île-de-France - L'Officiel des spectacles
Snow Rider 3D Unblocked Wtf
Aldi Süd Prospekt ᐅ Aktuelle Angebote online blättern
10-Day Weather Forecast for Santa Cruz, CA - The Weather Channel | weather.com
Csi Tv Series Wiki
Inter-Tech IM-2 Expander/SAMA IM01 Pro
Kountry Pumpkin 29
Ahrefs Koopje
Eine Band wie ein Baum
Sea To Dallas Google Flights
Dulce
Doki The Banker
Certain Red Dye Nyt Crossword
Understanding Gestalt Principles: Definition and Examples
1979 Ford F350 For Sale Craigslist
Pixel Combat Unblocked
Sams Gas Price Sanford Fl
The Rise of "t33n leaks": Understanding the Impact and Implications - The Digital Weekly
Current Time In Maryland
Craigslist Mount Pocono
Arcadia Lesson Plan | Day 4: Crossword Puzzle | GradeSaver
In Polen und Tschechien droht Hochwasser - Brandenburg beobachtet Lage
Marcus Roberts 1040 Answers
2008 DODGE RAM diesel for sale - Gladstone, OR - craigslist
Giovanna Ewbank Nua
Tgirls Philly
Lady Nagant Funko Pop
10 Types of Funeral Services, Ceremonies, and Events » US Urns Online
My Gsu Portal
Tacos Diego Hugoton Ks
Keci News
Electric Toothbrush Feature Crossword
Hkx File Compatibility Check Skyrim/Sse
7 Sites to Identify the Owner of a Phone Number
Renfield Showtimes Near Regal The Loop & Rpx
Land of Samurai: One Piece’s Wano Kuni Arc Explained
Stone Eater Bike Park
Latest Posts
Article information

Author: Greg O'Connell

Last Updated:

Views: 6643

Rating: 4.1 / 5 (42 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Greg O'Connell

Birthday: 1992-01-10

Address: Suite 517 2436 Jefferey Pass, Shanitaside, UT 27519

Phone: +2614651609714

Job: Education Developer

Hobby: Cooking, Gambling, Pottery, Shooting, Baseball, Singing, Snowboarding

Introduction: My name is Greg O'Connell, I am a delightful, colorful, talented, kind, lively, modern, tender person who loves writing and wants to share my knowledge and understanding with you.