Image to Base64 Converter — CSS Snippet, img src & Data URL
Convert any image to Base64 encoding with three ready-to-paste code snippets — a raw data URL, a CSS background-image declaration and an HTML img src attribute. Furthermore, each snippet has its own Copy button for pasting directly into the right context. File size, Base64 length and estimated overhead percentage all display in the stats strip.
Any image — converts to Base64 in browser
How to use the Image to Base64
Drop your image
Drag any image onto the drop zone or click to browse. Furthermore, the FileReader API converts the image to a Base64 data URL directly in the browser — the file never leaves your device. The stats strip shows file size, Base64 length and overhead percentage.
Review the three code snippets
Three snippets appear automatically — the raw data URL, an HTML img tag with the Base64 src. A CSS background-image declaration. Furthermore, each snippet is ready to paste into the correct context without any editing. A preview of the first and last characters of each snippet confirms the correct encoding.
Copy the snippet you need
Click the Copy button next to whichever snippet matches your use case. Furthermore, the full Base64 string copies to your clipboard regardless of the truncated preview display. Paste directly into your HTML, CSS or JavaScript file.
The three Base64 snippet formats
Each snippet format targets a different code context. Furthermore, using the wrong format requires manual editing — each snippet generates the correct syntax for its context automatically.
| Snippet | Syntax | Use in |
|---|---|---|
| Raw data URL | data:image/jpeg;base64,/9j/… | JavaScript, JSON, API payloads |
| HTML img src | <img src="data:…" alt="…"> | HTML files, email templates, inline images |
| CSS background-image | background-image: url("data:…"); | CSS files, inline styles, styled-components |
Base64 encoding and size overhead
Base64 encodes every 3 binary bytes as 4 ASCII characters. Furthermore, this produces a 33% size increase compared to the original binary file.
Characters: A–Z, a–z, 0–9, +, / (64 characters + = padding)
Data URL prefix: data:[MIME type];base64, added before encoded data
Worked example: embedding a logo in an HTML email template
An email designer needs a company logo that displays. Relying on external image hosting — some email clients block external images by default. Embedding as Base64 solves this:
What is Base64 image encoding?
Base64 encoding converts binary image data into a string of printable ASCII characters. Furthermore, this allows images to embed directly into text-based formats like HTML, CSS and JSON without requiring a separate file reference. The data URL format — data:[type];base64,[data] — is the standard way to include Base64-encoded images in web contexts. Moreover, Base64 embedding eliminates the HTTP request that a normal image src URL requires, which can improve performance for small images used frequently across a page.
The trade-off of Base64 encoding is a 33% size increase compared to the binary file. Furthermore, a 100 KB image becomes approximately 133 KB as Base64. For large images, this overhead outweighs the request-saving benefit. Moreover, Base64 images cannot be independently cached by the browser — the full string must download every time the HTML or CSS file loads. The optimal use case is small images under 10 KB — icons, tiny logos, loading spinners — where the request overhead dominates the performance cost.
Data URLs and Content Security Policy
Content Security Policy (CSP) headers on modern web applications may block inline data URLs for images. Furthermore, a CSP directive like img-src 'self' prevents data URL images from loading unless img-src data: is explicitly allowed. Developers must add data: to the img-src directive before embedded Base64 images will display. Moreover, this CSP requirement is a security consideration — data URL images bypass same-origin restrictions, which is why strict CSP configurations restrict them.
Why ready-made snippets save developer time
Base64 tools typically show only the raw encoded string. Furthermore, a developer who needs a CSS background-image declaration must manually wrap the string in url("…") syntax — an error-prone step when the string is thousands of characters long. The CSS and HTML snippets in this tool include the correct surrounding syntax automatically. Moreover, the per-snippet Copy button copies the entire string including the surrounding syntax — the developer pastes directly into their code editor without any manual assembly.
Frequently asked questions
Related tools
SVG Optimiser
Optimise SVG before converting to Base64. Furthermore, size reduction percentage shown.
Image Compressor
Compress the image before encoding to minimise overhead. Furthermore, target file size mode included.
Image Format Converter
Convert to a smaller format before encoding. Furthermore, WebP produces smaller Base64 strings.
Favicon Generator
Generate small favicons ideal for Base64 embedding. Furthermore, 16×16 PNG is under 1 KB.
Image to PDF
Convert images to PDF instead of Base64. Furthermore, multiple images combine into one PDF.
Image Colour Picker
Pick colours from images before encoding. Furthermore, HEX, RGB and HSL formats all shown.