Tag Archive
3D 3dprinting android ant BigData bitcoin Browsers C/C++ cryptocurrency CSS dd ddrescue dogecoin DOS editors find Games Git hadoop html html5 Java Linux litecoin node perl Postgres Programming Python scripting Shell SQL Swing TOTK Utilities utilization vi Video Web Web Design Wifi Windows Wordpress XML Zelda
Sprite Starter Tutorial
There are times that you are going to want to add some imagery to your site. This would normally mean using many different image files which will increase the load time for your site. What if you could use a single image and display portions of that image throughout your site.
You could use use a single image called a sprite sheet. This single image file can hold many different images in different tiles for use through out your website. This allows you to load a single file to your page and adjust the viewport in different locations throughtout your site to display the images or icons you are using. Many major websites use this technique.
Let’s use an image like the following:
The size of this image is 220px x 110px. Each tile of the grid is then a 110px x 110px image. So we need to allow for a viewport to this image that is the size of the tile.
The method we are going to use is a simple div element with the class of “sprite”.
<div class="sprite > </div>
The CSS for this element will use height and width to define the size of the div. You could also create a placeholder image of the size you want your image to be that is a transparent png and place that inside the div, but that is not necessary.
This snippet is the CSS that we require to show the first image with the smiley face in the div. We specify a height and width for the div to create a viewport into a segment of the image. Next set the background of the div to the image url. The last step is to make sure that the image does not repeat.
.sprite { width: 110px; height: 110px; background: url("http://etechtips.com/wp-content/uploads/2017/04/smiley_sad.png"); background-repeat: no-repeat; }
In order to display the next image in the sequence you need to use the position properties of the background property. Using the hover pseudo class will allow us to demonstrate the change image update. In the hover property we only need to override the background property with a position offset of -110px in the x direction. Notice that we have used a negative to move the image to the left. The origin of this image is considered 0,0.
.sprite:hover { background: url("http://etechtips.com/wp-content/uploads/2017/04/smiley_sad.png") -110px 0; }
If you used a larger image with more tiles you could scale in both horizontal and vertical directions to show more images.
Example:
If the example doesn’t show in the article, either visit the single page post or checkout this codepen: Smiley Sample
Due to a minor adjustment, I used an offset of -109px for this image or I got a slight shift of the image when I hovered. You should only see the shift of smile to frown when you mouse over the image.
The tiles can be uniformly the same size or you could try packing in many images of different sizes and shapes. Just make sure that you allow for the viewport to not display overlapping images.
XML: Characters that require replacement
I was generating some html based e-mails and due to some changes I made in the text, my mail stopped being sent. I found that it was not liking some of the characters that were being placed in my generated emails. I tried some of the standard replacemnt methods, but found that the XML file was the issue. The Greater than(>) and less than (<) and the Amersand(&) and the apostrophe(') and the quote(") all have special meaning to XML and can cause these issues. Once I replaced the following characters in my email variable, my e-mails began to flow again. Here is a table of the characters and their XML representations that can be used for replacement:
Name | Character | XML replacement |
Ampersand | & | & |
Apostrophe | ' | ' |
Greater Than | >r | > |
Less than | < | < |
Quote | " | " |
This is by no means the complete list of replacements. This is only that characters that were needed by the calls to Java Mail that my program was using.