SVG Data URIs Explained: How to Embed SVG in CSS and HTML (2026)

    Data URIs allow you to embed SVG code directly inside CSS stylesheets and HTML documents, eliminating the need for separate file requests. This technique reduces HTTP requests, simplifies deployment, and can improve performance for small graphics. However, it comes with trade-offs around file size, caching, and maintainability. This guide explains how SVG data URIs work, compares encoding methods, and provides clear guidelines for when to use them — and when not to.

    What Are Data URIs?

    A data URI is a way to include file content directly in a URL string using the data: scheme. Instead of referencing an external file, the file's content is encoded inline:

    background-image: url('data:image/svg+xml,<svg ...>...</svg>');

    Anatomy of an SVG data URI:

    • data: — The URI scheme indicating inline data
    • image/svg+xml — The MIME type declaring this is SVG content
    • , or ;base64, — The encoding separator (URL-encoded or Base64)
    • The SVG content itself — either URL-encoded text or Base64-encoded binary

    Browsers parse data URIs the same way they parse external files — the rendering is identical.

    URL-Encoding vs Base64: Which to Choose

    There are two ways to encode SVG content in a data URI. The choice significantly affects file size:

    CharacteristicURL-EncodedBase64
    Size overhead~10–20% (only special characters encoded)~33% (all bytes encoded)
    ReadabilityPartially readable in sourceCompletely unreadable
    CSS compatibilityAll modern browsersUniversal (including older systems)
    HTML <img> supportCSS background onlyWorks in <img src> and CSS
    Special charactersMust escape #, <, >, "No escaping needed
    Best forCSS backgrounds (smaller output)Maximum compatibility

    Recommendation: Use URL-encoded data URIs for CSS backgrounds. Use Base64 only when you need to embed SVG in <img src> attributes or when working with environments that have escaping issues.

    How to Create SVG Data URIs

    Follow these steps for optimal results:

    • Step 1: Optimize the SVG — Run it through the SVG Optimizer to remove metadata, strip whitespace, and minimize path data. Smaller source SVG = smaller data URI.
    • Step 2: Remove the XML declaration — The <?xml version="1.0"?> header is unnecessary for data URIs and adds bytes.
    • Step 3: Choose your encoding — For CSS use, URL-encode special characters. For img tags, use Base64.
    • Step 4: Construct the URI — Combine the MIME type, encoding separator, and content into the data URI string.
    • Step 5: Apply in your CSS — Use the data URI as a background-image value or content in pseudo-elements.

    Our SVG to Data URI tool handles all of these steps automatically — paste your SVG and copy the ready-to-use CSS.

    Common Use Cases for SVG Data URIs

    Data URIs work best for small, frequently-used graphics embedded in CSS:

    • CSS pseudo-elements — Icons in ::before and ::after where you can't use HTML markup
    • List markers — Custom bullet points using list-style-image
    • Form element decorators — Checkbox marks, select arrows, input icons
    • Repeating patterns — Small SVG tiles used with background-repeat
    • Critical-path icons — Small icons that must render immediately without waiting for external file downloads

    For all of these cases, the eliminated HTTP request outweighs the small increase in CSS file size.

    When NOT to Use Data URIs

    Data URIs have real limitations that make them the wrong choice in several scenarios:

    • Large SVGs (>4 KB) — The inline code bloats your CSS file, increasing parse time and reducing cache efficiency
    • Frequently changing graphics — Every change requires regenerating the data URI and updating CSS, which breaks CSS caching
    • Interactive SVGs — Data URI SVGs cannot be styled with CSS or manipulated with JavaScript. Use inline SVG instead.
    • Many unique SVGs — If you have 50+ unique data URIs in your CSS, the file becomes unwieldy. Use an SVG sprite sheet instead.
    • Images referenced multiple times — External SVG files are downloaded once and cached. Data URIs duplicate the content every time they're referenced.

    Size guideline: If the SVG is under 2 KB after optimization, a data URI is usually beneficial. Between 2–4 KB, measure the impact. Above 4 KB, use an external file.

    Data URI Performance Impact

    The performance trade-off is straightforward: you save one HTTP request but increase CSS file size. Here's how the math works for a typical icon:

    ApproachHTTP RequestsCSS Size ImpactTotal Bytes
    External SVG file+1 request~40 bytes (URL reference)~500 B (file) + 40 B (CSS)
    URL-encoded data URI0 requests~600 bytes (inline)600 B (CSS only)
    Base64 data URI0 requests~800 bytes (inline)800 B (CSS only)

    For small icons on HTTP/1.1 connections (where each request has overhead), data URIs win. On HTTP/2+ with multiplexing, the advantage is smaller but still relevant for critical-path rendering.

    Frequently Asked Questions

    Do data URIs work in all browsers?
    Yes. Both URL-encoded and Base64 SVG data URIs are supported in all modern browsers including Chrome, Firefox, Safari, and Edge. IE11 supports Base64 but has a 4 KB limit on data URIs.
    Can I animate an SVG embedded as a data URI?
    SMIL animations within the SVG itself will work, but you cannot apply external CSS or JavaScript to a data URI SVG. For interactive or CSS-animated SVGs, use inline SVG instead.
    Will data URIs increase my CSS file size significantly?
    For a handful of small icons (under 2 KB each), the impact is minimal — typically adding 5–15 KB to your CSS. For larger sets, consider SVG sprites or external files.

    Popular Tools