CSS Image Sprites
CSS Image Sprites
Search w3schools.c Select Language HOME HTML C SS JAVASC RIPT JQUERY XML ASP.NET PHP SQL MORE...
R EFER ENC ES | EXAMPLES
Get Certified
Study Web Technologies and get a diploma at w3schools.com
CSS Basic
CSS HOME CSS Introduction CSS Syntax CSS Id & Class CSS How To
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.
CSS Styling
Styling Backgrounds Styling Text Styling Fonts Styling Links Styling Lists Styling Tables
Example
i m g . h o m e { w i d t h : 4 6 p x ; h e i g h t : 4 4 p x ; b a c k g r o u n d : u r l ( i m g _ n a v s p r i t e s . g i f )00 ; }
Try it yourself 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.
CSS Advanced
CSS Grouping/Nesting CSS Dimension CSS Display CSS Positioning CSS Floating CSS Align CSS Pseudo-class CSS Pseudo-element CSS Navigation Bar CSS Image Gallery CSS Image Opacity CSS Image Sprites CSS Media Types CSS Attribute Selectors CSS Summary
CSS Examples
CSS Examples CSS Quiz CSS Certificate
Example
# n a v l i s t { p o s i t i o n : r e l a t i v e ; } # n a v l i s tl i { m a r g i n : 0 ; p a d d i n g : 0 ; l i s t s t y l e : n o n e ; p o s i t i o n : a b s o l u t e ; t o p : 0 ; } # n a v l i s tl i ,# n a v l i s ta { h e i g h t : 4 4 p x ; d i s p l a y : b l o c k ; } # h o m e { l e f t : 0 p x ; w i d t h : 4 6 p x ; } # h o m e { b a c k g r o u n d : u r l ( ' i m g _ n a v s p r i t e s . g i f ' )00 ; } # p r e v { l e f t : 6 3 p x ; w i d t h : 4 3 p x ; } # p r e v { b a c k g r o u n d : u r l ( ' i m g _ n a v s p r i t e s . g i f ' )4 7 p x0 ; } # n e x t { l e f t : 1 2 9 p x ; w i d t h : 4 3 p x ; } # n e x t { b a c k g r o u n d : u r l ( ' i m g _ n a v s p r i t e s . g i f ' )9 1 p x0 ; }
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.
CSS References Related Searches: PHP Tutorials CSS Templates W eb Design Templates Create HTML Pages Learn Basic HTML AJAX Programming SQL Server Database Free HTML Tutorial
CSS Reference CSS Selectors CSS Reference Aural CSS Web Safe Fonts CSS Units CSS Colors CSS Color Values CSS Color Names CSS Color HEX
www.w3schools.com/css/css_image_sprites.asp
1/3
8/4/13
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
# h o m ea : h o v e r { b a c k g r o u n d :u r l ( ' i m g _ n a v s p r i t e s _ h o v e r . g i f ' )04 5 p x ; } # p r e va : h o v e r { b a c k g r o u n d :u r l ( ' i m g _ n a v s p r i t e s _ h o v e r . g i f ' )4 7 p x4 5 p x ; } # n e x ta : h o v e r { b a c k g r o u n d :u r l ( ' i m g _ n a v s p r i t e s _ h o v e r . g i f ' )9 1 p x4 5 p x ; }
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
Next Chapter
www.w3schools.com/css/css_image_sprites.asp
2/3
8/4/13
Top 10 Tutorials
HTML Tutorial HTML5 Tutorial C SS Tutorial C SS3 Tutorial JavaScript Tutorial jQuery Tutorial SQL Tutorial PHP Tutorial ASP.NET Tutorial XML Tutorial
Top 10 References
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
Examples
HTML Examples C SS Examples XML Examples JavaScript Examples HTML DOM Examples XML DOM Examples AJAX Examples ASP.NET Examples Razor Examples ASP Examples SVG Examples
Quizzes
HTML Quiz HTML5 Quiz XHTML Quiz C SS Quiz JavaScript Quiz jQuery Quiz XML Quiz ASP Quiz PHP Quiz SQL Quiz
Color Picke
Statistics
RE P O RT E RRO R
HO ME
TO P
P RI N T
FO RU M
A BO U T
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 and privacy policy. C opyright 1999-2013 by Refsnes Data. All Rights Reserved.
www.w3schools.com/css/css_image_sprites.asp
3/3