Base64 in CSS background-image

The background-image property is the most common place to use Base64 data URIs in CSS. Instead of a file path, pass the whole data URI to url().

Example

.icon-search {
  width: 16px;
  height: 16px;
  background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAK...");
  background-repeat: no-repeat;
  background-size: contain;
}

/* multiple layers: inline icon on top of a gradient */
.badge {
  background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAK..."), linear-gradient(#fff, #eee);
}

Live demo

Notes

  • Always quote the data URI inside url(): SVG data in particular may contain characters that break unquoted URLs.
  • Inline only small images (icons, patterns). Each inlined image increases the CSS size by ~33% of the image size and blocks rendering until the CSS loads.
  • Data URIs in CSS are cached together with the stylesheet, so they are a good fit for icons used on many pages.

Other CSS properties

Related tools and guides

Comments (0)

Questions, ideas or a bug in “Base64 in CSS background-image”? Leave a comment, rate the tool, or reply to others.

No comments yet. Be the first to share your feedback!

Leave a comment

Rate this tool:
Esc
↑ ↓ move↵ openEsc close