How to make a sprite in Photoshop. Introduction to CSS sprites for teapots

Good day, today I want to tell you how to make CSS sprite icons for your site. Yes, in other matters, not only icons, but also any other elements, such as buttons, menus, etc. Although the schedule of all Ledding.

The article should go to the heading acceleration and optimization, since the use of sprites accelerates the page load, due to the fact that one picture, even bigger size, boot faster than somewhat smaller. This is because there are fewer appeals to the server.

For those who are not in the subject, the sprite looks like this:

That is, as applied to the web - this is one picture with many elements, each of which is shown in the right place of the site due to positioning.

But the question of how much faster or slower the page is loaded using sprites, I will touch on the next article, and it will definitely get into the right heading. So subscribe to Twitter. not to miss an announcement of the article.

And in this post, I will try to show you the inner kitchen of how sprites are used on the web, on the example of the icons of your blog.

So, I created a simple sprite icons in Photoshop. It was not particularly bored, but made a pair of each type, in order to show not only how to use the sprite, but how can I use sprites to create pseudo-arimation.

Creating CSS Spright - HTML Marking

Now you need to sketch html - markup. Nothing unusual. Non-dimumed list:


Subscribe to updates






Instead of "#" - enter the addresses of your accounts. Add Title links. And go to add styles!

Creating CSS Spright - CSS Marking

We ask the styles of the container:

SocSeti (
width: 270px;
HEIGHT: 150px;
Margin: 200px auto;
Background: URL (../ images / bg-soc.png) no-repeat;
Padding: 15px;
}

Describe styles for text and a non-dimmor list that Floatim on the left edge ( standard procedure For menus and similar elements)

Socseti UL (
Overflow: Hidden;
width: 246px;
Margin: 20px auto;
}

Socseti UL Li (
Float: Left;
margin-left: 2px;
}

Socseti UL LI: Last-Child (
MARGIN-RIGHT: 2PX
}

Now the most interesting begins. Let's set common styles for links:

SocSeti UL Li A (
Display: Block;
width: 59px;
HEIGHT: 59px;
}

As you might notice, I am a little mistaken when creating a sprite and therefore it turned out an idiotic size of 59 × 59 px - but it did not affect visual perception. We continue ...
Set the background image of the first link.

a. tvitter (

Transition: .3s;
}
a. tvitter: hover (

Transition: .3s;
}

In order to display the necessary element of the sprite, it should be sacrificed. To do this, after the background link is specified, you need to set the coordinates.

For example, you might notice that on the first element there is a social network icon "VKontakte", and the first icon in the SOC menu. Icons - Twitter. That is, if my icons have a width of 59px, then I need to move the background on -59px. And also, I want the color to change with gray on color. To do this, I need to lower the background on 59px. What is shown here:

a. tvitter (
Background: URL (../ images / css-sprite-iconok2.png) -59px -59px no-repeat;
Transition: .3s;
}

In order to change the class styles when you hover, it is necessary to set the Hover pseudo-class. What I do here:

a. tvitter: hover (
Background: url (/ images / css-sprite-iconok2.png) -59px 0 no-repeat;
Transition: .3s;
}

And in order for the picture to change the position - changed the display coordinates.

Property TRANSITION - Used to specify the speed of change. I set 0.3 seconds.

Similarly, it is necessary to set a position for the remaining elements of the sprite, moving it to 118 Px to display RSS - icons, and return to 0 px to display the "VC" icon.

I hope the article was useful to you, and now you will use CSS sprites for your sites. Tomorrow I will try to analyze how much the site is loaded faster when using this technology.

Before CSS appeared a pseudo-class: Hover, the creation of Rollover - an element that changes his appearance when hovering the cursor - was implemented through javaScript language. Now it is much easier, but there is one disadvantage: if in a state: hover (i.e., when you hover the cursor on the element) there should be some kind of background image, it starts to load at the time of the cursor, and not when the page is generally loaded .

Because of this, a slight delay may occur when the picture appears for the first time. And although with all the subsequent guidance of the cursor of this delay no longer, many developers thought about how to eliminate this problem. For example, using JavaScript, you can provide download all background images in advance. But there is a way that does not require knowledge of the programming language, and it consists in using sprites.

What is CSS sprites

