The Wayback Machine - https://web.archive.org/web/20240807002753/https://www.geeksforgeeks.org/css-image-sprites/
Open In App

CSS Image Sprites

Last Updated : 23 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

CSS image sprites are a single image that combines multiple images, used to reduce page loading time and bandwidth consumption.

Why Use Image Sprites?

Image sprites are used for two main reasons:

  1. Faster Page Loading: By using a single image, page loading time is significantly reduced.
  2. Reduced Bandwidth: Less data is consumed, resulting in faster loading and improved user experience.

Image sprites are generally used for designing a graphic social media bar or a navigation bar to make it more attractive and efficient at the same time. It is just a method in HTML and CSS to implement a more efficient way of putting images and designing web pages.

Used Image: icon image

How Image Sprites Work

Image sprites are implemented using HTML and CSS, where a single image is defined and then styled using CSS to display the desired image segment.

Example: This example shows the use of image sprites in CSS.

html
<!DOCTYPE html>
<html>

<head>
    <style>
        #navlist {
            position: relative;
        }

        #navlist li {
            margin: 0;
            padding: 0;
            list-style: none;
            position: absolute;
            top: 0;
        }

        #navlist li,
        #navlist a {
            height: 100px;
            display: block;
        }

        .gfg {
            width: 100px;
            left: 0px;
            background: url('https://media.geeksforgeeks.org/wp-content/uploads/icon.png') -0px -0px;
        }

        .gfg1 {
            width: 100px;
            left: 120px;
            background: url('https://media.geeksforgeeks.org/wp-content/uploads/icon.png') -0px -140px;
        }

        .gfg2 {
            width: 100px;
            left: 240px;
            background: url('https://media.geeksforgeeks.org/wp-content/uploads/icon.png') -300px -140px;
        }
    </style>
</head>

<body>
    <ul id="navlist">
        <li class="gfg"><a href="#"></a></li>
        <li class="gfg1"><a href="#"></a></li>
        <li class="gfg2"><a href="#"></a></li>
    </ul>
</body>

</html>

Output: sprite image

Use Cases for Image Sprites

Image sprites are commonly used for:

  • Graphic Social Media Bars
  • Navigation Bars
  • Icon Sets

CSS Image Sprites – FAQs

What are CSS image sprites?

CSS image sprites combine multiple images into a single image file. This technique is used in web development to manage multiple UI graphic elements efficiently, reducing the number of server requests and speeding up page loads.

How do CSS image sprites work?

In CSS image sprites, various images are arranged into one larger image at different positions. CSS is then used to display only the relevant part of the aggregate image for different elements on the webpage by adjusting the background-position property.

What are the benefits of using CSS image sprites?

The main benefits include reduced number of HTTP requests during page load, decreased overall file size of images, and faster site performance. Sprites also help in maintaining a cleaner HTML structure as multiple images are managed through a single file.

How do you create a CSS image sprite?

To create a CSS image sprite, you start by combining images into one single file using a graphic editor. Then, you reference the sprite image as a background image in CSS and use the background-position property to display only the needed section of the image for different elements.

Can CSS image sprites have hover effects?

Yes, CSS image sprites can be used for hover effects. By changing the background-position on hover, a different part of the sprite image is shown, which can simulate the effect of changing images on mouseover.



Previous Article
Next Article

Similar Reads

How to create and use CSS Image Sprites ?
In this article, we are going to learn how we can how to create and use CSS Image Sprites. CSS Image Sprites are nothing but a way to reduce the HTTP requests from the image resources. A CSS Image Sprite is a single image file containing all images on a document page. Image sprites are advantageous since image resources will only have to be loaded
3 min read
What are the benefits of using CSS sprites ?
In this article, we will know about the benefits of CSS Sprites, along with understanding their implementation through the examples. The CSS Sprites are a collection or set of image files combined into a single file that an HTML document can access. These images are then called into use within the HTML code to be shown on the website. A web page wo
6 min read
What is CSS sprites and how to implement them on a page ?
Let's understand how to implement CSS image sprites on a page. CSS Image Sprites are the set of images combined in a single image. It is used to decrease the download burden of the site and save bandwidth. So instead of downloading multiple images, we download a single image and use the different positions within the image for different purposes wi
3 min read
How to slice image specified by border-image-source in CSS ?
In this article, we will see how to add a border image as a slice image using CSS. To make the border slice image, the border-image-slice property is used. This property divides or slices an image specified by the border-image-source property. The border-image-slice property divides a given image into - 9 regions4 corners4 edgesA middle region The
2 min read
Image inside div has extra space below the image in CSS
Sometimes when you insert an image inside a div element, you might notice that there is extra space below the image. This can happen due to various reasons such as the image having a different aspect ratio than the div, or the div having padding or margin that affects the image placement. In this article, we will discuss different approaches to sol
5 min read
Convert an image into grayscale image using HTML/CSS
In this article, we will learn how to convert an image into a grayscale image using CSS properties. CSS provides a filter property that can be used to apply visual effects to an image. In our case, we can use this property to convert an image into a grayscale image. The grayscale() filter in CSS can take values between 0 and 1. Here's what these va
2 min read
How to specify an image as a server-side image-map in HTML ?
In image mapping, an image is specified with certain set of coordinates inside the image which act as hyperlink areas to different destinations. It is different from an image link since in image linking, an image can be used to serve a single link or destination whereas in a mapped image, different coordinates of the image can serve different links
1 min read
Which attribute is used to specify an alternate text for an image, if the image is not loaded properly in HTML ?
In this article, we will discuss the attribute that is used to specify an alternate text for an image, if an image is not loaded properly in HTML. The &lt;img&gt; alt attribute is used to specify the alternate text for an image. It is useful when the image is not displayed. It is used to give alternative information for an image. Syntax: &lt;img al
1 min read
How to Modify the Fill Color of an SVG Image when being Served as Background Image ?
In this article, we will see how to modify the fill color of an SVG image when being served as a background image. SVGs also known as scalable vector images, While being served as a background image we can change their background color easily. It has a number of approaches like using HTML only or using CSS or JavaScript. For starters let's look at
5 min read
What is the best way to replace image with default image on error in ReactJS ?
In this article, we will learn to replace the image with the default image when an error occurs in ReactJS. Sometimes, it happens that the image source URL is broken or empty. In this scenario, we can replace the current image source URL with the default image source URL to fix the image rendering error. Prerequisites:NodeJS or NPMReact JSSteps to
3 min read
three90RightbarBannerImg