data:image/png;base64,iVBORw0KGgo...直接用於 HTML、CSS、Markdown,或任何瀏覽器需要圖片 URL 的位置。Base64 圖片適合用在哪裡?
當圖片需要以文字傳遞時,Base64 很有用:展示頁、API 資料、郵件片段、文件說明和小型介面素材。
先選對輸出形態
Data URI 帶有 MIME 類型,可直接放進 HTML 或 CSS;Raw Base64 更適合 API 欄位、設定和單獨儲存。
iVBORw0KGgoAAAANSUhEUg...用於 JSON、API 參數、設定欄位,或 MIME 類型單獨保存的情境。常見實作方式
複製符合目標情境的程式碼形態,再用轉換器產生的結果替換範例值。
Embed an image in HTML
Good for tiny icons, portable demos, or documentation where the image should be self-contained.
<img src="data:image/png;base64,iVBORw0KGgo..." alt="Inline preview" width="120" height="120" />Use a Base64 image in CSS
Useful for small decorative assets, skeleton placeholders, and one-file prototypes.
.avatar-placeholder {
width: 48px;
height: 48px;
background-image: url("data:image/png;base64,iVBORw0KGgo...");
background-size: cover;
}Send images through JSON APIs
Use raw Base64 when the receiving service expects a text field instead of multipart upload.
{
"fileName": "logo.png",
"mimeType": "image/png",
"base64": "iVBORw0KGgoAAAANSUhEUg..."
}Inline image for email templates
Inline Data URIs can help with small fallback images, but always test client compatibility.
<img src="data:image/png;base64,iVBORw0KGgo..." alt="Brand mark" style="display:block;width:96px;height:auto;" />Keep docs and examples self-contained
Base64 keeps screenshots, placeholders, and tiny diagrams inside one Markdown or HTML file.
Debug payloads across languages
Raw Base64 is plain text, so it can be pasted into test fixtures, cURL commands, or logs.
curl -X POST https://api.example.com/upload \
-H "Content-Type: application/json" \
-d '{"mimeType":"image/png","base64":"iVBORw0KGgo..."}'什麼時候不建議用 Base64
Base64 會增加體積,也繞過普通圖片快取,不適合作為所有圖片的預設方案。
實用判斷規則
- 01用 Data URI
當結果要放進 HTML、CSS、Markdown、郵件模板或瀏覽器 URL 欄位。
- 02用 Raw Base64
當目標系統已有 MIME 類型、檔名和編碼內容等獨立欄位。
- 03用普通 URL
當圖片較大、重複使用,或需要 CDN 與瀏覽器快取。