Quantcast
Channel: Modern Webz
Viewing all articles
Browse latest Browse all 132

Web Design: CSS Image Sprite

$
0
0

CSS Image Sprite is a method of combining several images into one image file to reduce HTTP requests and optimize web load performance.

There are many ways and handy tools to do this, or else you can also do it traditionally with Photoshop.

But, through all my experience of dealing with CSS Image Sprite, there is no other way that much easier than using Sprite Function for Compass. So, let’s take a look at how it works.

Recommended Reading: Syntactically Awesome Stylesheets: Using Compass In Sass

Starting Compass

Before working with Compass codes, we need to create Compass project watch it. So, every time there is a change in the project including the images and the .scss, Compass will compile it into the proper form.

Let’s open your Terminal or Command Prompt (if you are on Windows), and run the following commands.

  1. compass create /path/to/project
  2. cd /path/to/project
  3. compass watch

Combining Images

As we mentioned above, you can use Photoshop to manually join the images or you can also use some handy tools, such as SpriteBox or SpriteMe. Compass integrates this feature in the Function. Let’s say we have the following icons under images/browsers/<images>.png folders.

Icons by Futurosoft

To join those icons in Compass, we can use @import rule and then direct it to the image folders followed by the image extension through the .scss stylesheet, like so

view plaincopy to clipboardprint?
  1. @import ”browsers/*.png”;

After saving the file, Compass will combine those images and generate new image files, as follows.

Layout Orientation

Furthermore, we can also set the sprite orientation. As you can see in the screenshot above, the images are arranged vertically by default. In case vertical orientation does not fit the circumstances, we can direct them horizontally or diagonally with the following variable $<map>-layout: horizontal; or $<map>-layout: horizontal; replace the <map> with the folder name where you saved the images.

Horizontal

view plaincopy to clipboardprint?
  1. $browsers-layout:horizontal;
  2. @import ”browsers/*.png”;

Diagonal

view plaincopy to clipboardprint?
  1. $browsers-layout:vertical;
  2. @import ”browsers/*.png”;

Adding Image In The Stylesheet

Once we have done combining the image, we add the image in the stylesheet through background image. Traditionally we will do it this way.

view plaincopy to clipboardprint?
  1. .chrome {
  2.     background-position: 0 0; width: 128px; height: 128px;
  3. }
  4. .firefox {
  5.     background-position: 0 -133px; width: 128px; height: 128px;
  6. }
  7. .ie {
  8.     background-position: 0 -266px; width: 128px; height: 128px;
  9. }
  10. .opera {
  11.     background-position: 0 -399px; width: 128px; height: 128px;
  12. }
  13. .safari {
  14.     background-position: 0 -532px; width: 128px; height: 128px;
  15. }

In Compass, we have a couple of ways that are much simpler. First, we can generate something like those CSS rules with this syntax @include all-<map>-sprites;. Replace the <map> with the folders where we store the images.

view plaincopy to clipboardprint?
  1. @include  all-browsers-sprites;

This line above when compiled to regular CSS generates the background image definition as well as each of the position, as shown below.

view plaincopy to clipboardprint?
  1. .browsers-sprite, .browsers-chrome, .browsers-firefox, .browsers-ie, .browsers-opera, .browsers-safari {
  2.   background: url(‘/images/browsers-sad8e949931.png’) no-repeat;
  3. }
  4. .browsers-chrome {
  5.     background-position: 0 0;
  6. }
  7. .browsers-firefox {
  8.     background-position: 0 -128px;
  9. }
  10. .browsers-ie {
  11.     background-position: 0 -256px;
  12. }
  13. .browsers-opera {
  14.     background-position: 0 -384px;
  15. }
  16. .browsers-safari {
  17.     background-position: 0 -512px;
  18. }

Or, we can also add background image individually to particular selectors with this syntax @include <map>-sprite(image-naem);; as in previous codes replace the <map> with the folder where we store these images. Here is an example.

  1. li {
  2.     @include browsers-sprite(safari);
  3. }

Then, Compass is clever enough to identify the background position, without us having to specify it explicitly. In regular CSS, that line above will turn into

view plaincopy to clipboardprint?
  1. .browsers-sprite, li {
  2.   background: url(‘/images/browsers-sad8e949931.png’) no-repeat;
  3. }
  4. li {
  5.     background-position: 0 -512px;
  6. }

Specifying Container Dimension

The last thing we need to do is specifying the container height and width that contain the image. We commonly do it in traditional way by manually inspecting the image width and height (most likely through image info or image properties).

With Compass, we can utilize this function <map>-sprite-height(image-name) or <map>-sprite-width(image-name) to retrieve the image width and height. In this example, we will retrieve one of the image width and height and store the value variables as well assign the background image with @include directive.

view plaincopy to clipboardprint?
  1. $height: browsers-sprite-height(safari);
  2. $width: browsers-sprite-width(safari);
  3. li {
  4.     display: inline-block;
  5.     height: $height;
  6.     width: $width;
  7.     @include browsers-sprite(safari);
  8. }

When we compile these codes above, it turns into the following in regular CSS.

view plaincopy to clipboardprint?
  1. .browsers-sprite, li {
  2.     background: url(‘/images/browsers-sad8e949931.png’) no-repeat;
  3. }
  4. li {
  5.     display: inline-block;
  6.     height: 128px;
  7.     width: 128px;
  8.     background-position: 0 -512px;
  9. }

The post Web Design: CSS Image Sprite appeared first on MODERNWEBZ.COM.


Viewing all articles
Browse latest Browse all 132

Trending Articles