responsive sprites / percentages

前端 未结 9 808
天涯浪人
天涯浪人 2020-12-04 09:54

I\'ve read every single question about responsive sprites using css, I saw jsfiddle with working examples of responsive sprites, but I still cannot understand how to get the

相关标签:
9条回答
  • 2020-12-04 10:50

    I've written a Responsive CSS Sprite Generator to take care of all the work for you. You can just upload a bunch of images and it will give you a sprite image and the CSS for it.

    It uses a novel method for making the sprites responsive - a data src with a transparent PNG to make the image maintain its aspect ratio, so unlike other methods the images don't need to be square, or all the same ratio.

    0 讨论(0)
  • 2020-12-04 10:52

    This is a simpler solution, check this

    .my_picture{
        //target your sprite
        background: url(my_img.jpg) no-repeat;
    
        //Specify it full image
        backgound-size: 100%;
    
        //Position of the targeted picture
        background-position: left 0 bottom x%;
    
        //Zoom in or out on the position
        width: x%;
    
        //Scale height by playing with padding
        padding-bottom: x%;
    
        //Set height to 0 because of sprite size
        height: 0;
    }
    

    How does it work? To target any sprite pictures easily, we have to specify sprite size to original, “100%”. We will then target a picture position from corresponding bottom, with left 0.

    Because the sprite size is set to max 100%, we have to disable height, and the only option to set height now, is to play with padding-bottom, in percentage too.

    Your image is now fully responsive, just play with width values (in percentage), to zoom in or out, and that’s all, you have a fully responsive css sprite.

    Original article on my blog here: http://creativcoders.wordpress.com/2014/05/05/css-responsive-sprites/

    0 讨论(0)
  • 2020-12-04 10:54

    An update to @vals' answer. Some of his calcs didn't quite work for me.

    The background-size calcs worked, except that he was multiplying by 1000 instead of 100 to get the final percentage figures. So 12500% should be 1250% and so on. (Update: 10/2015 - it looks like @vals has corrected this in his answer.)

    The background-position X value calcs were very slightly out for me. They didn't match what was generated by spritecow.com (as per Adrian Florescu's suggestion). This is, I think, because absolute coordinates are calculated from the left of the background image, whereas with percentages, you have to calculate from the right of the background image. That being the case, you have to subtract the image width from the overall background width before you divide the absolute x-pos number with it.

    So instead of:

    x-part 173px / 1000px = 0.173 ->> 17.3%
    

    do this:

    x-part 173px / (1000px - 80px) = 0.1880434783 ->> 18.80434783%
    

    Where:

    1000px is the width of the background image (sprite)

    80px is the width of displayed image

    173px is the absolute x-coordinate of the displayed image.

    This is what works for me, anyway!

    0 讨论(0)
提交回复
热议问题