画像をデータURLにしてHTMLに埋め込む
- 選ぶ
- 印をつける
- 貼る
小さな画像を、別ファイルにせず、HTMLやCSSの中に直接埋め込みたい。そのときに使うのが、データURLです。ファイルとBase64を相互変換する の「データURLの形式にする(data:…;base64,)」で作れます。
手順
- 「向きを選ぶ」で、「ファイル → Base64」を選びます。
- 「ファイルを1つ選ぶ」で、画像のファイルを選びます。
- 「データURLの形式にする(data:…;base64,)」にチェックを入れます。
- 「変換後のBase64」に、「data:image/png;base64,…」のような文字列が出ます。
- 「結果をコピー」でコピーして、HTMLの画像のsrcに貼り付けます。
使い方の例
HTMLに貼り付けるときの形:画像のタグのsrcに、作った文字列を入れます。CSSの背景画像のurl()の中にも、入れられます。
データURLのメリット
- 画像を別ファイルとして、用意しなくてよい
- 画像の読み込みのための、通信が減る
- 1つのファイルで、完結する(メールのHTMLなど)
データURLのデメリット
- 文字数が、元の約1.3倍になる
- HTMLが長くなり、読みにくい
- ブラウザに、画像がキャッシュされない
- 同じ画像を、複数の場所で使うと、そのたびに文字が増える
向いている画像
- 小さなアイコン(数KB程度)
- 単色のロゴや、小さな装飾
- 背景のパターン
向かない画像
- 写真
- 大きな画像
- 何度も使う画像
目安の大きさ
一般には、数KB〜十数KB程度までの画像が、埋め込みに向くと言われます。それ以上は、別ファイルにするほうが扱いやすいです。
例
2KBの小さなアイコンを、メール用のHTMLに埋め込んだ。画像を添付しなくても、表示できるようになった。
つまずきやすい点
- メールで表示されない:メールソフトによっては、データURLの画像を表示しない。
- HTMLが長すぎて扱いにくい:大きな画像には、使わない。
- 種類が合っていない:先頭の「image/png」の部分が、画像の形式と合っているか。
よくある質問
画像以外でも、データURLにできますか?
できます。PDFやテキストなども、データURLにできます。ただし、使える場面は限られます。
まとめ
小さな画像だけを、データURLにして埋め込む。大きな画像は、別ファイルにしましょう。
ファイルとBase64を相互変換するを使う(無料)あわせて使えるツール
- ファイルのハッシュ値を調べる(SHA-256):ファイルのハッシュ値を調べて、照合します。
- CSVとJSONを相互変換する:CSVとJSONを、すぐに相互変換します。
- ファイルをZIPにまとめる:複数のファイルを、1つのZIPにまとめます。
同じツールの、ほかの解説
Embedding an Image in HTML as a Data URL
- Choose
- Tick
- Paste
You want to embed a small image directly in HTML or CSS without making it a separate file. A data URL is what you use. You can make one with "Use data URL format (data:…;base64,)" in Convert Files and Base64.
Steps
- For "Direction", choose "File → Base64".
- With "Choose one file", choose the image file.
- Tick "Use data URL format (data:…;base64,)".
- A string like "data:image/png;base64,…" appears under "Base64 after conversion".
- Copy with "Copy result" and paste it into the HTML image's src.
Example of use
The form for pasting into HTML: put the string you made in the src of the image tag. It can also go inside url() of a CSS background image.
Advantages of data URLs
- No need to prepare the image as a separate file
- Less communication for loading images
- Completes in one file (such as email HTML)
Disadvantages of data URLs
- The character count becomes about 1.3 times the original
- The HTML gets long and hard to read
- The browser cannot cache the image
- Using the same image in several places adds the text each time
Images that suit
- Small icons (a few KB)
- Single-colour logos and small decorations
- Background patterns
Images that don't suit
- Photos
- Large images
- Images used many times
A guide to size
Generally images up to a few KB to a dozen or so KB are said to suit embedding. Larger ones are easier to handle as separate files.
Example
Embedded a 2KB small icon in HTML for email. It could be displayed without attaching the image.
Common stumbling blocks
- Not displayed in email: some email software doesn't display data URL images.
- The HTML is too long to handle: don't use it for large images.
- The type doesn't match: check that the leading "image/png" part matches the image format.
Frequently asked questions
Can things other than images become data URLs?
Yes. PDFs, text and so on can also be data URLs. But the situations to use them are limited.
Summary
Make only small images into data URLs and embed them. Keep large images as separate files.
Use "Convert Files and Base64" (free)Tools that go well with this
- Check a File's Hash (SHA-256): Calculate a file's hash and compare it.
- Convert CSV and JSON: Convert between CSV and JSON instantly.
- Make a ZIP File: Pack several files into one ZIP.