Skip to content

Master base64 image encoding

Learn how to convert and use base64 images like a pro

1. Why base64 encoding?

Base64 encoding converts binary image data into a text string that can be embedded directly into your HTML or CSS. Instead of linking to an external image file, the entire image is contained within your code using a special data:image/... URI format.

How it works:

When you encode an image to base64, it converts the binary data (the actual image file) into ASCII text characters. This text can then be embedded directly in your HTML, CSS, or even JavaScript, eliminating the need for separate image files.

Advantages:

  • Zero HTTP Requests - Images load instantly with the page, no separate download needed
  • Perfect for Email - No "show images" warnings in email signatures and newsletters
  • Single-File Deployment - Great for mockups, demos, or standalone HTML files
  • No 404 Errors - Images can't be accidentally deleted or moved
  • Faster Initial Render - Small icons and logos appear immediately above the fold
  • Works Offline - Perfect for Progressive Web Apps (PWAs) and cached content
  • Version Control Friendly - All assets in one file, easier to track changes

Disadvantages:

  • File Size Increase - Base64 encoded images are ~33% larger than the original
  • No Browser Caching - Embedded images can't be cached separately from the HTML/CSS
  • Not Good for Large Images - Increases page size significantly for photos or large graphics
  • Harder to Update - Changing an image requires re-encoding and updating the code
  • Code Bloat - Makes HTML/CSS files larger and harder to read
  • No Lazy Loading - All base64 images load immediately, can slow initial page load

Use Base64 When:

  • Images are small (under 10KB): icons, logos, buttons
  • Creating email signatures or newsletters
  • Building single-file demos or prototypes
  • Reducing HTTP requests for above-the-fold content
  • Images change rarely and need to be bundled with code

Avoid Base64 When:

  • Images are large (photos, banners over 50KB)
  • The same image is used multiple times on different pages
  • Images need to be updated frequently
  • You need responsive images with multiple resolutions (srcset)
  • Building performance-critical production websites

Real-World Use Cases:

  • Email Signatures - Company logos that display without "show images" prompts
  • Small UI Icons - Navigation icons, social media badges (16x16, 24x24 pixels)
  • Loading Spinners - Animated GIFs that appear instantly while content loads
  • Data Visualization - Embedded charts and graphs in reports
  • CSS Patterns - Repeating background textures and decorative elements
  • Offline-First Apps - PWAs that need to function without network connectivity
  • Code Documentation - Technical docs with inline diagrams and screenshots

Performance Rule of Thumb:

Base64 is fastest for images under 5KB. Between 5-10KB, test both methods. Above 10KB, traditional image files with caching will usually perform better. Remember: smaller files = faster websites!

2. How to convert images

Converting images to base64 is simple and fast. Visit the home page and either:

  • Drag & drop your images directly into the browser window
  • Click the upload button to select files from your computer

Once uploaded, each file will show a status indicator:

  • Green row = Successfully encoded
  • Red row = Error occurred (see error message on the right)

Batch Processing: You can upload up to 20 files at once!

3. Use base64 as image sources

You can use the base64 encoded string directly in your HTML <img> tag's src attribute using the data:image/... format.

To get the code, click the copy image button and it will be automatically copied to your clipboard.

Basic inline image:

HTML
<!-- Simple inline image -->
<img src="data:image/png;base64,iVBORw0KGgoAAAANS..." alt="Logo">

Responsive image with dimensions:

HTML
<!-- Set width and height to prevent layout shift -->
<img
  src="data:image/png;base64,iVBORw0KGgoAAAANS..."
  alt="Company Logo"
  width="200"
  height="50"
  loading="lazy"
>

Email signature with base64 logo:

HTML
<!-- Email signature - works without "show images" prompt -->
<table cellpadding="0" cellspacing="0" border="0">
  <tr>
    <td style="padding-right: 15px;">
      <img src="data:image/png;base64,iVBORw0KGgo..."
           alt="Logo" width="80" height="80"
           style="border-radius: 8px;">
    </td>
    <td>
      <strong>Jane Doe</strong><br>
      Software Engineer<br>
      jane@example.com
    </td>
  </tr>
</table>

Favicon using base64:

HTML
<!-- Inline favicon - no extra HTTP request -->
<link rel="icon" href="data:image/png;base64,iVBORw0KGgo..." type="image/png">

JavaScript - dynamically set image source:

JavaScript
// Set image source dynamically
const logo = document.getElementById('logo');
logo.src = 'data:image/png;base64,iVBORw0KGgoAAAANS...';

// Create an image element from base64
const img = new Image();
img.src = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0i...';
img.alt = 'Dynamic icon';
document.body.appendChild(img);

4. Use base64 as CSS background

Base64 encoded images also work perfectly as CSS background images. Simply use the url() function with the data:image/... format.

Click the copy CSS button to get the CSS code copied to your clipboard.

Simple background image:

CSS
/* Basic background image */
.hero-section {
    background-image: url('data:image/jpeg;base64,/9j/4RiDRXhpZgAATU...');
    background-size: cover;
    background-position: center;
}

Repeating pattern / texture:

CSS
/* Repeating background pattern */
.textured-bg {
    background-image: url('data:image/png;base64,iVBORw0KGgo...');
    background-repeat: repeat;
    background-size: 20px 20px;
}

Icon in a button using pseudo-element:

CSS
/* Base64 icon inside a button */
.btn-download::before {
    content: '';
    display: inline-block;
    width: 16px;
    height: 16px;
    margin-right: 8px;
    background: url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0i...') no-repeat center;
    background-size: contain;
    vertical-align: middle;
}

Multiple backgrounds with fallback:

CSS
/* Layered backgrounds with a base64 overlay */
.card {
    background:
        url('data:image/png;base64,iVBORw0KGgo...') top right no-repeat,
        linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    background-size: 120px auto, cover;
}

CSS custom property with base64:

CSS
/* Store base64 in a CSS variable for reuse */
:root {
    --icon-checkmark: url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0i...');
}

.success-badge::after {
    content: '';
    width: 14px;
    height: 14px;
    background: var(--icon-checkmark) no-repeat center;
    background-size: contain;
}