Data URI Generator

URI length—
Media type—
Input bytes—

How Data URI Generator is calculated: data:[media type];base64,[base64 of the input]. Base64 expands the data by about a third, so a data URI is only for small assets.

What is a data URI generator?

A data URI generator encodes text — or a small file — as a data: URI, a complete resource written as a single string. The default text "Hello, World!" becomes "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==", which is 43 characters long. A browser reading that URI gets the text itself, with no separate file to fetch.

Data URIs embed small assets inline. A tiny icon in a stylesheet, a small SVG in an HTML page, or a short text snippet in a link can all live inside the file that uses them, instead of in a separate file that needs its own request. That saves a round trip, which matters on slow connections.

This tool encodes text you paste, and it can also read a small file you pick — an image or a text file — and encode that instead. Everything runs in your browser: the file is read locally with FileReader and never uploaded.

How a data URI works

A data URI has three parts: the scheme "data:", the media type such as text/plain, and the data itself, prefixed with ";base64," when it is base64-encoded. The default output is "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==" — the media type text/plain, then the base64 of "Hello, World!".

Base64 is the encoding that makes binary data safe to write as text. It uses only letters, digits, +, /, and =, so the result can sit inside HTML, CSS, or a URL without breaking anything. Percent-encoding is the other encoding a URL uses, and a URL encoder decoder handles that one. The cost is size: base64 expands the data by about a third, so 13 bytes of input become a longer string — which is why data URIs suit small assets only.

The media type tells the browser what the data is. text/plain is plain text, text/html is a web page, application/json is JSON, and image/svg+xml is an SVG image. Pick the wrong type and the browser will show the data as the wrong thing — an SVG with a text/plain type shows as code, not as an image.

Text, files, and media types, explained

The text path is for short content: a snippet of HTML, a JSON object, or a line of text. Type it in, pick the media type, and the data URI appears. The default text is 13 bytes, and its data URI is 43 characters — the expansion is the base64 overhead plus the "data:text/plain;base64," prefix.

The file path is for small files: pick a file and the tool reads it as a data URI with the file's own media type. An image file becomes a data URI you can paste into an img tag's src, and a text file becomes a data URI you can paste into a link. The file size is shown under the picker, so you can see whether the result is small enough to embed.

The media type matters most for files. A PNG file read as a data URI gets image/png automatically, because the browser knows the file's type. For pasted text, you pick the type yourself — text/plain for plain text, text/html for markup, application/json for data, and image/svg+xml for SVG code.

The media type is the part that tells the browser what the data is, and it must match the data exactly. A PNG image needs image/png, a JPEG needs image/jpeg, and plain text needs text/plain, because the browser decides how to display the data from the media type, not from the bytes. A text file labelled as an image will fail to display, and an image labelled as text will show as garbage characters. The generator sets the media type from the file you pick, or from the choice you make for pasted text, and the prefix is part of the URI, so a wrong media type is a broken URI.

A worked example: embedding text

Take the default text, "Hello, World!" — 13 bytes. Its base64 is SGVsbG8sIFdvcmxkIQ==, and the full data URI is "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==", which is 43 characters. Paste that URI into a browser's address bar and the browser shows the text "Hello, World!" — no server, no file, just the URI itself.

Try a different media type: switch to text/html with the text "<b>Hello</b>" and the data URI becomes a tiny web page that renders Hello in bold. The media type is what turns the same bytes into a different result, which is why picking it matters.

Try a file: pick a small PNG and the output is a data URI starting "data:image/png;base64," followed by the image's bytes in base64. Paste it into an img tag — <img src="data:image/png;base64,..."> — and the image shows without any file on the server. Keep files small: a data URI larger than a few tens of kilobytes belongs in a file, not inline.

Who uses a data URI generator, and why

Web developers are the main users. A small icon, a tiny background pattern, or a short SVG can be embedded in a stylesheet as a data URI, which saves a request and keeps the page to a single file. It is the standard trick for critical CSS and for single-file pages.

Email developers are the second group. HTML emails cannot always fetch external images, so small images are embedded as data URIs instead. A logo in an email signature is the classic case — it shows even when the mail client blocks external images.

