Css Sprites – How They Work And Why Theyre Great-sichen

By , July 24, 2018 8:59 am

While it’s far from being a new technique, the use of CSS sprites is one of my favorites, for several reasons. A sprite is simply a group of images that have been .bined into a single file. Sprites allow you to reuse your image over and over, and hide or display different sections using a little bit of CSS. Creating A CSS Sprite The first step is going to be creating the image, so you’ll need to open up your favorite image editor. Before you create the sprite, you should have your individual images handy. Once they’re all organized and ready, you can get started. Firstly, decide how you wish to lay out your sprite. A lost of sprites are arranged in just one column, but it is possible to go horizontal, or create a grid with rows and columns. This decision is partly going to depend upon whether your images are all the exact same size, but also on your own personal preferences. Once you’ve decided how to line up the pictures, you’ll need to establish the total size of your sprite. Add all of the image dimensions together, based on how you plan to arrange them. If your images don’t already have a background color built in, leave a pixel of white space between each – add that to your total.. If you’re lining up 10 images which are 25px by 25 pixels, and you would like sprite with 2 rows of 5 images, your total dimensions would be 129 pixels by 51 pixels (thats 25×5, plus 4 single pixel gaps, and 25×2 plus 1 single pixel gap). I usually like to place my images in the order that they’re going to be used on the page, just for organizational purposes. Now the 2nd image. Build a row or column as you see fit by lining the second image up alongside the 1st, and repeat this process with the rest of your images. This will leave you with a sprite: all of your images, side by side with no touching or overlapping each other. Now you just save your file and you’re ready to move on to the code. CSS Sprite Code: Making It All Work Now we’ve .e to the REALLY fun part: the code that hides/displays the appropriate section of the image. The first piece of code will define all the .mon characteristics your different elements share: size, display, outline, whatever they all have in .mon – plus the background image (your sprite) You’re including that in this first batch of .mon CSS because you’ll use the same image for each element – all you’re really changing is the alignment. It’s important to remember to label this code as a container ID or class – that way you don’t affect other elements on the page. It’s also vital that you assign a unique ID to each element within the container. That is what makes the whole process work. Now let’s set up each elements background position. For these styles, we are going to list two elements, separated by a single space. First, the container, followed by the specific element that you are applying the background property to. This is called "nesting."Doing this ensures that this element, and only this element are targeted by this style – protecting you in the event that you inadvertently reuse your unique ID somewhere outside the container you’ve set up. Now that you’ve selected the right item to apply the first section of the sprite to, you just need to add the "background-position" parameter. Assuming that the first image on the sprite is the one you’re using for your first element, the default value of "top left" will work just fine. "Top left" tells visiting browsers to align the top edges of both the background image and the element, as well as aligning the left hand images of both as well. In addition to "top" and "left" values of "right," "center," and "bottom" can be used as well. You also have the option of using variables to designate how many pixels the image should shift vertically or horizontally. Now, it’s important to remember that the edge values (top, left, right, bottom) can be listed in whatever order .es naturally, but for values like "center" or a number that could apply to either axis, you must always list the x-axis (horizontal) first – that’s just how the browser is going to interpret the code. The second line is where it gets fun. In our earlier example, we used 25 pixel images. To display the second image, our horizontal value would be 25px. This will slide the entire background image 25 pixels to the left of the edge of the element. As a result, the first 25 pixels of your background are now hidden, allowing the second 25 pixels to display, giving the appearance of a .pletely different image. If I lost you at all with that last step, just imagine a graph. The starting point, or 0,0, is at the top left corner of your image. You simply need to select new coordinates from there to decide where the top left corner of the SPRITE should be. Positive numbers move your sprite up and right, negative down and left. The second image would need a horizontal value of -50, to slide the sprite over one more image. The same principle applies to displaying different rows of your sprite: just change the second value and leave the first one alone. If you have a grid, you can change both.. We did it!Our CSS is now automatically loading our background image, and placing it so that the right section is visible for each of the three buttons! You can add as many rules as you need to for as many images as you want. And that’s really all there is to it! 1. Create your images 2. .bine your images 3. Define the .mon characteristics of the elements in a single style 4. Give each element a unique ID 5. Write some targeted rules for each elements unique ID 6. Take a nap Sprites like these can be used for almost anything, not just buttons. You could use them for graphic bullets if you write a lot of lists, or even create fancy images of numbers for numbered lists. You can use sprites to display fancy titles on things like sidebar widgets: the sky’s the limit. Virtually anywhere you use an image, you can incorporate a sprite to speed things up. 相关的主题文章:

Comments are closed