Reputation: 11876
I'm trying to use the background-position
property with percentages in an image sprite. It doesn't seem to work though i.e. it doesn't shift the background-image.
You can see an example here: http://jsfiddle.net/3UQYg/3/
When I use pixels, then the image does shift: See here http://jsfiddle.net/3UQYg/2/.
What am I doing wrong?!
Upvotes: 4
Views: 2871
Reputation: 5477
Actually, the percent background-position works like this:
0%
, then image's left side aligns container's left side. 100%
, then image's right side aligns with container's right side.Similarly,
0%
, then image's top side aligns container's top side. 100%
, then image's bottom side aligns with container's bottom.Upvotes: 8
Reputation: 277
Exactly.
Pixels don't work the same way than percentage in background-position. As a summary: background-position:30% 10% for example will align the point located at 30% 10% of your image with the 30% 10% axis of your cointaining DIV or HTML tag. Being not so obvious to calculate the percentage number.
Next article explains it all: http://www.sitepoint.com/css-using-percentages-in-background-image/
Upvotes: 0