Should I embed images as data/base64 in CSS or HTML

Is this a good practice? Are there some reasons to avoid this?

It’s a good practice usually only for very small CSS images that are going to be used together (like CSS sprites) when IE compatibility doesn’t matter, and saving the request is more important than cacheability.

It has a number of notable downsides:

  • Doesn’t work at all in IE6 and 7.

  • Works for resources only up to 32k in size in IE8. This is the limit that applies after base64 encoding. In other words, no longer than 32768 characters.

  • It saves a request, but bloats the HTML page instead! And makes images uncacheable. They get loaded every time the containing page or style sheet get loaded.

  • Base64 encoding bloats image sizes by 33%.

  • If served in a gzipped resource, data: images are almost certainly going to be a terrible strain on the server’s resources! Images are traditionally very CPU intensive to compress, with very little reduction in size.

Leave a Comment