Here’s the problem: I want to use CSS Sprites for transparent images, and they require different matte colors.
The promise of CSS Sprites is great: you can combine many small images into one bigger image, and use CSS to reveal only the revelant parts. But what if you have a background image, and the background changes color on :hover? When trying to save the smilies, you can only choose one matte color. I need multiple matte colors!
The issue really is a pain when you have already given your object a stroke. Using the Appearance panel, we’ll make everything happy in the world again!
My solution: Making a Multiple Matte Sprite (“MMM Sprite”)
In order to solve this issue, we must first declare it: one matte color is not enough! And if we can’t use one, we must use no matte. In order to reproduce the matte effect, we will use multiple strokes on an object in Illustrator. There is a similar solution for Photoshop, described in less detail below.
The MMM Sprite Process:
- Create as many objects as you’ll need (often just two, but for this example, I show three. Each smiley will have a different matte color.
- Select one of the smiley’s yellow circles, and go to the Illustrator Appearance Panel.
- In the upper-right corner of the Appearance panel, click the downward facing arrow. This brings up a contextual menu. Select “Add New Stroke”. Note: we’re doing it this way in case your object already has a stroke applied to it. You can apply multiple strokes to an object using this method.
- The stroke should be selected by default now, but if not, select the stroke in the Appearance Panel
- Give the stroke a color in the Color panel. For the first smiley, we want it to have a white stroke (for the white background).
- Go to the Stroke panel. In the Stroke panel (with the circle still selected, and the stroke selected in the Appearance Panel) align the stroke to the outside. This makes it so the object does not change.
- Give each smiley its own stroke color based on the background you have. Notice the smileys have the same background colors as the first graphic (white, blue, and red). You can use the eyedropper tool to accurately select the stroke color (be sure to hold down Shift to not affect the object’s fill!)
- When you have finished giving your objects the strokes they need, then Save for Web. In the GIF settings, turn on transparency, and choose ‘None’ for the Matte setting (!important)
- When you’re finished, you have a single GIF image with multiple Matted sprite images.
A summary:
This simple process allows you to further reduce the number of HTTP Requests that your site is making, just because of a silly matte issue. You can, by adding strokes, make any transparent GIF work in most situations.
But what about Photoshop?
In Photoshop, you can do a similar trick:
- Select your layer by Command/Ctr clicking your layer thumbnail in the Layers panel.
- Go to the menu bar and click on Select > Modify > Expand
- Expand by 1 pixel
- Save for Web with no matte
What are your solutions?
Do you have a better way? Share your methods below!
8 replies on “Multiple Matte Colors for CSS Sprites”
I’ve added the article to WikiHow.com :
http://www.wikihow.com/Creat-Multiple-Matte-Colors-for-Transparent-Gif%27s
It’s been nominated for featuring on the home page of wikiHow and wikiHow’s Google RSS feed. Congrats! I hope you choose to share more of your content with us.
[…] 3, 2008 · No Comments If you want to know more about CSS sprites and how to put them together, feast your eyes on […]
[…] site uses CSS sprites to achieve fast load time. There is some more improvement to be made (the post ratings stars, […]
FYI, I’ve created a Photoshop Script that combines two images of the same size to make a simple normal/hover sprite. It’s not as genius as the 20 image sprites you can make online, but it’s much simpler and you can assign a keyboard shortcut to it. I prefer the control of it.
http://www.smjdesign.com/designwell/archives/photoshop-script-combine-two-images-css-hover-css-sprite/
@Stephen – I took a look at your script, and it wouldn’t do much for me…I create navigation and buttons on the same Photoshop file.
Check out the sprite image for the SteveSpangler.com website.
@Zack So what do you do if you want to make one of the images bigger? Do you create all new CSS rules and sprite image?
I often set up a grid system where each image has extra space to change. So for an image 30px tall, I set up a grid of 50px tall. That makes it easier when coding the CSS; I know one image is 50px below the other.
For the Spangler site, for instance, I would just expand the image horizontally and down, so I wouldn’t have to change the current CSS. I might leave a white block where the old image used to be (since that uses very little memory) and move it below.