The Wayback Machine - https://web.archive.org/web/20241126023852/https://www.geeksforgeeks.org/how-to-create-and-use-css-image-sprites/
Open In App

How to create and use CSS Image Sprites ?

Last Updated : 25 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 once.

Approach: At first we need to create an image that will have all the images we want to combine into one using any image editor. At first, we need to add all the divs or anchor tags, the number of them will be the same as the number of images in that sprite(generally). Then in the style part, we need to specify the background-position of the divs or anchor tags with the width or height of the images in the sprite. In this article, we have made a navbar using the image sprite below.

Properties used:

  • background: It is a property that is used to add a background to any element, it can be a simple color or it can even be an image.
  • background-position: This specifies the initial image position of the background image.
  • position: This property specifies the type of positioning an element will have. It takes values such as static, relative, absolute, fixed, or sticky.
  • display: This property defines how the element will be shown on the webpage.
  • left: This class is used to position elements horizontally, this class only works on an element whose position is set to some value.
  • width: This class is used to specify the total width of an element.

Example 1: The below example shows how we can use a combined image as an image sprite and make the webpage more efficient by reducing the HTTP requests for separate images.

HTML
<!DOCTYPE html>
<html>
<head>
    <style type="text/css">
        .sprite {
           background: url("sprite.png") no-repeat;
           width: 280px;
           height: 200px;
           display: inline-block;
        }
        .logo1 {
            left: 0px;
            width: 100px;
            background-position: 0px 0px;
        }
        .logo2 {
            left: 100px;
            width: 111px;
            background-position: -100px 0px;
        }
        .logo3 {
            left: 200px;
            width: 100px;
            background-position: -211px 0px;
        }
        .logo4 {
            left: 300px;
            width: 100px;
            background-position: -311px 0px;
        }
        body {
           text-align: center;
        }
        h1{
            color: green;
            text-align: center;
        }
    </style>
</head>
<body>
    <h1>GeeksForGeeks</h1>
    <div><h2>How to create and use CSS Image Sprites ?</h2></div>
    <div class="sprite logo1"></div>
    <div class="sprite logo2"></div>
    <div class="sprite logo3"></div>
    <div class="sprite logo4"></div>

</body>
</html>

Output:

Example 2: Here you can see how we can add separate anchor tags/links to the separate images in the image sprite.

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;
        }

        #home {
            left: 0px;
            width: 100px;
            background: url('text.gif');
            background-position: 0 0;
        }

        #prev {
            left: 100px;
            width: 200px;
            background: url('text.gif');
            background-position: -100px 0;
        }
        
        #next {
            left: 200px;
            width: 100px;
            background: url('text.gif');
            background-position: -200px 0;
        }
        h1{
            color: green;
        }
    </style>
</head>
<body>
    <h1>GeeksForGeeks</h1>
    <div><h2>How to create and use CSS Image Sprites ?</h2></div>
    <ul id="navlist">
    <li id="home"><a href=
  "https://www.geeksforgeeks.org"></a></li>
    <li id="prev"><a href=
    "https://www.geeksforgeeks.org/data-structures/?ref=shm">
      </a></li>
    <li id="next"><a href=
 "https://www.geeksforgeeks.org/fundamentals-of-algorithms/?ref=shm">
      </a></li>
    </ul>
</body>
</html>

Output:



Previous Article
Next Article

Similar Reads