Anyone sharing a snippet is a third group. A data URI turns a piece of text or a small file into a single string that can be pasted into a chat, a ticket, or a document. A base64 encoder decoder gives the base64 part on its own, when the data: prefix is not needed.

Email developers are a third group. An email that references an external image may show a broken picture, because many email clients block remote images by default. A data URI embeds the image inside the email itself, so the picture displays without a download, and the email works offline. The trade-off is size — the email grows by the size of the image — so the technique suits small logos and icons, not photographs. The generator makes the URI, and the email template carries it.

Data URI vs base64 encoding

A data URI is base64 with a header. The base64 encoder decoder on this site gives you the base64 of a text — SGVsbG8sIFdvcmxkIQ== for "Hello, World!" — while this tool wraps it as a complete data URI with the media type, which is what a browser needs to use it directly.

Use base64 alone when another system expects it: an API field, a token, or a basic-auth header. Use a data URI when a browser needs to consume the data: an img src, a CSS background, or a link href. The difference is the prefix, and the prefix is what makes the string usable as a URL.

Both tools share the same encoder, so they agree: the base64 in the data URI is the same string the encoder produces. A base64 to image converter goes the other way, turning a base64 image back into a picture.

Common data URI mistakes

The first mistake is embedding a large file. Base64 expands data by a third, and a data URI is downloaded with the page that contains it, so a 1 MB image becomes 1.4 MB of page weight that cannot be cached separately. Keep data URIs to a few tens of kilobytes at most, and use a file for anything bigger.

Second: the wrong media type. An SVG with text/plain shows as code, and HTML with text/plain shows as text. Match the type to the content — image/svg+xml for SVG, text/html for markup, application/json for data — or the browser will show the wrong thing.

Third: forgetting that data URIs do not cache separately. A file is downloaded once and cached; a data URI is downloaded again with every page that contains it. An icon used on every page of a site belongs in a file, not in a data URI on every page.

Fourth: pasting a data URI into a place that does not accept one. Some fields — a plain text message, a CSV cell — treat the URI as text, which is fine, but a system that validates URLs may reject the data: scheme. Check where the URI will be used before embedding it.

A second mistake is using a data URI for a large file. A data URI makes the data about a third larger, because base64 expands it, and the whole URI sits inside the page, so the page itself grows by the size of the file. A small icon of a few kilobytes is fine; a photograph of a few megabytes will make the page slow to load and impossible to cache separately. The rule is simple: a data URI is for small assets that are used once, and a file that is large or reused belongs on the server as a separate file.

How to use this data URI generator

Type text into the box — the default is "Hello, World!", 13 bytes — and pick the media type. The data URI updates live in the box below, 43 characters for the default. To encode a file instead, pick it with the file picker and the URI appears with the file's own type.

To copy the URI, use the Copy button next to the output box. To use it, paste it into an img src, a CSS url(), or a browser address bar. If the URI is longer than a few tens of kilobytes, use a file instead — the size is shown above the output so you can judge.

Everything runs in your browser. No sign-up, no account, no data sent anywhere — your text and your file never leave your device. It works offline once the page has loaded.

Type the text or pick a file, and the data URI appears underneath with its length and the size of the input. The text path is for short strings, such as a snippet of SVG or a line of text, and the file path is for images and other files — pick the file and the browser reads it and encodes it. Copy the URI and paste it where it belongs: into an img tag, a stylesheet, or a link. Test it by opening the URI in a browser tab; if the image or text appears, the URI is correct.

Frequently Asked Questions

A complete resource written as a single string: data:, then the media type, then ;base64, then the base64 of the data. The text Hello, World! becomes data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==, which a browser can open directly with no file to fetch.

Base64 expands data by about a third, and the data: prefix adds more. Hello, World! is 13 bytes, and its data URI is 43 characters. That overhead is why data URIs suit small assets only — a few tens of kilobytes at most.

Yes — pick the file with the file picker and the tool reads it as a data URI with the image's own media type, such as data:image/png;base64,.... Paste it into an img tag's src and the image shows with no file on the server. Keep images small.

image/svg+xml. An SVG with text/plain shows as code, not as an image. Match the type to the content: text/plain for plain text, text/html for markup, application/json for data, and image/svg+xml for SVG.

More free tools

More calculators and utilities from Sortrun — free, no sign-up.