Showing posts with label Web Graphics. Show all posts
Showing posts with label Web Graphics. Show all posts

Sunday, September 19, 2010

CSS Sprites: What They Are and How to Use Them

CSS Sprites

A visually appealing website must have a great design. And usually with a great design, comes great images. And with great images, comes loading time. And with loading time, comes slow pages. ect, ect. Images are key in most web designs, so it’s always important to keep in mind how to optimize your images for the best user experience.
There are countless methods on minimizing the load time on your site, but this post will address one way in particular: sprites. More specifically, CSS sprites.

The use of CSS sprites is a fairly common practice with web designers. But what are they? The name may be a little misleading, as CSS sprites aren’t small, but big images containing dozens, and even hundreds, of little images.

History

Super Mario Bros. Sprites
Sprites originated in video games, back during the 8-bit glory days. Every visual item in a game was drawn as an individual image that would later be ‘layered‘ into the game as it was being played. However, as the complexity of games increased, game developers realized the benefits from combining all the images for a game into a singe sprite. By combining all the images into a single sprite (in a grid like formation), they could program the game to map positions of each individual graphic, and selectively placed them into the game.

What does this have to do with web development? Well, aren’t websites not unlike 8-bit games? Eh?


Why Sprites?



Sprite image from Google
Having all these images individually makes for smaller files sizes and fast loading, right? You may think so, but no. While it may look like your page loads fast, since there are dozens of images half loaded, they’re actually loading slower.

For each image, an HTTP-Request is called to download the image, and having a few dozen of them called at the same time slows down the entire process. Not to mention that despite a single image being smaller than a large sprite, file sizes add up, and fast. Combining a bunch of your images into a single sprite cuts down of your HTTP-requests, lowers the total file size, and your page then loads faster.

Example

Here’s a comparative example of the difference between a single sprite image, and 20 small images.

While the individual images on their own are smaller in size, collectively they’re more than 5x larger than the single sprite image.

Another great benefit to using sprites is when you use images for the :hover state. Normally, you’d have to use JavaScript to pre-load an image used in a :hover state, but if you’re using a sprite, and simply change the background position on :hover, there is no need to pre-load the image, since it will already be loaded.


Use and Limitations

One you create a single sprite image, you’ll have to use some CSS trickery to display the right image from the sprite.

Here is the CSS for the sprite image above:

.arrow { background-position: 0 0; }
.biggrin { background-position: 0 -20px; }
.confused { background-position: 0 -40px; }
.cool { background-position: 0 -60px; }
.cry { background-position: 0 -80px; }
.eek { background-position: 20px 0; }
.evil { background-position: 20px -20px; }
.exclaim { background-position: 20px -40px; }
.idea { background-position: 20px -60px; }
.lol { background-position: 20px -80px; }
.mad { background-position: 40px 0; }
.mrgreen { background-position: 40px -20px; }
.question { background-position: 40px -40px; }
.razz { background-position: 40px -60px; }
.rolleyes { background-position: 40px -80px; }
.sad { background-position: 60px 0; }
.smile { background-position: 60px -20px; }
.surprised { background-position: 60px -40px; }
.twisted { background-position: 60px -60px; }
.wink { background-position: 60px -80px; }


In my opinion, easiest way to apply sprites are to assign a base class that declares the background image and any other attributes, and then a sub-class that handles the background position.

<div class="smiles wink"></div>

.smiles {
background-image: url(smile_sprite.gif);
background-repeat: no-repeat;
width: 15px;
height: 15px;
}
.wink { background-position: 60px -80px; }
/* ect. ect. */

CSS sprites are really only good for background images. So they’re useless for anything in an img tag. They’re also are not good for images you want to be repeated on both the x and y axis, as when you repeat an image, the entire image is repeated, not just what is visible.
There is a workaround however for repeating sprites on one axis (in this example, the x). Here is a situation:
  • The height of the element is 20.
  • The width is dynamic.
  • The images are arranged in the sprite vertically, in one column
  • The image is repeated on the x-axis
There are other ways to work with repeating a sprite, but this was just one example.

So…

There are lots of resources out there for CSS sprites, from generators, to more detailed information. Here is a great generator that allows you upload a .zip file full of images, and will automatically generate a single sprite image, CSS styles included. :eek:
CSS Sprite Generator
I suggest trying sprites out. The concept can be difficult to adapt your methods to at first, but the more you use it, but more second nature it becomes.

