CSSsprite: Photoshop script combines two images for CSS hover

The need

Professional web developers have been aware of CSS sprites or CSS rollovers/hovers since 2004 (The rollover term is taken from JavaScript and the hover term is taken from CSS). They are the background graphics that change position based on mouse hover thus eliminating the need for JavaScript image swaps and image caching since the “mouse on” and “mouse off” are one image. I have been using sprites for years. It is a more tedious process than clicking Swap image on a DreamWeaver drop down menu, but it produces more accessible and more clean code. The most tedious part of creating CSS sprites for menu navigation is aligning the static (in-active) and hover (active) images. This script solves this problem by taking two images and merging them into one. I hope this increases your productivity and creates a more standards-compliant web.

There are online CSS sprite generators out there. My Photoshop script is not as remarkable as these 20 image sprites, but it’s much simpler, and you can assign a keyboard shortcut to it. Also, all my sites are designed in Illustrator which is a slightly different work flow than most. I find that Photoshop is an awful layout program.

Download CSSsprite.jsx

Installation

A JSX script is similar to a Photoshop plugin or an action, but are commands in Photoshop’s JavaScript based script interface. Download and copy this script within the Presets>>Scripts folder of your Photoshop application folder:

  • OS-X: Macintosh HD/Applications/Adobe Photoshop CS3/Presets/Scripts/
  • Windows: Program Files/Photoshop CS/Presets/Scripts

The Script will appear under File>>Scripts>>CSSsprite on the menu. You can assign a keyboard shortcut to this script. I prefer Control+Command+S. This shortcut fits well with the Save for Web shortcut, Command+Alt+Shift+S.

Usage

Open two files (and only two files) with the same height and width (if they are different sizes, the offset of your hover button state will not be half of the height). Select the non-hover background image, making it active–thus making the hover background image de-active. Run the script from the Scripts menu mentioned above. This will place the hover background image on the bottom of the sprite. This script does not support adding a third state to the sprite, but you could add a third and fourth state. To do this, first combine the third and fourth state, and then combine that image with the normal/hover sprite.

For your information: States for this script

The following is a list of Photoshop commands that are executed in this script:

  • Convert Image Mode to RGB
  • Make horizontal guide at 100% of canvas height
  • Switch to inactive document (-1 in document array)
  • “Duplicate Layer. . .” Layer Menu via right click
  • Switch back to inactive document (+1 in document array)
  • Double canvas size vertically
  • Transform/Move top layer down (50% of canvas size)
  • Zoom In (twice)

Result
This gives you a two-layered Photoshop file with second layer the CSS hover layer. Save the file as GIF, PNG, or JPEG using File>>Save for Web.

Notes
Adobe Photoshop JavaScript is the only cross-platform scripting language for Photoshop. This script was only tested Photoshop CS3 Extended (Mac). Our firm does not use Photoshop for web layout–only for image manipulation. We use Illustrator for layout.

I would like to thank the creators of ScriptingListenerJS and ActionToJavaScript.

You may be interested in this PhotoShop script that creates a sprite from all the images in a folder.

8 Responses to “CSSsprite: Photoshop script combines two images for CSS hover”

  1. [...] 36. Stephen James:Designer, Thinking, Just plain Awesome Recent Post: CSSsprite: Photoshop script combines two images for CSS hover [...]

  2. [...] CSS Sprite: Photoshop Script Combines Two Images for CSS Hover This article presents a simple JSX Photoshop script for creating image sprites, and you can also assign a keyboard shortcut to it. [...]

  3. I owe you about an hour of my time! At least…. :) Thanks for this!!!

  4. wparena says:

    this is excellent script…going to mention in my next article

  5. This has been my favorite discovery in a while .. thank you for creating this!! Such a time saver!

  6. Rick says:

    Thanks for the useful script. From a clarity/SEO perspective, doesn’t “CSS rollover” better describe your script? I’m kind of surprised that the term “rollover” doesn’t even appear on the page (OK it does now).

  7. gjkh says:

    Fantastic, thanks for sharing, saved me a load of time.

Leave a Reply