Data URL Encoder
Convert files to base64 Data URLs for embedding in HTML and CSS.
About this tool
A Data URL is a special URL format that encodes file contents directly into a URL string, allowing you to embed images, fonts, and other resources inline in your HTML and CSS without separate HTTP requests. This tool converts any file into a base64-encoded Data URL, which is useful for reducing server requests, improving page load times for small assets, and simplifying deployment when you need self-contained markup.
To use the tool, simply upload a file or paste its content, and the encoder instantly generates a Data URL that you can copy and paste directly into your CSS background-image property, an HTML img src attribute, or other embed contexts. The tool automatically detects the file type and sets the correct MIME type in the Data URL, so your browser knows how to interpret the embedded content.
Data URLs work best for small files—typically under 100 KB—since embedding large files can bloat your HTML and CSS. They're especially valuable for web developers optimizing performance, designers embedding custom fonts, and teams building email templates or offline-first applications where external asset requests aren't practical.
Frequently Asked Questions
Code Implementation
import base64
import mimetypes
from pathlib import Path
def file_to_data_url(filepath: str) -> str:
"""Convert a file to a base64 Data URL."""
path = Path(filepath)
mime_type, _ = mimetypes.guess_type(filepath)
if mime_type is None:
mime_type = "application/octet-stream"
with open(path, "rb") as f:
data = f.read()
encoded = base64.b64encode(data).decode("utf-8")
return f"data:{mime_type};base64,{encoded}"
# Example usage
url = file_to_data_url("image.png")
print(url[:80] + "...")
print(f"Total length: {len(url)} chars")
# Embed in HTML
html = f'<img src="{url}" alt="Embedded image">'Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.