Saturday, March 6, 2010

Web graphics tips for web designers

Here are some Web graphics tips and techniques for novice web designers which will help you create images that look good and are as compact as they can be... The main Web graphics formats are GIF, JPG, and PNG. It's important to know the differences and choose the best format for each images, so that pictures look good and are as compact as
they can be so they appear quickly on your site visitor's screen. Below I explain this three web graphics format. If you read this you will never make mistake to choose the right graphic format.

GIF - Graphic Information Format
GIF graphics format is best for images with only a few colors: charts, graphs, or text set as graphics.
The fewer colors you use, the more efficient GIF files are. GIF files....
  • can contain up to 256 colors.
  • support a feature called transparency, in which one color out of the 256 colors is set to be transparent. This keeps your graphics from looking as if they're in boxes, because the background of the file is invisible, letting the Web page's background show through.
  • can be animated. Inside a single file is stored many picture frames and an index telling how long each frame should be shown. Animated GIF is treated as a standard image file, so it is loaded with the standard <IMG> tag.
  • are lossless, which means the image quality is not degraded by the compression

process.
  • can be interlaced so they appear to fade in, from lower to higher quality, while loading. This gives your visitors something to look at while they're waiting.
  • are not good for photographs - you lose quality and the files won't be compact. Use JPG graphics format for photos.

JPG - Joint Photographic Experts Group
JPG graphics format is best for images with many colors, such as photographs or scanned artwork. JPG
files...
  • can contain up to 16 million colors.
  • support variable compression. You can apply more or less compression to each individual image. The more compression you apply, the more quality you lose. While file sizes can be made quite small with this graphics format, you often have to compromise between file size and picture quality. Newer graphics software gives you a preview before you save - this allows you to experiment with various levels of compression to choose the best compromise between quality and file size.
  • come in three types: baseline or standard, baseline optimized or standard optimized, and progressive. Baseline was designed for browsers that we'd consider to be ancient these days (such as Internet Explorer version 1). Baseline optimized offers more compression over standard baseline, and practically every browser today can read such an image. A progressive JPG, like an interlaced GIF file, builds as it downloads, going from a crude representation of the image to its finished look. While this is a nice Web graphics format, older browsers don't all support this format.
  • are not good for images with only a few colors, such as text set as graphics or images with areas of flat colors. If you use JPG for these graphics, they will be larger than necessary, and look "mottled."

PNG - Progressive Network Graphics
PNG is the newest Web graphics format. PNG files...
  • are supported by all modern browsers. These files may not appear in older browsers, so using this graphics format may cause some of your Web site visitors to be unable to see your images.
  • are compact and versatile and can combine the best features of GIF and JPG, such as the ability to have transparent backgrounds or the ability to contain images with millions of colors.
  • are still not widely used, mostly because they're not supported by older browsers.
When to use which?
Choosing the right Web graphics format can ensure that your images look good and appear quickly on your
visitor's computer. Choosing the wrong format makes your images look bad and take forever to download.
The most common graphics mistake people make on the Web is to use the wrong graphics format for their
images. But the choice is really quite simple...

  • If your graphics have a lot of colors (such as a photo), choose JPG.
  • If your graphics have few colors, choose GIF. When using GIF, try optimized palettes that contain only the colors used - they can cut file sizes in half.
I hope you understand now when and how you can use web graphics. If you still aren't too sure on which
format to use when see the chart below. Hope it will give you clear idea about web graphics.

    * JPEG:
          o Good:
                + Photographs
                + Game

screenshots
                + Movie stills
                + Desktop

backgrounds
          o Bad:
                + Windows

application screenshots
                + Line art and text
                + Comic strips
                + Anywhere

where fine lines or sharp color contrast is needed
    * PNG:
          o Good:
                + Text, line

art, comic-style drawings, general web graphics
                + Windows

application screenshots
                + When

accurate reproduction (lossless) is required (24 bit PNG)
                + When alpha

channel support is required
                + As a general

replacement for anything that is a non-animated GIF
          o Bad:
                + Photos,

in-game screenshots (only when quality is not important and you're looking for small files)
                +

Disappointing browser support for 32 bit PNGs from Microsoft and others
    * GIF:
          o Good:
                + Where

animations are required
                + Widespread

browser support
          o Bad:
                + Large file

sizes compared to PNG for the same quality
                + Often

limited to 256 colors (8 bit)