Discover how to use Base64 in CSS. Base64 is a method of encoding binary data into ASCII characters, which can then be used to embed images or fonts directly into CSS code. This can help reduce the number of HTTP requests, but it must be used with caution because it can increase the size of your CSS file.
What is Base64 Encoding?
Base64 encoding is a technique for displaying digital data as text. It enables the conversion of binary data, like images or files, into a string of readable characters that may be quickly communicated across text-based protocols like HTTP or included into text-based publications like HTML and CSS.
Every three bytes of binary data in Base64 encoding are represented by four readable characters chosen from a list of 64 characters. This procedure entails segmenting the binary data into decimal values, translating those decimal values to the characters in the Base64 character set. The data that is produced is safe to utilize in a variety of situations where binary data might not be practical.

What is CSS?
Since anyone reading this article is probably familiar with the meaning of CSS, let’s get this over with quickly.
CSS, or Cascading Style Sheets, is a style sheet language used for describing the look and formatting of a document written in HTML. CSS is designed to enable the separation of content from presentation, including layout, colors, and fonts. This separation can improve content accessibility, provide more flexibility and control in the specification of presentation characteristics, and reduce complexity and repetition in the structural content.
What is DATA URI?
When working with Base64-encoded data in HTML, it’s extremely important to understand Data URIs. With the help of a Data URI (Uniform Resource Identifier), you may incorporate data right into the URL. This method is frequently used to embed binary data directly into HTML or CSS files, including photos, audio files, movies, and other types of data.
Data URIs are important for HTML because they give you a means to directly add Base64-encoded data in your markup. When specifying images or other resources in properties like src or background, where the content is typically assumed to be a URL, this is especially helpful. You can integrate the data directly into the HTML document rather than having to use separate files by using Data URIs.
The “data:” scheme often comes first in a Data URI, then the media type (such as image/png), and finally the Base64-encoded data itself.
Here’s an example of a Data URI:
data:image/png;base64,
After the comma, you have to enter the Base64 content.
Advantages and disadvantages of embedding Base64 in CSS
The practice of directly embedding Base64-encoded data into CSS files has a number of benefits and drawbacks that influence its suitability for various scenarios.
Advantages:
- Reduced HTTP Requests: By embedding assets like images within CSS using Base64 encoding, separate HTTP requests are eliminated, which can help streamline the loading process.
- Offline Accessibility: By including all necessary data within the stylesheet, resources encoded within CSS files ensure offline functionality.
- Simplified Management: Management is simplified because there is no need to manage separate files when images and assets are consolidated directly within the CSS.
Disadvantages:
- Increased CSS Size: Because Base64-encoded data is typically larger than its binary counterpart, CSS files grow in size, potentially affecting loading performance.
- Caching Issues: Even minor changes to resources necessitate downloading the entire CSS file, which can reduce caching efficiency.
- Page Load Impact: Larger CSS files may result in slower page loading times, especially on networks with limited bandwidth.
- Maintenance Difficulty: Embedding resources within CSS can make maintenance more difficult because updates necessitate changes to the CSS file itself, potentially affecting version control.
- Browser Compatibility: Older browsers may struggle to handle large amounts of Base64-encoded data contained within CSS files.
How to Convert Data to Base64?
Many techniques exist for converting photos to Base64. Using online image-to-Base64 converters, which are easily accessible on several platforms, is one method. Additionally, a free online tool for Base64 conversion is available on our website.
As an alternative, you can implement the conversion using custom code. This entails reading the image file, using Base64 to encode its binary data, and then retrieving the encoded Base64 string. Here are examples of Base64 encoding and decoding in JavaScript.
Embedding Images with Base64 in CSS
Beyond its traditional use in background images, Base64 encoding can be used for a variety of other elements. Below, we’ll highlight a few key areas where Base64-encoded data can help improve the presentation and style of web content.
You can use Base64 data in CSS for the following elements:
- background-image: Sets the background image of an element. For example:
.my-element { background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAEFSURBVHhe7dExAcAgEMDALzo61r+zaoAFERnulgjI87/fHjLWLRGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBiSMnMAEuUC9McmgJQAAAAASUVORK5CYII=); }
- content: Can be used with the
::before
and::after
pseudo-elements. For instance:
.my-element::before { content: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAEFSURBVHhe7dExAcAgEMDALzo61r+zaoAFERnulgjI87/fHjLWLRGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBiSMnMAEuUC9McmgJQAAAAASUVORK5CYII=); }
- cursor: Allows you to define a custom cursor. For example:
.my-element { cursor: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAEFSURBVHhe7dExAcAgEMDALzo61r+zaoAFERnulgjI87/fHjLWLRGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBiSMnMAEuUC9McmgJQAAAAASUVORK5CYII=), auto; }
- list-style-image: Sets the list item marker image. For instance:
ul { list-style-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAEFSURBVHhe7dExAcAgEMDALzo61r+zaoAFERnulgjI87/fHjLWLRGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBiSMnMAEuUC9McmgJQAAAAASUVORK5CYII=); }
- border-image: Used for creating decorative borders. For example:
.my-element { border-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAEFSURBVHhe7dExAcAgEMDALzo61r+zaoAFERnulgjI87/fHjLWLRGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBgSY0iMITGGxBiSMnMAEuUC9McmgJQAAAAASUVORK5CYII=); }
There are also additional CSS elements where Base64 encoding can be used.