Hongmu Notes

📦 Image to Base64

Select a picture, read the directly usable Base64 (Data URI) string locally, and paste it into img, CSS or interface; the picture is not uploaded to the server.

Image to Base64

Supports PNG, GIF, JPG, SVG, PSD, BMP, ICO, TTF, WOFF, EOT and other formats

Instructions for use

Click "Click here to select file" to select a local image (supports PNG, JPG, GIF, SVG, WebP, BMP, ICO), the browser will immediately read it and convert it into a Base64 string to fill in the text box below; click "Copy" to remove the entire content. The conversion is one-time. After changing the image, the content of the text box will be completely replaced. Please copy or save the results that need to be retained.

✅ How to check the results

  • The string should start with data:image/ followed by the format name followed by ;base64,
  • Paste the entire paragraph into the browser address bar. If the image is displayed, it means the conversion is complete.
  • The length should be significantly longer than the original file (about 1.33 times). If it is too short, it may result in incomplete copying.

🔒 Data and privacy

Images are read and encoded locally by the browser, without going through the server, and are cleared when the page is closed. At the same time, remember that Base64 is only encoding, not encryption. The string can be restored to the original image by anyone. Do not use it to transmit sensitive images such as documents.

Image to CSS background Base64

Small images or textures should be written directly into CSS.

🎨 Standard writing

After getting the string, write it like this: background-image:url("data:image/png;base64,…");, and then use background-size and background-repeat to control the rendering. Note that the entire string must be enclosed in quotation marks, and no line breaks are allowed in the middle - many people wrap long strings for typesetting, and the style becomes invalid.

📄 Suggestions for putting in style files

  • Write the long string into a separate rule and add annotations to indicate which picture it corresponds to.
  • Multiple icons are placed at the end of the file for easy replacement.
  • When compressing and building, be careful not to double-escape the data URI.
  • Change the image file reference back when the size exceeds dozens of KB to avoid slowing down style loading.

🧪 Troubleshooting order

When the style does not take effect, check in this order: ① Whether the prefix is complete (data: image/png; base64, not a single character is missing); ② Whether the quotation marks are paired; ③ Whether a newline or space is inserted in the middle of the string; ④ Paste the entire paragraph to the address bar to test. If the picture can be displayed, it means there is no problem with the data itself. The problem lies in the CSS writing method.

All keywords and tutorials 12

❓ FAQ

Do I need to add quotes in CSS??
If necessary, write it as url("data:image/png;base64,…"), and no line breaks are allowed in the string.
How to check if the background image is not displayed??
First confirm that the prefix and quotation marks are complete, and that the string has no line breaks, and then paste the entire paragraph into the address bar to verify the data itself.
How to put in CSS background
Just write it as background-image:url("Entire Base64"); Be sure to keep the quotation marks and make sure the prefix is complete. If one character is missing, the image will not be displayed.