CSS-sprite is one graphic file that contains many different images. It can be collected, for example, all graphic elements of the interface, such as icons, buttons, etc. Also in sprites often combine background pictures along with their variations for the condition: Hover. Typical examples of sprites:

Sprite, in which all used sprite icons are collected with social network icons and their variations for: Hover

Advantages of CSS-sprites

  • CSS-sprite contains background images for different states of items, and it is entirely loaded when the web page is loaded. This eliminates the Rollover to the primary hovering cursor to the element.
  • The number of appeals to the server is reduced. After all, to download each small icon, a separate request is required, and the fewer appeals, the better. To download the sprite you will need only one request to the server.
  • Another advantage of storing a plurality of pictures in Right is that if they have a similar color palette, then one large image will weigh smaller than some small pictures in the amount.
  • All this as a whole positively affects the download speed of the site.

How to create sprite from pictures

In the classical case, the sprites are created in the image editor. Small pictures are sequentially placed on a large canvase (often with transparent background), which is then saved as a graphic file.

The manual way to create sprites - the lesson is quite long. Therefore, when the use of sprites began to gain momentum, there were online services for the automatic creation of sprites, for example, Spritegen., Retinaspritegenerator. other. You download the necessary images in separate files to the site, and the sprite is generated for you, which you can immediately download to the computer.

How to use CSS sprites

It's time to learn how to use CSS-sprites in practice. We will show you a small example using sprite containing icons. social networks (We showed it above). In the usual state of reference, the social network icon will be color (top row of sprite), and when you hover the cursor it will become gray (as in the bottom row).

HTML markup looks like this:

For reference to each social network, we created our class. Since this is a test example, we do not spend time to fill the href attributes - instead of links to real sites we put the plugs. Inside tag We do not put anything because appearance Links will answer their background images with social network icons.

Go to CSS. First of all, we write the common styles for all links:

