CSS Data URI Generator
Convert images to CSS Data URIs for inline embedding in stylesheets.
Code Examples
Learn how to use CSS Data URIs in your projects
JavaScript - Dynamic Background
// Convert image file to Data URI and apply as background
const fileInput = document.querySelector('#imageInput');
fileInput.addEventListener('change', (e) => {
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = (event) => {
const dataUri = event.target.result;
// Apply as background image
document.body.style.backgroundImage = `url('${dataUri}')`;
};
reader.readAsDataURL(file);
});CSS - Inline Background Image
.logo {
background-image: url('data:image/png;base64,iVBORw0KGgoAAAANS...');
background-size: contain;
background-repeat: no-repeat;
width: 200px;
height: 100px;
}
/* Alternative: Using shorthand */
.icon {
background: url('data:image/svg+xml;base64,PHN2ZyB4bWxuc...')
no-repeat center center;
background-size: 24px 24px;
}CSS - Pseudo-elements with Content
.decorative-element::before {
content: url('data:image/svg+xml;base64,PHN2ZyB3aWR0aD0...');
display: inline-block;
margin-right: 8px;
}
.custom-bullet::before {
content: url('data:image/png;base64,iVBORw0KGgoAAAANS...');
position: absolute;
left: 0;
top: 4px;
}HTML - Inline Image Tag
<!-- Using Data URI in img src -->
<img
src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA..."
alt="Logo"
width="200"
height="100"
/>
<!-- Using in srcset for responsive images -->
<img
srcset="data:image/webp;base64,UklGRiQAAABXRUJQVl... 1x,
data:image/webp;base64,UklGRkQAAABXRUJQVl... 2x"
alt="Responsive icon"
/>How to Generate CSS Data URIs
- 1Upload an image file (PNG, JPG, GIF, SVG, or WebP).
- 2View the file information and image preview.
- 3Choose from multiple CSS output formats.
- 4Click copy to use the Data URI in your stylesheet.
Key Features
Multiple CSS Formats
Generate Data URIs for background-image, background, content, mask-image, and border-image properties.
Image Preview
See a real-time preview of your image before generating CSS code.
Size Optimization Tips
Get warnings for large files that might impact performance.
Auto-detect Dimensions
Automatically detect and display image width and height.
Common Use Cases
Reduce HTTP Requests
Embed small images directly in CSS to reduce the number of HTTP requests and improve page load time.
CSS Sprites Alternative
Use Data URIs as a modern alternative to traditional CSS sprite techniques.
Icon Font Replacement
Replace icon fonts with inline SVG Data URIs for better control and accessibility.
Email Templates
Embed images in CSS for HTML email templates that work without external resources.
Single-file Components
Create self-contained CSS components that include all necessary assets inline.
Offline Applications
Build web apps that work offline by embedding all images as Data URIs.
Best Practices
When to Use Data URIs
Best for small images (< 10KB) like icons, logos, and decorative elements. Avoid for large photos or frequently changing images.
Performance Considerations
Data URIs increase CSS file size by ~33% due to Base64 encoding. Large Data URIs can block rendering and are not cached separately.
Browser Caching
Since Data URIs are embedded in CSS, they are cached with the stylesheet. This is good for small, static assets but bad for frequently updated images.
SVG Optimization
For SVG files, consider using inline SVG with URL encoding instead of Base64 for better compression and readability.
Frequently Asked Questions
What is a CSS Data URI?
A Data URI is a way to embed data directly into CSS or HTML without requiring a separate file. It uses the format "data:[MIME-type];base64,[encoded-data]" to include images, fonts, or other resources inline.
Why are Data URIs larger than the original file?
Base64 encoding increases file size by approximately 33% because it converts binary data into ASCII characters. However, this can be offset by reduced HTTP requests and improved compression when the CSS is gzipped.
Should I use Data URIs for all images?
No. Data URIs are best for small, frequently used images like icons and logos. For large images, photos, or images that change often, use regular image files with proper caching.
Do Data URIs work in all browsers?
Yes, all modern browsers support Data URIs. Even Internet Explorer 8+ supports them, though IE8 has a 32KB size limit per Data URI.
How do Data URIs affect page performance?
Data URIs reduce HTTP requests but increase CSS file size. For small images, this usually improves performance. For large images, it can slow down initial page rendering since the entire CSS file must be downloaded before rendering begins.
Can I use Data URIs in responsive images?
Yes, you can use Data URIs in srcset attributes and CSS media queries. However, be cautious with file sizes as mobile devices will download the entire CSS regardless of which image is displayed.