You are on page 1of 3

7/21/2014

HOME

CSS Image Sprites

HTML

CSS

JAVASCRIPT

SQL

PHP

JQUERY

XML

ASP.NET

MORE...

REFERENCES

EXAMPLES

FORUM

Search w3schools.com

CSS Tutorial
CSS HOME
CSS Introduction

Select Language

CSS Syntax
CSS Selectors
CSS How To
CSS Backgrounds

WEB HOSTING

CSS Image Sprites

UK Reseller Host

cPanel Hosting

CSS Text
CSS Fonts
CSS Links
CSS Lists
CSS Tables
CSS Box Model
CSS Border
CSS Outline
CSS Margin
CSS Padding
CSS Dimension
CSS Display
CSS Positioning

Previous

Next Chapter

Image Sprites
An image sprite is a collection of images put into a single image.
A web page with many images can take a long time to load and generates multiple server requests.
Using image sprites will reduce the number of server requests and save bandwidth.

WEB BUILDING

Download XML Ed

FREE Website BUIL

Free HTML5 Templ

W3SCHOOLS EXA

HTML, C SS, JavaSc


PHP, jQuery, XML,
ASP C ertification

Image Sprites - Simple Example


Instead of using three separate images, we use this single image ("img_navsprites.gif"):

SHARE THIS PA

CSS Floating
CSS Align
CSS Combinators
CSS Pseudo-class
CSS Pseudo-element
CSS Navigation Bar
CSS Image Gallery
CSS Image Opacity
CSS Image Sprites
CSS Media Types
CSS Attr Selectors

CSS3 Tutorial
CSS3 Introduction
CSS3 Borders

With CSS, we can show just the part of the image we need.
In the following example the CSS specifies which part of the "img_navsprites.gif" image to show:

Example
img.home {
width: 46px;
height: 44px;
background: url(img_navsprites.gif) 0 0;
}
Try it yourself

CSS3 Backgrounds
CSS3 Gradients
CSS3 Text Effects
CSS3 Fonts
CSS3 2D Transforms
CSS3 3D Transforms
CSS3 Transitions

Example explained:
<img class="home" src="img_trans.gif"> - Only defines a small transparent image because the src attribute cannot be
empty. The displayed image will be the background image we specify in CSS
width: 46px; height: 44px; - Defines the portion of the image we want to use
background: url(img_navsprites.gif) 0 0; - Defines the background image and its position (left 0px, top 0px)
This is the easiest way to use image sprites, now we want to expand it by using links and hover effects.

CSS3 Animations
CSS3 Multiple Columns
CSS3 User Interface

Image Sprites - Create a Navigation List

CSS Summary

We want to use the sprite image ("img_navsprites.gif") to create a navigation list.

CSS Examples

We will use an HTML list, because it can be a link and also supports a background image:

CSS Examples
CSS Quiz
CSS Certificate

CSS References

Example
#navlist {
position: relative;
}

CSS Reference
CSS Selectors
CSS Reference Aural
CSS Web Safe Fonts
CSS Units
CSS Colors
CSS Color Values
CSS Color Names
CSS Color HEX

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

WEB RESOURC

Web Statistics

Web Validation

#navlist li, #navlist a {


height: 44px;
display: block;
}
#home {
left: 0px;
width: 46px;
background: url('img_navsprites.gif') 0 0;

http://www.w3schools.com/css/css_image_sprites.asp

1/3

7/21/2014

CSS Image Sprites


}
#prev {
left: 63px;
width: 43px;
background: url('img_navsprites.gif') -47px 0;
}
#next {
left: 129px;
width: 43px;
background: url('img_navsprites.gif') -91px 0;
}
Try it yourself

Example explained:
#navlist {position:relative;} - position is set to relative to allow absolute positioning inside it
#navlist li {margin:0;padding:0;list-style:none;position:absolute;top:0;} - margin and padding is set to 0, list-style
is removed, and all list items are absolute positioned
#navlist li, #navlist a {height:44px;display:block;} - the height of all the images are 44px
Now start to position and style for each specific part:
#home {left:0px;width:46px;} - Positioned all the way to the left, and the width of the image is 46px
#home {background:url(img_navsprites.gif) 0 0;} - Defines the background image and its position (left 0px, top 0px)
#prev {left:63px;width:43px;} - Positioned 63px to the right (#home width 46px + some extra space between
items), and the width is 43px.
#prev {background:url('img_navsprites.gif') -47px 0;} - Defines the background image 47px to the right (#home
width 46px + 1px line divider)
#next {left:129px;width:43px;}- Positioned 129px to the right (start of #prev is 63px + #prev width 43px + extra
space), and the width is 43px.
#next {background:url('img_navsprites.gif') -91px 0;} - Defines the background image 91px to the right (#home
width 46px + 1px line divider + #prev width 43px + 1px line divider )

Image Sprites - Hover Effect


Now we want to add a hover effect to our navigation list.
The :hover selector is used to select elements when you mouse over them.
Tip: The :hover selector can be used on all elements, not only on links.
Our new image ("img_navsprites_hover.gif") contains three navigation images and three images to use for hover effects:

Because this is one single image, and not six separate files, there will be no loading delay when a user hovers over the
image.
We only add three lines of code to add the hover effect:

Example
#home a:hover {
background: url('img_navsprites_hover.gif') 0 -45px;
}
#prev a:hover {
background: url('img_navsprites_hover.gif') -47px -45px;
}
#next a:hover {
background: url('img_navsprites_hover.gif') -91px -45px;
}
Try it yourself

Example explained:
#home a:hover {background: transparent url('img_navsprites_hover.gif') 0 -45px;} - For all three hover images we
specify the same background position, only 45px further down

Previous

http://www.w3schools.com/css/css_image_sprites.asp

Next Chapter

2/3

7/21/2014

CSS Image Sprites

Top 10 Tutorials

Top 10 References

Top 10 Examples

Web Certificates

HTML Tutorial
HTML5 Tutorial
C SS Tutorial
C SS3 Tutorial
JavaScript Tutorial
jQuery Tutorial
SQL Tutorial
PHP Tutorial
ASP.NET Tutorial
XML Tutorial

HTML/HTML5 Reference
C SS 1,2,3 Reference
C SS 3 Browser Support
JavaScript
HTML DOM
XML DOM
PHP Reference
jQuery Reference
ASP.NET Reference
HTML C olors

HTML Examples
C SS Examples
JavaScript Examples
HTML DOM Examples
PHP Examples
jQuery Examples
XML Examples
XML DOM Examples
ASP Examples
SVG Examples

R EPO R T ER R O R

HO ME |

TO P

Color Picker

HTML C ertificate
HTML5 C ertificate
C SS C ertificate
JavaScript C ertificate
jQuery C ertificate
PHP C ertificate
XML C ertificate
ASP C ertificate

PR INT

FO R UM

ABO UT

ADVER TISE W ITH US

W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples
are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use,
cookie and privacy policy. C opyright 1999-2014 by Refsnes Data. All Rights Reserved.

http://www.w3schools.com/css/css_image_sprites.asp

3/3

You might also like