A (background-image: url (https://goo.gl/1t18rz); background-repeat: no-repeat; height: 98px; width: 100px; display: inline-block; margin-right: 10px;)

The following two lines - the height and width of each element . How did these values \u200b\u200bdetermine? Here we pushed out of the size of the icons in right. The height of each icon is 98 pixels, and the width is 100 pixels. Next, we will position the background picture for each link, adjusting the background in such a way that the icon is smoothly fit in the "window" of the links of 98 × 100 pixels.

A.Facebook (Background-Position: Left Top;)

Go further: position the background for reference to Twitter. The Twitter icon is located to the right of Facebook and adjoins it, without creating empty gaps. We need to move sprite to the left to so many pixels to hide the Facebook icon and fully fill the reference area of \u200b\u200bthe Twitter icon. Since the width of each icon is 100 pixels, then we shift the background to the left per 100 pixels. Vertical positioning, we do not change anywhere, but leave the value TOP:

A.twitter (Background-Position: -100px top;)

We will do a similar effect for other references, adding another 100 pixels to each subsequent horizontal positioning, thereby shifting the sprite more and more left. It turns out such a picture:

A.youtube (Background-Position: -200px top;) a.instagram (background-position: -300px top;) a.linkedin (background-position: -400px top;) a.tumblr (background-position: -500px top ;) a.Google (Background-Position: -600px TOP;)

The result of this code is shown in the screenshot below:


Now we will deal with the behavior of the background pictures when you hover the cursor to the link. We need to shift the sprite vertically up in such a way that an icon from the bottom row of the sprite hit the viewing area. Here everything is quite simple and solved by one CSS rule, namely the assignment of the Bottom value to the Background-Position-Y property:

A: Hover (Background-Position-Y: Bottom;)

In our case, this style can be written one for all links, and it will work correctly. However, in other situations, when elements in spray are in a different order, you may need to write more code.

For the result of working sprites, see the demonstration below (for a more beautiful example of the example, we made a shift background more smooth when you hover the cursor):


If it became interesting to you, then here is a string that is responsible for the smoothness of changing icons (do not worry, a little later we will study CSS animation, and in particular the Transition property):

A (Transition: All 0.2S Ease-Out;)

So, now you know what CSS sprites are and what they can be useful. In addition to preventing delays when loading background patterns, the use of sprites helps reduce the load on the server. In addition, this is a convenient way to store all the graphic elements of the interface in one place.

So we disassemble the main methods of styling links. We recommend that you additionally practice to secure the knowledge gained. And the next chapter of our book is devoted to CSS animation, the implementation of which became possible due to the properties that appeared in CSS3. And in the first lesson, we will analyze the transformation property -

Sprite is a favorite drink of millions of people who can not only purchase in every store, but also prepare themselves. There are many different recipes and tips on the network of networks, how to make a sprite no worse than the present. The most popular of them are collected in this article.

The prescription recipe is quite simple. It is necessary in the correct proportions to mix such ingredients such as lime, lemon juice, sugar and mineral water. However, there are several rules that must be kept that the drink is really tasty and natural.

The first thing that is important is to use for the preparation of exceptionally fresh fruits, and not to take those that "sorry to throw out", but there is no longer. The spoiled product can spoil the taste of the whole dish, the same goes for drinks.

In addition, it is important to use high-quality mineral water. Do not save on this ingredient, because it plays a key role in cooking. Water must be a strong, weak or non-carbonated mineral water for such purposes will not fit.

Instead of sugar in domestic drinks like sprite, mojito and other cocktails, it is recommended to add sugar syrup. A detailed recipe for the preparation of the syrup is described below.

How to cook sugar syrup?

Sugar syrup is necessary in order for the home cocktail to taste as close as possible to the purchase. It is quite simple to prepare such a syrup, and too much time will not need.

First of all, it is necessary to put a saucepan on fire and wait until water heats up. The fire should be not strong, but not weak, otherwise the sugar will be melted for a very long time. Gradually, the required amount of sugar must be filled into a saucepan, bring water to a boil, give sugar to completely dissolve.

The resulting liquid is cool. Sugar syrup is ready - and now it can be used to prepare delicious cocktails.

Sprite in 10 minutes

For the manufacture of this popular soda, it will not be necessary a lot of time - you can cope with the task in less than 10 minutes. One of the most common recipes, how to make a sprite at home, looks like this:

  • finely cut one lemon, get rid of the zest;
  • add a lemon in a saucep with 5 sugar-spoons, cook five minutes;
  • the resulting liquid is praised for about an hour;
  • strain and add it to any strong-hydraized water.

As a result, it will be a wonderful drink, completely identical to the purchase sprite. And goes on its preparation for only ten minutes, not counting the time for which the liquid must be broken.

Advanced way how to make home sprite

In addition to standard recipes for the preparation of a coquiler with lemon and carbonated water, you can find more interesting ways to create a drink using other ingredients. To cook sprite, you will need:

  • simple water;
  • fragrance "sprite";
  • lemon acid;
  • carbon dioxide.

As for the last "ingredient", it is far from everyone. Carbon dioxide is used in factories and factories with mass preparation of carbonated beverages. However, many chefs experimenters among stocks in the kitchen there are this element.

Homemade sprite-based drinks

Spright is popular in particular due to the fact that it is based on its basis, many well-known alcoholic and non-alcoholic cocktails are being prepared, which are served in nightclubs and bars. One of these is a mojito drink. This cocktail is especially relevant in the summer, because he not only quenches thirst, but also perfectly tones the body.

In the composition of traditional mojito there are several ingredients. As a rule, it is carbonated water, mint juice and sugar. If we are talking about an alcoholic cocktail, the white rum is added to this list.

Mochito

The article presents several recipes, how to make a sprite at home, but it's still easier with Mojito! This cocktail only seems difficult.

As a rule, the following ingredients are required to create its own cocktail:

  • cane sugar;
  • soda;
  • lime or lemon;
  • mint;
  • rum (in extreme cases vodka);
  • ice cubes.

As soda, sprite is perfect, which can also be prepared on your own. If you are wondering how to make a sprite at home, recipe and detailed step by step description can be found above.

"Collect" mojito is needed in this way:

  • cut the lime in half, from one half squeeze juice;
  • finely cut into mint, add to the glass to lime;
  • fill a glass of ice cubes;
  • the remaining space in the glass pour soda;
  • add 30 ml of white rum.

Mojito should be served immediately after cooking so that the ice in the glass does not have time to melt. It is recommended to drink from the tube. Non-alcoholic mojito can be prepared even to children.

Raspberry lemonade

This is another popular non-alcoholic cocktail, which so like many children, because the main ingredients in it - sweet juicy raspberries and no less tasty also to create this cocktail and sprite. You already know how to make a sprite, a recipe with photos and methods of cooking at home are described above.

For the cocktail "Raspberry lemonade" will be required:

  • raspberries;
  • raspberry syrup;
  • lemon;
  • sprite;
  • mint;
  • lime.

First of all, it is necessary to prepare raspberries - thoroughly to smoke three ripe berries in a glass. After that, you need to add a lemon lurch and fill the glass to the top ice cubes. The remaining place in the glass will take the fragrant home sprite. For decoration, use lime, lemon and mint.

Orange lemonade

Another children's delicacy is an orange lemonade, which can also be prepared in the home. This refreshing invigorating drink is no less inferior to shopping gazepers. On the contrary, it turns out even tastier if you cook it correctly.

For independent preparation of orange lemonade, these products will be needed:

  • 3 middle orange;
  • 2 lemon;
  • ground ginger root (teaspoon);
  • 150 grams of sugar;
  • 1 l sprite;
  • 1.5 liters of simple water.

First you need to prepare sugar syrup, as it will be used in the recipe. Oranges to clean from the peel, finely cut, lemon cut in half. Half lemon and oranges put in a saucepan, pour water and add ginger root.

The resulting liquid is thoroughly strain. Fill a decanter by half cubes of ice, pour citrus water, sugar syrup and sprite. Also suitable and ordinary mineral water. The remaining half of the lemon is cut on the slices and add to the resulting drink.

One of the key tasks CSS sprite Is optimizing the graphics of the downloaded page. What is the task of this optimization and why should it be performed? The idea is as follows: while the page is not optimized, graphic elements are represented by separate files. The number of similar elements exceeds 5 or 10 pieces, therefore, in order to upload each picture, the browser carries out a separate request.

It is logical to assume that you can speed up the process by combining graphic elements. Display parts configured through CSS. After that, the browser will execute only one request to upload images. Thereby the page will be loaded at times faster. That's the whole principle of optimization.

Besides Sprites are used In order to correctly form the effect of the visual change of the element at the time when the user brings the cursor. The absence of this can be noted when the visitor sees a flashing button: the button has two states - initial and at the time of guidance, between loading of these states, in the absence of an organization, a too long pause occurs, which causes the effect of blinking.

So, consider the main examples of CSS sprites:

We have a list of icons in one file. Task: withdraw some pictures in the random sequence.

Picture ( cSS-SPRITE-01.PNG) Below is sprite.

After applying the list of styles, the icons remain out of random order.

Let's figure it out in more detail with this example. Permission of the initial image-sprite: 200 * 200 px. The main picture is divided into 4 pieces, each of which has permission 100 * 100 px. We need to create a CSS-SPRITE-UL list in the document. List points number as 001, 002, 003, etc.

< ul id = "css-sprite-ul" >

< li id = "css-sprite-001" > < / li >

< li id = "css-sprite-002" > < / li >

< li id = "css-sprite-003" > < / li >

< li id = "css-sprite-004" > < / li >

< / ul >

Now this list will show items, and you need to get parts of the sprite image. To do this, refer to the .css file, where you insert this code:

# CSS-Sprite-UL Li (Margin: 0; Padding: 0; List-style: none; position: absolute; top: 0;)

# CSS-Sprite-UL Li, # CSS-Sprite-UL A (Height: 100px; Display: Block;)

# CSS-SPRITE-001 (Left: 0px; width: 100px; heigth: 100px; background: url ("/ images / css-sprite-01.png") 0 0;)

# CSS-SPRITE-002 (Left: 200px; width: 100px; heigth: 100px; background: url ("/ images / css-sprite-01.png") 100px 0;)

# CSS-SPRITE-003 (Left: 400px; width: 100px; heigth: 100px; background: url ("/ images / css-sprite-01.png") 0 100px;)

# CSS-SPRITE-004 (Left: 600px; width: 100px; heigth: 100px; background: url ("/ images / css-sprite-01.png") 100px 100px;)

Analysis of styles will make it possible to understand in which place and how exactly parts of this sprite image will be displayed.

Example 2. The image is changing when you hover.

Another example: Tries to create a button that will change when the cursor is hovering. So, it will change the color and text. Our task is to avoid the effect of blinking, which was described above. To do this, both states must be placed in one file.

What a sprite looks like:

hTML code:

< ul id = "css-sprite-button" >

< li id = "css-sprite-button-hover" > < / li >

In today's article I want to talk a little about CSS sprites and give several links where you can create these most sprites. The technique of using sprites is not at all nov and used for a long time, but the complexity of their creation scares webmasters. Let's try to help them ...

What is CSS sprites?

Imagine one, large picture, on which all your icons, dividers, buttons, arrows and other components of the design are depicted. It loads along with the site, everything is as usual. That's just does not create a bunch of requests for all files, but is executed by one request. Cool? Undoubtedly. After downloading, the desired part of the big picture is displayed in the desired location.

I saw the statistics somewhere (I do not remember where, so I will not give a reference) - from 5 to 40% of the time it is actually a page, everything else - the processing of requests by the server. It is logical to assume if the number of requests will decrease - the download speed will grow proportionally.

Let's take a closer look at the pros and cons of the use of sprites:

pros Use of sprites:

Minuses These are technologies:

  • when laying, it is necessary to specify not only a picture, but also the position of the fragment, which is not entirely convenient and habitual.
  • increase the load on the server. The big picture takes more random access memory, given the "pacifiers" between fragments.
  • to change one icon, you will have to pick the entire file, which is not too convenient. Although, maybe it is for the better

We choose who is more important - simplicity or speed and go further. If nothing can be done with the load on the server, then to solve problems with layout and changing sprites there are tools.

Sprute generators - Online services, which, in principle, work according to the same scheme. You download your set of pictures, select the format of the sprite and the system creates a picture and CSS markup for this file.

Where to create sprites?

Probably the oldest tool for creating CSS. sprites. A distinctive feature is a Russian interface version, it is convenient. Well configured, not just creating a picture, but also CSS code. In general, an excellent generator, I recommend. By the way, in addition, the service is able to generate Favonons and calculate the permissions for UNIX systems.

CSS Sprites Generator.

This generator is also not bad, has everything necessary settingsgenerates a slightly smaller file at the output than the previous service. A distinctive feature is a convenient output of CSS code for each image.

A fairly functional generator that allows not only to create a regular sprite, it makes it possible to move the pictures on your own, placing them as compact as possible. Plus - able to produce a compressed CSS code, as well as Sass code.

SASS. - This is a CSS-based methane language, designed to increase the level of CSS abstraction code and simplifying the files of styles cascading tables.

This service is different from the remaining presented by the fact that it does not create a picture of the sprite. You can create it yourself in Photoshop or somewhere else, download here and the service will help with CSS code. Visual I. convenient interface. I recommend to do everything for lovers yourself, not trusting the car

A good generator, there is everything you need to create. The only minus - every picture will have to be loaded separately, you can not load a bunch. Otherwise - nothing remarkable, strong middling.

The service is written on HTML5, it works only in Chrome and Firefox, but the development of such services pleases. The web does not stand still and it is a good very simple, minimum settings. But if you want a quick and simple solution - this generator will suit 100%

Sprite generator for real Jedies Install the program, specify the path to CSS filewith old layout and this service will do everything for you. He has a lot of opportunities - I recommend to read the documentation on the official website.

Almost a complete analogue of spratetbox is also used only to get CSS code, with the difference that here the selection of the element's boundary is carried out not manually, but automatically, the accuracy of testing is noticeably higher. Works only in Chrome and Firefox

Excellent service that works like this - go to the finished, tormar as usual, page. We click on the service icon (you need to install it by a bookmarket) and all - the service collects all the images from this page and generates a sprite with the code. Very convenient, there are settings that specifically you need to collect in a bunch. In general, a good service.

Very simple, but understandable and convenient generator. Drag the pictures to combine, position them as you want and press the button - the sprite and the code are ready. The minus of this service - it works only in chrome and fire.

The service is very similar to the Spriteme described above, with the difference that it is necessary to load all the pictures and the CSS file to be changed by the hands. Minimum settings, but he does his work well.

I hope now you are not afraid work with CSS sprites and your projects will be even faster.