CSS Image Sprites
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: Faster Page Loading: By using a single image, page loading time is significantly reduced.Reduced Bandwidth: Less data is consumed, resulting in faster load
3 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
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
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
A grayscale image is an image that displays only shades of gray, without any color, representing varying levels of light intensity. You can easily convert an image to grayscale in HTML/CSS by applying the filter: grayscale(100%) property to desaturate the colors. The grayscale() filter in CSS can take values between 0 and 1. Here's what these value
2 min read
How to use Tailwind CSS with Next.js Image ?
Integrating Tailwind CSS with Next.js enables you to style your application efficiently using utility-first CSS. When combined with the Next.js Image component, you can create responsive and optimized images with ease and consistency. In this article, we will learn how to use Tailwind CSS with Next.js Image. Prerequisite: NodeJS React Tailwind CSS
3 min read
How to use Flex to Shrink an Image in CSS ?
Shrinking an image in CSS means reducing its size to fit within a container. Using Flexbox, you can apply the flex-shrink property to control how much an image shrinks relative to other elements in a flex container, ensuring responsive resizing when space is limited. Syntaxflex-wrap: wrapNote: wrap is used to break the flex item into multiples line
1 min read
How to Create a Wave Image for a Background using HTML and CSS ?
This type of background creates uniqueness on your webpage by avoiding regular rectangular sized background or header. The following header design will show your creativity. This design can be achieved in two ways: Using ::before and ::after selector on a div element in CSS.Using SVG in HTML. Example: This example uses ::before and ::after selector
3 min read
How to create Image Folding Effect using HTML and CSS?
In this article, we are going to create an image folding effect below the main image. This is a simple project, we can achieve our target only by using HTML and CSS. Approach: Create the main div in which we are creating 4 list tags.Use of nth-child() selector property to give different styles to different list items. HTML Code: First, we create an
2 min read
How To Create a Thumbnail Image using HTML and CSS ?
The thumbnail image is used to add a 1px rounded border around the image. A thumbnail is a small size image that represents a larger image. The thumbnail is the image that is displayed as a preview of the video. It is used to represent what the video contains or what it is related to. It is displayed until the time the video is started. We will use
1 min read
How to create a Blur Mask Image Website using HTML CSS and JavaScript ?
In this article, we will see how to create a website with a blur mask image using HTML, CSS, and JavaScript. Generally, we often see this kind of effect on many websites. When clicking the button, the box immediately becomes hidden, displaying the blurred content. The website is responsive and works well on every screen size. Preview ApproachCreate
4 min read
How to create a Hero Image using HTML and CSS ?
A Hero Image is a large image with text, often placed at the top of a webpage. Hero images can be designed using HTML and CSS. This article contains two sections. The first section attaches the image and designs the basic structure. The second section designs the images and texts on the images. The hero image looks attractive when you are using it
2 min read
How to create swinging Image Hanger using HTML and CSS ?
In this article, we will learn how to create a type of Image Hanger that undergoes a swinging motion using HTML and CSS. This can be used to add interactivity to the page or highlight an image to draw attention. Approach: We will first create an HTML file in which we are going to add an image for our image hanger.We will then create a CSS style to
3 min read
How to Create Image Lightbox Gallery using HTML CSS and JavaScript ?
A lightbox gallery is basically used to view the gallery images in detail specifically. You can code the JavaScript to do so but also we can use some downloaded JS and CSS. In this article, we will download the lightbox and attach the JS and CSS files into our code. For our own design satisfaction, we can also use the CSS code as we want. We will d
3 min read
How to Create Image Accordion using HTML and CSS ?
In this article, we will see how to create an image accordion using HTML & CSS that is basically used for advertising purposes on e-commerce websites. An Accordion is often used to open multiple sections at full width & also content pushes the page content downward. Approach:Create an HTML file that will contain the different types of image
3 min read
How to Create a Simple Image Editor with CSS Filters and jQuery ?
CSS filter property is used to edit images or visual effects without the need for any specialized software. CSS filters work with a default value, so they don't offer users any control in changing their intensity.jQuery changes the value of intensity in CSS filters dynamically and thus gives the user complete control over the image.  Syntax:  .demo
3 min read
How to create responsive image gallery using HTML, CSS, jQuery and Bootstrap?
With the advent of new frameworks in web technologies, it has become quite easy to design and implement feature-rich and responsive web pages. Here, we are going to design a responsive image gallery using HTML, CSS, jQuery, and Bootstrap.  Features or Functionalities to implement: Responsive imagesResponsive Grid SystemImage viewer Prerequisites: B
3 min read
How to create Image Stack Illusion using HTML and CSS ?
In this article, we are going to create an illusion of images below the main image. It is the same as the image set of the gallery in an older version of Android. This is a simple project, we can achieve our target only by using HTML AND CSS. Overview of the project: Approach: Create a main div in which we are storing other 3 divs.Use the pseudo cl
4 min read
How to Create an Image Overlay Icon using HTML and CSS ?
Image overlay Icon can be an impressive addition to interactive detail or a set of features for your website. This article content will divide the task into two sections, the first section creating the structure and attach the link for the icon. In the second section, we will design the structure using CSS. Creating Structure: In this section, we w
3 min read
How to create image slider using HTML CSS and JavaScript ?
An image slide, or slideshow, is a dynamic display of images that automatically transitions from one to the next, often with animations. To create an image slide, use HTML to structure the images, CSS for styling and animations, and JavaScript to control the timing and transitions between images. ApproachHTML Structure: The HTML structure includes
3 min read
How to Create Image Magnifier using HTML CSS and JavaScript?
An image magnifier is a feature that allows users to zoom into specific parts of an image, simulating a magnifying glass effect. It’s created using HTML for structure, CSS for styling, and JavaScript to control the zoom functionality dynamically on hover or click. There are two methods to create an image magnifier: Table of Content Rollover/Follow
3 min read
Create Image Resize App using Tailwind CSS
Image Resizer is an application that is used to resize an uploaded image by providing new dimensions in the form of width and height. This application contains two numbered input fields to get the number input for width and height from the user. It also contains a file input field to upload an image file. It allows you to download the resized image
3 min read
How to Create an Image Overlay Fade in Text Effect on Hover using CSS ?
In the context of web design, it's all about making dynamic and interesting user experiences(UI). Adding picture overlay effects to the website is a good method to improve the aesthetic appeal, especially when users interact with it by hovering over images. This article will walk you through the steps to create a seamless image overlay fade-in text
3 min read
Create a Responsive Image Carousel in Tailwind CSS
Image Carousel is a UI element that acts like a slideshow by cycling through different images. You can easily design a Carousel in Tailwind CSS by using anchor tags along with proper width, height, and overflow properties. The Carousel can also be made responsive using the Tailwind responsive utility classes. Preview Image Tailwind CSS CDN Link<
2 min read
How to Create a Half Page Background Image with CSS?
A half-page background image is an image that covers half of the viewport height. This can be useful for creating visually distinct sections on a webpage, especially for headers or introductory sections. These are the different approaches to creating a half-page background image using CSS: Table of Content Using viewport height (vh) unitsUsing flex
2 min read
How to Create a Responsive Image Grid using CSS?
A responsive image grid is a layout technique used to display images in a grid-like structure that adjusts dynamically based on the screen size to ensure that the grid looks good and functions well across various devices and screen resolutions. Below are the approaches to creat a responsive image grid using CSS: Table of Content Using CSS GridUsing
3 min read
How to Create an Image Gallery with a Horizontal Scrollbar using CSS?
Image galleries are essential tools for showcasing multiple images within a single container on a website. They allow users to view a collection of images in an organized way. When we use the horizontal scrollbar, then we can reduce the space. In this tutorial, we will go through the steps on how to create an image gallery with a horizontal scrollb
4 min read
How to create a sticky image using CSS ?
A sticky image in CSS refers to an image that remains fixed within the viewport when scrolling, but only after it crosses a specified position. This effect is achieved using the `position: sticky;` property, combined with a specific top, right, bottom, or left value. For giving a sticky image effect to the web page two approaches can be used these
5 min read
Create an Image Background Remover App Using HTML CSS & JavaScript
Building an Image Background Remover using HTML, CSS, and JavaScript demonstrates the power of combining these technologies to create interactive and user-friendly applications. The clean design and intuitive features make this tool not only functional but also enjoyable for users to interact with. The goal of our project is to provide users with a
3 min read
three90RightbarBannerImg