I have a CSS sprite with various icons in size 32x32px.
Now what I need is to display one of those icons in 20x20px size. I tried a few things, like setting the size to 20px, using background-size:cover, or using transform:scale(0.625) - but none of my attempts give the result I desire.
HTML for my tests:
32px icons:
<div class="sprite" style="background-position:0px 32px;"> </div>
<div class="sprite" style="background-position:64px 32px;"> </div>
<div class="sprite" style="background-position:32px 96px;"> </div>
<div class="sprite" style="background-position:0px 0px;"> </div>
<div class="sprite" style="background-position:32px 64px;"> </div>
<hr>
20px icons (attempts):
<div class="sprite" style="background-position:32px 64px; width:20px; height:20px;"> </div>
<div class="sprite" style="background-position:32px 64px; width:20px; height:20px; background-size:cover;"> </div>
<div class="sprite" style="background-position:32px 64px; width:20px; height:20px; transform:scale(0.625);"> </div>
CSS for my tests:
.sprite {
background-image:url(http://buildnewgames.com/assets/article//dom-sprites/spritesheet.png);
width:32px;
height:32px;
}
Please have a look at this fiddle to see what I tried:
https://jsfiddle.net/dfqh0yrc/4/
You can only use the scale
without width
and height
like the following:
.sprite {
background:url(http://buildnewgames.com/assets/article//dom-sprites/spritesheet.png);
width:32px;
height:32px;
}
32px icons:
<div class="sprite" style="background-position:0px 32px;"> </div>
<div class="sprite" style="background-position:64px 32px;"> </div>
<div class="sprite" style="background-position:32px 96px;"> </div>
<div class="sprite" style="background-position:0px 0px;"> </div>
<div class="sprite" style="background-position:32px 64px;"> </div>
<hr>
20px icons (attempts):
<div class="sprite" style="background-position:32px 64px; transform:scale(0.625);"> </div>
<div class="sprite" style="background-position:32px 64px; transform:scale(calc(20 / 32));"> </div>
You can also use calc
to calculate the scale factor like the following:
<div class="sprite" style="background-position:32px 64px; transform:scale(calc(20 / 32));"> </div>
Demo on JSFiddle: https://jsfiddle.net/dfqh0yrc/5/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With