background-size with background-position doesn't scale the position?

前端 未结 6 1718
轮回少年
轮回少年 2021-02-05 13:16

I\'ve a sprite of 62 images of 91 * 91px, which makes the whole thing 91 * 5642 px. They\'re displayed in sort of a dynamic grid that grows and shrinks depending on user/pointer

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-05 14:11

    Many of the answers suggests percentage instead of static pixels in the background-pos, i however have a different approach. It's a bit wierd... but it works! <-- fiddle

    So i've added a span inside your link with the background attached to it. And instead of your inline background-position, ive instead used height in percent on the span, like this:

    
        
    
    

    And with this css it will be all you need and the image will fix the rest.

    a {
        display: block;
        overflow: hidden;
        width: 91px;
        height: 91px;
        position: relative;
    }
    a span{
        background: url(//hotblocks.nl/tests/gamessprite.gif) no-repeat 0 0;
        background-size: 100% auto;
        background-position: 0 0;
        position: absolute;
        display: block;
        bottom: 0;
        width: 100%;
    }
    a:hover {
        width: 120px;
        height: 120px;
    }
    

    This is more html and css that you need if you dont use the other solutions, but the more ways to solve something the better :)

提交回复
热议问题