While examining the source code of a greasemonkey userscript, I came across some interesting CSS:
.even { background: #fff url(data:image/gif;base64,R0lGODlhBgASALMAAOfn5+rq6uvr6+zs7O7u7vHx8fPz8/b29vj4+P39/f///wAAAAAAAAAAAAAAAAAAACwAAAAABgASAAAIMAAVCBxIsKDBgwgTDkzAsKGAhxARSJx4oKJFAxgzFtjIkYDHjwNCigxAsiSAkygDAgA7) repeat-x bottom}
This technique of embedding images directly into the CSS file intrigued me for several reasons:
- Reduces HTTP requests on page load, improving performance
- If no CDN is used, reduces traffic generated by cookies alongside images
- Allows for caching and GZIP compression of CSS files
Considering the cache issues IE6 has with background images, this approach seems quite appealing...
So, what are the pros and cons of this practice? Why might someone choose not to use it, and what tools can be utilized for base64 encoding images?
Update - Testing Results
Image test: - 133.6Kb
Test URL:
Dedicated CSS file: - 178.1Kb
GZIP encoding server-side
Size sent to client after GZIP (YSLOW components test): 59.3Kb
Data saved in client browser: 74.3Kb
Although slightly less effective for smaller images, the results are promising.
UPDATE: Bryan McQuade from Google's PageSpeed team mentioned at ChromeDevSummit 2013 that data:uris in CSS can be a render-blocking anti-pattern for delivering essential CSS efficiently. Check out his talk
at http://developer.chrome.com/devsummit/sessions for more details. Keep that perspective in mind - see the actual slide.#perfmatters: Instant mobile web apps