What is the best way to create an HTML image that displays various states (e.g. for a button: normal, hovered, selected, disabled)?
Ideally, I want to do via CSS classes (including the :hover meta-class).
Also, I’ve seen multiple states combined into a single image (which makes image pre-loading easier), but I don’t know how to display a single slice of the image as needed.
Thanks!
I assume you are talking about CSS Sprites. Here’s a A List Apart article to get you started: http://www.alistapart.com/articles/sprites. The basic idea is that you have all of your images combined into a single, large image.
Whereas normally you will have
imgtags or small elements with background image applied, now you have the single image applied to multiple elements as a background image, and each of them have different background position values to position the correct image into position. An example of this would be jQuery UI’s icons – the single combined image look like this:Then each of the individual icons share a single class, with a
backgroun-imageset:As well as individual
background-positionfor each of the different icons:The same can be done for the individual interactive states – change the background-position of the element on
:hover, and you get a different color or icon.