I'm new to CSS and I'm trying out image spriting in CSS. I have an sprite map at present. There are multiple CSS files which are cascaded from my main CSS using #import. Each of these CSS files defines the layout for various components. I need only one call to be made to my sprite image hence I need to play around with background-position.
Could anyone tell me a way such that I make only one call to my image and the rest of the CSS files just manipulate the position?
You will need to work with multiple classes.
In the first class (lets call it .sprite) you have to define the background-image.
.sprite {
background: transparent url(path/to/image) no-repeat 0 0;
height: 16px;
width: 16px; //assuming you are using this size for your icons...
}
Now you will have to define another class for every sprite image you will be using.
For example, you have an magnifier-icon, you will add a class .sprite .magnifier and there you will have to define your background-position.
.sprite.magnifier {
background-position: 45px 30px;
}
Now in your HTML you just call for <div class="sprite magnifier"></div> and your image will just one time loaded.
There are various sprite generators. Or you can create your own using some kind of image manipulation software.
You'll want to save all the images in one layout as a .png (generally) with sufficient spacing between elements.
Then depending on your individual needs you can set the background of say all elements, to be your sprite, but this would potentially cause problems.
It's far better to say ~
ul.foo li {
background:url(my-sprite.png);
}
ul.foo li.home {
background-position:0 10px;
}
ul.foo li.about {
background-position:10px 20px;
}
and style individual elements as required. The sprite will still be only loaded once.
You should not use sprites for lots of large images either - best suited for icons, buttons etc. See YouTube, Google, Stack Overflow for good examples.
Give your elements the same class of sprite then override them:
.sprite { background: transparent url(../images/sprite.png) no-repeat top left; }
.add_icon { background-position: 20px 60px; }
.next_icon { background-position: 40px 60px; }
<div class="sprite add_icon"> etc.
and so on..
Related
I have found a gorgeous, free SVG file with all 52 poker cards.
Project
Image
Raw content of the SVG
I'd like to use it in a web based game to display the sprites of the cards.
Can I "split" this file into icons using css ?
I've tried following this tutorial but it requires to edit the SVG file to define the elements inside a def tag, which I would prefer to avoid (SVG is huge : 2MB).
I've also tried using the viewbox/viewport attributes with no success.
Note : I've also found a cards font but the quality of the rendering is so much worse ...
Here is a simple suggestion where background-position is used to move the wanted area of an image into view
div {
display: inline-block;
border: 1px solid;
height: 355px;
width: 260px;
background-image: url(https://cdn.rawgit.com/tyrcho/fa8bec7cc670515cd250a9dcfad5898f/raw/2d6bb400ab12bf785a29ba06202cbec789617801/Color_52_Faces_v.2.0.svg);
}
.div1 {
background-position: -490px -1155px;
}
.div2 {
background-position: -490px -1530px;
}
<div class="div1"></div>
<div class="div2"></div>
I have a sprite that I want to use defining a class to and not an id:
I want to use the white one to show expansion option and black one to show expanded state. For non expanded state I have a class sprite-right and want to use sprite-expanded for expanded state. Can anyone guide me through this? I forgot pasting what I did...duh!
sprite-right
{
overflow:hidden;
width:16px;
height:20px;
cursor:pointer;
background:transparent no-repeat 0 0;
background-image:url('../images/arrows.gif');
}
It's pretty simple to set up. You first need to set a class for applying the image as a background. Then add specific classes for each icon. Then in your CSS you change the background position, height and width to match the location of each icon. Here is an example:
.icon {
background-image: url('path/to/image.png');
display: block;
}
.icon.sprite-right {
background-position: 0 0;
height: 10px; // You can change these for each sprite
width: 10px; // You can change these for each sprite
}
.icon.sprite-expanded {
background-position: -10px 0;
}
.icon.sprite-right:hover {
background-position: -20px 0;
}
.icon.sprite-expanded:hover {
background-position: -30px 0;
}
Then, as you add new sprites you simply adjust the position until you can see the icon and then adjust the height and width until you are not clipping the image.
There are many great tutorials out there if you do a Google search. I use this tool alot when dealing with simple sprites.
Check out this link: http://labs.engageinteractive.co.uk/nav-o-matic/
Here is a codepen I forked so I can understand sprites a little better.
http://codepen.io/daugaard47/pen/lntzE
Study the code and it will start making sense to you.
Use background positioning to move your sprites to the desired class/state.
Hope this helps a little.
This post should help : http://mindthesemicolon.com/using-css-sprites/
It explains how to create and use sprites, with a code pen example.
I'm first-time using Compass spriting. I wanted to have icons images (all are in little different in sizes) centered position. like the attached image
I'm using this setting
$icons-spacing:40px;
#import "icons/*.png";
#include all-icons-sprites;
the css i'm getting is (for example)
.icons-adventure {
background-position: 0 -608px;
}
It's not that as I required. I want to give more spacing from top and left.
You may want to check out this Github Gist: https://gist.github.com/adamlogic/3577147, which has helped me fix spriting issues in the past and also gain a better understanding of how spriting in Compass works.
Of particular interest to you may be the portion where the author mentions the following: (pasted here in case the Gist is removed)
"I took this a bit further by defining my own (sprite) mixin."
$spritemap-spacing: 50px
#import "spritemap/*.png"
=background-sprite($name, $repeat: no-repeat, $offset-x: 0, $offset-y: 0)
background-image: $spritemap-sprites
background-repeat: $repeat
+sprite-background-position($spritemap-sprites, $name, $offset-x, $offset-y)
// if no offsets given, set the dimensions of the element to match the image
#if $offset-x == 0 and $offset-y == 0
+sprite-dimensions($spritemap-sprites, $name)
"and how I'm using it"
// simplest case; sets the background image and dimensions of the element
h3
+background-sprite(ribbonfull)
// custom offset; does not set the dimensions of the element
h2
+background-sprite(ribbonend, no-repeat, 3px, 22px)
// repeating backgrounds are possible, too
#positions
+background-sprite(doubleline, repeat-x, 0, 45px)
And, the author's generated CSS:
h3 {
background-image: url('/images/spritemap-sb826ca2aba.png');
background-repeat: no-repeat;
background-position: 0 -405px;
height: 29px;
width: 295px; }
h2 {
background-image: url('/images/spritemap-sb826ca2aba.png');
background-repeat: no-repeat;
background-position: 3px -296px; }
#positions {
background-image: url('/images/spritemap-sb826ca2aba.png');
background-repeat: repeat-x;
background-position: 0 -751px; }
I’ve found two workarounds for this issue, both not perfect:
You can simply save the icon in your image editor with the necessary padding - it works if you want to use it only in one place, otherwise you have to create duplicates (which is why this doesn't always work).
Other solution is to use pseudoelements. Assuming is the element you want to add the background to and you’ve placed your icons in icons folder:
#import "icons/*.png";
el {
position: relative;
}
el:after {
content: "";
position: absolute;
left: 50%;
top: 50%;
#include icons-sprite(some_icon);
margin-top: - round(icons-sprite-height(some_icon) / 2);
margin-left: - round(icons-sprite-width(some_icon) / 2);
}
$icons-spacing defines the number of pixels that separates each image in the generated sprite map. I believe you want to adjust the $icons-position which adjusts (offsets) the generated background-position styles
First off, a good question...
When you give sprites in CSS, you will be able to generate classes with the .image-name. And this is how Compass sprites work. Your image will be appended to a big sprite image, and all the irregular images will be clubbed together in a grid manner.
Even though $icons-spacing gives you the ability to give some padding to the grid, it won't be that easy for you to put it in this case. So, going ahead with what is generated, we will do the following.
So, if you want something like the picture, you need to center the element, which has the Compass generated class.
Now say, you have adventure.png in it and it has generated this class:
.icons-adventure {
background-position: 0 -608px;
}
Now, if you want to make this centered, you can make this way.
<div class="border">
<i class="icons-adventure"></i>
</div>
And for the border class, give padding. So, what I mean here is, you have wrapped the .border on the .icons-adventure. Now, you need to give some padding and width to it.
.border {padding: 15px; width: 40px;}
Here, there's is no need of height, as the height is automatically taken care. Let me come with a fiddle for you to get a clear explanation.
If you know the size of your icon you could set a default height for all icons and give single icons an vertical offset of (default height - icon height)/2 and position horizontal with center:
$sprite-spacing: 50px;
#import "compass/utilities/sprites";
#import "sprite/*.png";
#include all-sprite-sprites;
$sprite: sprite-map("sprite/*.png", $spacing: 50px);
#include sprite-sprite(myicon);
#include sprite-background-position($sprite, myicon, center, 12px);
If you are using Bootstrap 3 just use the following code, its simple and clean,
SASS File
#import "compass";
$home-spacing : 10px;
$home-sprite-dimensions : true;
#import "web/images/home/*.png";
#include all-home-sprites;
And in the HTML/Slim File I just use the center-block provided by Twitter Bootstrap 3, My HTML helper,
=content_tag('div','',:class=>'home-save_money center-block')
Basically it just center align the images into the center of the div, all the answer above use some custom Mixins or a hack.
PS: Even if you dont use twitter bootstrap, just use the following CSS, that would do the trick,
display: block;
margin-left: auto;
margin-right: auto;
I hope this helps some one,
Is it possible to use CSS sprites for "foreground" images -- i.e. images that users are supposed to click on and interact with and maybe even print?
Instead of using the CSS background-image property. What would you use?
You can use a standard <img /> tag and put it in a container (like a <div />) with a limited height/width. Then use relative positioning or negative margins to control the position of the image.
I have solved this problem using img tags and using the object-fit and object-position properties in my css. Here's a sample of the html and css I used:-
HTML
<img src="<your image source>" class="sprite-icon sprite-icon-1 " />
CSS
.sprite-icon {
height: 20px;
width: 20px;
object-fit: none;
}
.sprite-icon-1 {
object-position: 0 0;
}
.sprite-icon-2 {
object-position: -20px 0;
}
Obviously, you need to change the position and the size parameters according to the sprite you are using. For a full working example, check out this fiddle
You can do this with less CSS like this:
.myClass { background: url(../Images/Sprite.png) no-repeat;
height: 20px;
width: 40px;
background-position: -40px 0;
display: block; }
.myClass:hover { background-position: -40px -20px; }
Whatever has the class class="myClass" will have just the image in it, nothing else. This can be a <a> an <input> or a normal <div>, whatever you want.
It's a background image...but it's the element you're looking at, nothing's in front of that background. Just because you're using background-image for the property doesn't mean it's not a foreground element...like a button you can click on. You can do that like this:
<input type="button" class="myClass" />
One primary requirement that cannot be handled by background images is for ARIA. All ARIA requirements will reject the use of background images for meaningful, navigational, and other 'informative' uses that a screen reader must interpret on behalf of a user with a disability. Being able to swap out a background image css statement for an img tag and some ARIA tagging whenever necessary is a critical feature in the current regulated development environment.
The answer to the original question is yes! It is possible to use the image that is displayed in a css background statement. But you must open the sprite image in an image editor and select out the portion that represents the sprite you want and save it as a separate image and reference it in an img tag.
The challenge is that often, these situations arise in a pre-built control library. Finding and altering the code in the library that selects and displays the background image is a little difficult, changing out the code is hard!
#Waughwaugh's answer https://stackoverflow.com/a/50715682/2733244 using object-fit and object-position is a simple and solid solution for this problem. Its only downside is that it won't support some older browsers. If you still need to target IE11 you can instead work with clip-path and negative margins:
.sprite {
width: 240px;
height: 20px;
}
.sprite-1 {
clip-path: polygon(60px 0, 80px 0, 80px 20px, 60px 20px);
margin-left: -60px;
margin-right: -160px;
}
Full demo: https://jsfiddle.net/wortwart/8omfcyxb/10/
Using "real" images instead of background is often semantically better (e.g. for icons) and can have benefits for accessibility: If the image has not loaded or was blocked by the user we still have <img>'s built-in alt description. Accessibility is more than just screenreaders ...
The best approach of course is to ditch CSS sprites and simply load the images separately with HTTP/2.
You can do this, but you have to use background images for sprites as you have to be able to set position. This can be used for links or whatever you want. Look at Googles sprite, they use it for there buttons on iGoogle: http://img0.gmodules.com/ig/images/v2/sprite0_classic.gif
I am familiar with CSS techniques to replace text with an image. For example, here are 9 of them: http://css-tricks.com/nine-techniques-for-css-image-replacement/
Are there any techniques for replacing images? Is there anyway to set the background of an image to an image and then hide or move the foreground of the image (the image src element).
I am trying to write a skin for a site that has an image that I want to replace. Thanks.
From how I understand it he's trying to do this in pure CSS, with no changes to HTML or JavaScript.
That is correct. I am adding a new stylesheet to an existing page. Let say I can not modify HTML or utilize javascript.
After a little bit of tinkering, I figured it out!
img.someclass {
background: url("NEW IMAGE URL") top right;
height: 0;
width: 0;
padding: 200px 550px 0 0; /* Insert actual image size (height width 0 0) */
}
This will make the height and width of the actual image 0, but will expand the box to fill the size of the image with padding. The only downside to this is it won't look perfect in older versions of Internet Explorer.
If you have an element surrounding the image, e.g. a DIV, you should be able to set a background image (along with no-repeat and a position) on it, then set the image to display:none.
Alternatively, here's a haphazard solution that seems to work. It positions the image off-screen, then uses the :after pseudo-element to set a background image. It should be workable, but you'll need to fiddle with the values to get it working right. It won't work in IE6 though.
<style>
img.test {
background: url('image_to_show.png') no-repeat right top;
position: relative;
left: -16000px;
}
img.test:after {
content: ".";
color: transparent;
display: block;
width: 16000px;
}
</style>
<img class="test" src="image_to_hide.png">
The best way to replace images is to set the background position. First create the two different images and put them one above the other in the same image. Say your skin element is 50x50 pixels, you'd create a 50x100 image.
Then use some code like this:
.skinElement1 {
background: #fff url("image.png") no-repeat 0 0;
}
.skinElement2 {
background: #fff url("image.png") no-repeat 0 -50px;
}
So to view the second image you move the background up by the required amount. You could either use javascript or your server-side code to set the appropriate class.
Maybe you can set an opacity of an element and then set the background to the image you want.
Musicfreak: I meant using TWO elements.
you will have assign different classes for the two states then write some javascript to have the image change upon an event.
for example:
.firsImage { background:transparent url(/images/someImage.jpg) no-repeat; }
.secondIMage { background:transparent url(/images/image2.jpg) no-repeat; }
HTML:
<div id="imageDiv" class="firstImage"> some content </div>
<a onclick="changeImage()">Change the image!</a>
Javascript:
function changeImage(){
var imageDiv = document.getElementById("imageDiv")
if ( imageDiv.className === "firsImage" )
document.getElementById("imageDiv").className = "secondImage"
else
document.getElementById("imageDiv").className = "firstImage"
}