Вы находитесь на странице: 1из 3

8/4/13

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

CSS Image Sprites


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.

CSS Styling
Styling Backgrounds Styling Text Styling Fonts Styling Links Styling Lists Styling Tables

Image Sprites - Simple Example


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

CSS Box Model


CSS Box Model CSS Border CSS Outline CSS Margin CSS Padding 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
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

Image Sprites - Create a Navigation List


We want to use the sprite image ("img_navsprites.gif") to create a navigation list. We will use an HTML list, because it can be a link and also supports a background image:

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

CSS Image Sprites


#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') no-repeat -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
# 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

Build Your Powerful HTML Website for Free with Wix


Start building your own stunning website - programming free! Wix.com provides a free, easy-to-use online platform that lets you create and publish your own unique website. Its powerful editing tools & customizable website designs make building a beautiful website easy. Add eCommerce features, connect a custom domain and experience superior SEO results. Wix is your ultimate solution for creating an exquisite HTML website. Over 20 million users have created their website with Wix. Create yours now!

W3Schools' Online Certification


The perfect solution for professionals who need to balance work, family, and career building. More than 10 000 certificates already issued!

Get Your Certificate


The HTML Certificate documents your knowledge of HTML. The HTML5 Certificate documents your knowledge of advanced HTML5. The CSS Certificate documents your knowledge of advanced CSS. The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM. The jQuery Certificate documents your knowledge of jQuery.

www.w3schools.com/css/css_image_sprites.asp

2/3

8/4/13

CSS Image Sprites


The XML Certificate documents your knowledge of XML, XML DOM and XSLT. The ASP Certificate documents your knowledge of ASP, SQL, and ADO. The PHP Certificate documents your knowledge of PHP and SQL (MySQL).

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

Browser Stat Browser OS Browser Disp

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

Вам также может понравиться