About this tool
The Data URI scheme (RFC 2397) enables embedding images directly into HTML markup, CSS stylesheets, and JSON API payloads as Base64 strings. For tiny UI icons and badges, converting assets into Base64 Data URLs eliminates auxiliary HTTP request roundtrips and avoids unstyled layout flashes. This tool provides instant bidirectional conversion between image files and Base64 strings, outputting ready-to-use HTML, CSS, and Markdown tags.
How to Use This Tool (Step-by-Step)
Upload Image or Paste Data URL
Select a local image, drag and drop PNG, JPG, SVG, WebP, or GIF files into the workspace, or paste an existing Base64 Data URI.
Inspect Encoding & Dimensions
The engine transforms the binary stream into a standard Data URI, detailing raw size, encoded size (inflated ~33%), and pixel dimensions.
Copy HTML, CSS, or Raw Base64
Select your desired export syntax: Raw Base64, Full Data URI, HTML `<img src="...">`, or CSS `background: url(...)`. Copy with one click.
Multi-Format Export Snippets
Exports raw Base64, complete Data URIs, HTML `<img>` tags, and CSS background rules ready for instant pasting into IDEs.
Full SVG & Animated GIF Support
Transcodes raster assets alongside vector SVGs and multi-frame animated GIFs while preserving vector scaling and animation loops.
100% Client-Side Privacy
FileReader operations and Base64 transcoding execute 100% in local memory. Proprietary graphics assets never leave your device.
Base64 Inline Performance Tradeoffs & Tips
- •Cap inline images under 8KB–10KB: Base64 expands file size by roughly 33%. Inlining large photos inflates HTML/CSS bundles, hurting Core Web Vitals.
- •No independent browser caching: Standalone files enjoy aggressive HTTP caching. Inlined Base64 payloads re-download every time the containing HTML/CSS changes.
- •Inline raw SVG XML instead of Base64: Raw SVG elements yield smaller payloads than Base64 and allow dynamic CSS styling of fills and strokes.
Code & Transformation Examples
Data URL Structure Breakdown Benchmark
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==• data: : 协议方案前缀 (Scheme)
• image/png : 媒体 MIME 类型 (指出原始文件格式)
• ;base64 : 编码声明标记 (表明后续内容采用 Base64 编码)
• ,iVBORw0... : 真实的图片二进制 Base64 数据串* Assigning this entire string to an `<img src="...">` tag allows browsers to paint the image with zero network roundtrips.
JavaScript FileReader Base64 Implementation
function fileToBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.onerror = error => reject(error);
reader.readAsDataURL(file);
});
}// 监听 input[type=file] 的 change 事件
fileInput.addEventListener('change', async (e) => {
const base64Url = await fileToBase64(e.target.files[0]);
console.log("生成的 DataURL:", base64Url.substring(0, 50));
});* FileReader.readAsDataURL() is the browser-native standard API for converting File objects into Base64 strings.
Frequently Asked Questions
Q: Why is a Base64 encoded string ~33% larger than the image?
A: Base64 encodes 6 bits per character. Converting 3 bytes (24 bits) into 4 characters (4 × 6 = 24 bits) naturally creates a 4:3 (33.3%) data footprint overhead.
Q: How do I convert a Base64 string back into a downloadable image file?
A: Paste your Data URL string into the workspace. The tool renders the image preview immediately and provides a button to save the binary image file.
Q: Why do browsers sometimes fail on huge Data URLs in the address bar?
A: Browsers enforce maximum URL length restrictions in address bars (typically 2MB to 64KB). Overly massive Base64 strings exceed limits and get rejected.