twolfson / grunt-spritesmith

Grunt task for converting a set of images into a spritesheet and corresponding CSS variables
MIT License
1.14k stars 92 forks source link

:hover styles #87

Closed davidmatas closed 10 years ago

davidmatas commented 10 years ago

Is there a way to generate :hover styles from the name of the image?

twolfson commented 10 years ago

Selectors for the css template can be defined via cssOpts.cssClass. For adding hover to all selectors, this can be done with:

cssOpts: {
  cssClass: function (item) {
    // Name CSS selector `.sprite-home:hover` for `home.png`
    return '.sprite-' + item.name + ':hover';
  }
}
yuyokk commented 9 years ago

Hi Todd,

I'm using the version 4.6.1 and I have the following task in the Gruntfile.js

sprite: {
  icons: {
    src: 'styles/img/sprites/icons/*.png',
    dest: 'styles/img/icons.png',
    destCss: 'styles/css/icons.css',
    cssTemplate: 'styles/img/sprites/sprite.mustache',
    cssOpts: {
      cssClass: function (item) {
          return '.sprite-' + item.name + ':hover';
      }
    }
  }
},

but still does not see the hover state in the resulted css. Can you please clarify what I'm doing wrong.

Thank you!

twolfson commented 9 years ago

@yuyokk There are a few things wrong with your setup:

To remedy your issue, the clearest options are:

sprite: {
  icons: {
    src: 'styles/img/sprites/icons/*.png',
    dest: 'styles/img/icons.png',
    destCss: 'styles/css/icons.css',
    cssOpts: {
      cssClass: function (item) {
          return '.sprite-' + item.name + ':hover';
      }
    }
  }
},
yuyokk commented 9 years ago

@twolfson

this solution works for me.

Stop using a custom CSS template

Thank you for the reply. I appreciate it!

Bogdaan commented 9 years ago

Thank, useful feature