Data URI
Definition
A data URI, also called a data URL, is a URL scheme defined in RFC 2397 that holds the resource inline. The shape is the word data, a colon, a MIME type, an optional charset or the marker base64, a comma, and then the payload. So a small PNG becomes an src attribute beginning data:image/png;base64, followed by a few thousand characters of encoded bytes. The browser decodes it and treats the result exactly as if it had been fetched. This is the mechanism behind a single-file web app that still shows a logo: there is no second file to lose, because the picture is part of the page. Text payloads can skip the encoding and be percent-escaped instead, which is often smaller for SVG.
Why It Matters
Embedding removes a request and removes a broken-image risk, and for a small icon that is a clear win. Past a certain size it stops paying. Base64 encoding inflates the data by roughly a third, so a 90 KB photograph becomes about 120 KB of text inside your HTML. That text cannot be cached separately, so every visit re-downloads it along with the page, and it cannot be cached by a second page that uses the same image. It also bloats the file you have to edit. A rough rule holds up well: inline below about 4 KB, link anything larger. Three large photos inlined is the difference between a 20 KB page and a 700 KB one.
How It Works
The parser reads the scheme, splits on the first comma, and takes everything before it as the header. The MIME type tells the browser how to interpret the bytes — get it wrong and an image renders as broken even though the data is intact. If base64 appears in the header the payload is decoded from base64 encoding; otherwise it is treated as percent-encoded text. Data URIs are their own origin for security purposes, which is why a page cannot navigate to a top-level data: URL in modern browsers and why a Content Security Policy must list data: explicitly in img-src or the image is blocked. Since the bytes are already in the file, a host simply serves the HTML and no extra request is made.
Real-World Example
A designer publishes a one-page portfolio at studio-one.99helpers.site on 99helpers with the logo inlined as a data URI and the six project photographs left as ordinary files beside the HTML. The logo is 2 KB and costs nothing; inlining the photographs would have pushed the HTML past 2 MB and delayed first paint by seconds. Keeping them separate lets the CDN cache them once and reuse them across every visit.
Common Mistakes
- ✕Inlining large photographs — the page grows by about a third of their size and none of it can be cached separately
- ✕Writing the wrong MIME type in the header, so the browser refuses to render perfectly valid data
- ✕Forgetting to percent-encode the hash and plus characters in a plain-text SVG data URI, which truncates it at the first hash
- ✕Adding a strict Content Security Policy without allowing data: in img-src, which blocks every inlined image at once
Related Terms
Base64 Encoding
A way of writing binary data using 64 printable characters, so that images and other files can travel inside text. It makes the data about a third larger and is not encryption.
Inline CSS
CSS written inside the HTML document rather than in a separate .css file — either in a style element in the head or in a style attribute on a single element. It removes a network request and keeps everything in one file.
Single-File Web App
A single-file web app is a complete, working application contained in one HTML document, with the styles and the JavaScript written inside the same file. Open it in a browser and it runs — there is nothing to install and nothing to build.
MIME Type
A MIME type, also called a media type or content type, is the short label a server sends to say what kind of data a response contains — text/html, image/png, application/pdf. The browser decides what to do with the bytes based on that label, not on the file name.
Web Asset
A web asset is any supporting file a page pulls in once the HTML has arrived — stylesheets, scripts, images, fonts, icons, videos. The HTML is the page; the assets are everything it asks for next.
Put a file online in seconds
Drop in a document, an image, a page or a whole static website and share the link — free, with no build step and no server to set up.
Host a file free →