In this article you will learn how to create image sprite using Visual Studio Web Essential Extension. Suppose you have a website that uses lots of images and each image loads individually which increases number of requests and makes website slow. We need quick website optimization to fix it.
Image Sprite is sort of technique that combines multiple images to make single large image and website loads one image instead of all images it makes website fast.
Let’s understand what is Web Essential and then we will look at creating Image Sprite inside Visual Studio IDE.
Web Essentials extends Visual Studio with a lot of new features that web developers have been missing for many years.
This extension is for all Web developers using Visual Studio. This extension was first introduced in August 2012 by Mads Kristensen.
Web Essentials 1.7 for Visual Studio 2013 it is now easier than ever to work with images.
If you don’t have this extension installed in your favorite Visual Studio IDE then you are missing many features, you can download it quickly from here.
Now let’s start exploring how to create Image Sprite.
Paste Clipboard Image onto Visual Studio Editor
There are various ways we can create Image Sprite even before Web Essential included this feature. But with Visual Studio Web Essential extension we can create it way-way faster the before. Let’s follow the steps.
Step 1: Select images and right click on selected images > Web Essentials > Create Image Sprite...
When we click on ‘Create image sprite…’ it will ask us to enter sprite name. Enter any name of your choice and click ‘Save’. By default new sprite will be added inside ‘images’ folder.
Step 2: Expand sprite tree (as in above image), you will see .png file immediately under sprite tree. This .png file is actually single image contains all small images we selected while creating this sprite. Here is the image.
Step 3: Expand MySprite.png file, you will find .css, .less, .map and .scss files. These files are nothing but to backing up the sprite, and necessary to keep with project.
If you open MySprite.png.css file, you will see normal CSS codes that we can copy it to use in our original style file (Site.css). Here is the screenshot of MySprite.png.css file.
In above image you can see for each small image, there is location/region defined to use when displaying large image portion (we see small image).
Copy these styles into Site.css file or any other style file you may have.
Step 4: Now in order to use these new CSS or say this new sprite, we can use following markup on the DOM.
You can see I’m using class selector to map the style from CSS file.
I have recorded a Hindi video on this, if you don’t understand Hindi, well you can watch the steps I’m using J
Hope this helps.