question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Responsive sprite images?

See original GitHub issue

First of all, this is an awesome plugin. Really saves a lot of time and trouble.

I’m wondering- is there a way to use grunt-spritesmith to force images to scale according to the width of its parent container?

Right now, it seems like all of the values that are generated are pixel values, not percentages, so they never change size. This is especially troublesome when used with Bootstrap 3’s img-responsive class, as images will get cut off on mobile (or any smaller screen).

HTML markup:

<div class="col-md-2 col-sm-6 col-xs-6">
    <a href="#">
        <div class="sprite1 img-responsive"></div>
    </a>
</div>

Generated stylus code via grunt-spritesmith:

$sprite1_x = 0px;
$sprite1_y = 0px;
$sprite1_offset_x = 0px;
$sprite1_offset_y = 0px;
$sprite1_width = 360px;
$sprite1_height = 240px;
$sprite1_total_width = 360px;
$sprite1_total_height = 1440px;
$sprite1_image = '../images/logos_sprite.png';
$sprite1 = 0px 0px 0px 0px 360px 240px 360px 1440px '../images/logos_sprite.png';

Result on smaller displays: screenshot 2014-05-31 15 02 08

Any ideas on a solution? Thanks!

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
emitxyzcommented, Sep 3, 2014

Hey, great job on this! This has been a lifesaver as I’m working on a project that requires over 800 images that need to be made into sprites. However these sprites all need to be scaleable, and easily repackaged and distrubuted and I’m running into some problems getting this all automated.

I believe you are incorrect regarding your second argument for abandoning this issue, changing the background-position values to percentages would work exactly the same out of the box, but instead allow for re-sizing of the element, while pixel values do not allow for this. You would still set a pixel based height and width value on the actual .icon- elements (as the ‘default’), which could then be overwritten if you wanted it half size, quarter size, etc.

I don’t believe there is any benefit in using pixel values for the background-position, except for the ability to set pixel based offsets. However, you could instead allow for percentage-based offsets in the exact same way (albeit not as human readable). Additionally I can’t really think of an actual use-case for needing custom offsets? What are those meant for?

As I understand it, to get this working on my end all that’s needed is to update the mixins in the outputted SCSS/less/etc to divide the values by the total_height and total_width, however all the units are suffixed with px. I think these values would be generally easier to handle, and result in a smaller generated scss file if the suffix was added as part of one of the mixins instead. I also can’t seem to figure out how to pass the total_height and total_width values as variables into the scss, which would speed things up significantly. Any ideas?

Thanks

0reactions
denisz1commented, Feb 24, 2015

Hey all see a good approach here. Works well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Responsive Background Image Sprites - CSS Tutorial
We will be using pure CSS to make background image sprites responsive in this easy-to-use tutorial.
Read more >
css - responsive sprites / percentages - Stack Overflow
I've written a Responsive CSS Sprite Generator to take care of all the work for you. You can just upload a bunch of...
Read more >
Responsive CSS Sprite Generator
Responsive CSS Sprite Generator · 1: Upload Your Images. Select up to 20 files, total 2MB. · 2: Choose Options. Output Type: JPEG...
Read more >
How to create Responsive Retina CSS sprites
Learn how to create css sprites: Simple workflow; Minified images using pngquant algorithm; Serve high quality images for Retina / HighDPI devices ...
Read more >
Responsive Background Image Sprites In CSS - Weston Ganger
Using sprites is pretty sweet I think. You will probably need to make it responsive at one point. Heres how to do it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found