🍋
Menu
Web

Data URI

Data URI (Inline Data Scheme)

A URL scheme that embeds file content directly within HTML, CSS, or JavaScript using the format data:[mediatype][;base64],data, eliminating the need for a separate HTTP request to fetch the resource.

技術的詳細

Data URIs follow the syntax: data:[][;charset=][;base64],. They are commonly used for small images (icons, SVGs), CSS backgrounds, and font files to reduce HTTP round trips. However, data URIs cannot be cached separately by the browser, increase HTML/CSS file size by ~33% (base64 overhead), and are subject to size limits in some browsers (2MB in older IE, generally unlimited in modern browsers). For SVG, the URL-encoded variant (data:image/svg+xml,...) avoids the base64 overhead entirely.

```javascript
// Encode string to Base64
const encoded = btoa('Hello, World!');  // 'SGVsbG8sIFdvcmxkIQ=='

// Decode Base64 to string
const decoded = atob('SGVsbG8sIFdvcmxkIQ==');  // 'Hello, World!'

// File to Base64 Data URI
const reader = new FileReader();
reader.onload = () => console.log(reader.result);
// → 'data:image/png;base64,iVBORw0KGgo...'
reader.readAsDataURL(file);
```

関連フォーマット

関連ツール

関連用語