how to use css sprites?
Here is the complete code. Copy and paste in a notepad and save it as abc.html
. Save the two images below in the same folder as:image.gif
for big one and trans.gif
for small one.
<html>
<head>
<title>Image Sprites</title>
<style type="text/css">
img.sprite
{
width:46px;
height:44px;
background:url(image.gif) 0px 0px;
}
.sprite:hover
{
background: url(image.gif) 0px 44px;
}
</style>
</head>
<body>
<img class="sprite" src="trans.gif" />
</body>
</html>
- image.gif on imgur
- trans.gif on imgur (1x1 pixels)
In simple words, CSS sprites use only one image instead of many.
So instead of many image requests from server only a single request is sent.
How use CSS sprites?
HTML
<div class="container">
<ul>
<li id="belt-1"></li>
<li id="belt-2"></li>
<li id="belt-3"></li>
<li id="belt-4"></li>
</ul>
</div>
CSS
.container {
position: relative;
width: 600px;
height: 600px;
}
.container ul {
list-style: none;
margin: 0;
padding: 0;
}
.container ul li {
background-image: url(https://i.stack.imgur.com/SBxX4.png);
margin-bottom: 20px;
width: 150px;
height: 78px;
background-size: auto 286px;
background-position: 0 0;
}
.container ul li#belt-2 {
background-position-y: 210px;
}
.container ul li#belt-3 {
background-position-y: 123px;
height: 58px;
}
.container ul li#belt-4 {
background-position-y: 66px;
height: 58px;
}
Always Remember all images in CSS sprites, should have same canvas size
I create a basic example of CSS sprites, I hope this will help you.
use CSS sprites for list ( li ) background image
You can also use
li:before {
background:url(..) no-repeat -##px -##px;
width:##px;
height:##px;
display:block;
position:absolute;
content: " ";
top:##px;
left:##px;
}
and thus get an additional element added to the DOM and give that the background image.
How to use CSS sprites within a responsive design
You can use:
- generated content (
:before
/:after
) instead of real elements to keep code clean; background-size
property to zoom your background images;- Data URIs instead of sprites. Data URIs are supported by all current browsers (IE8 has 32KB limitation of maximum size for each file represented as a Data URI).
Using css sprites with javascript
.classA{
width:120px;
height:123px;
background-image:url('Images/newcampaign_sprite2.png');
background-position:0px 0px;
}
.classB{
width:160px;
height:145px;
background-image:url('Images/newcampaign_sprite2.png');
background-position:0px 0px;
}
var elem = document.getElementById('table_drawad');
if (pricing == 'Paid')
{
elem.className = 'classB';
}
else
{
elem.className = 'classA';
}
CSS: using image sprites with css pseudo classes :before and :after
Don't use content
to insert your image, as you cannot modify its position. Instead, set the content to " "
and add the sprite as a background image. You can then use the background-position
property to move the sprite to the correct position. Otherwise your example should be working just fine.
A working demo:
http://jsfiddle.net/RvRxY/1/
How to make an image link using CSS sprites
You can use the standard <a>
element to create links. Then with the css you can set the background image/position to each of them. Something similar (You have to learn how to do it):
<style>
.SocialNav > a {
background-image: ...;
}
.Facebook {
background-position: ...;
}
.Twitter {
background-position: ...;
}
</style>
<nav class="SocialNav">
<a href="https://" class="Facebook" aria-label="Facebook"></a>
<a href="https://" class="Twitter" aria-label="Twitter"></a>
</nav>
You have to elaborate my example with these resources:
- https://developer.mozilla.org/en-US/docs/Web/CSS/background-position
- https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Images/Implementing_image_sprites_in_CSS
- https://css-tricks.com/snippets/css/perfect-css-sprite-sliding-doors-button/
Related Topics
Are CSS Frameworks Really Worth Using
Using Props to Set '&:Hover' Background-Color
How to Change a Background Image Opacity Without Changing on Div Content
(Sass) How to Exclude My Imported Files to Compile to CSS Files
CSS Filter Cancels Element Position
How to Suppress Certain CSS Errors in Aptana Studio 3
Javafx CSS Border-Radius Issue
Bootstrap 3 Arrow on Dropdown Menu
Google Chrome CSS Doesn't Update Unless Clear Cache
Different CSS Transition-Delays for Hover and Mouseout
How to Have Multiple Columns That Consume 100% Height Using Twitter Bootstrap
How to Use a Data Attribute to Set a Background-Image in CSS
How to Create a Row of Elements of Equal Width Inside an Inline Container? Possibly Using Flexbox
CSS Menu - Keep Parent Hovered While Focus on Submenu
CSS Overlapping Elements & Opacity Issue