The complete tutorial (background-position application) for CSS to take a thumbnail from a large image) and backgroundposition

Source: Internet
Author: User

The complete tutorial (background-position application) for CSS to take a small image from a large image (conversion) and backgroundposition

I think that the application of background-position is very easy to understand. Make a mark.

I believe that many children's shoes who like to study the webpage interface have encountered a wonderful phenomenon: many pictures and materials on the webpage are merged into one picture.

At first, when I imitated the website, I often encountered this phenomenon. At that time, I didn't know what technology it was at. The whole picture material of people would not be used. I could only use ps to cut the image and cut it into a single image for reuse...

In fact, this is a very simple technology. It is hard to find the key to the problem because it is too difficult to imagine.

To Implement CSS image cutting, you only need to use one attribute: background-position.

Literally, this attribute is the background positioning. First, let's look at the google website's material map,As follows:

1 <div class = "btn"> 2 <a href = "http://www.kpdown.com"> + 1 </a> 3 </div>

 

With the html skeleton, We need to write css styles.

If we do not consider anything, we can directly set the entire image as the background,The style is as follows:

1 .btn{2     background:url(bg.png);3 }

 

Effect

1. btn {2 background: url(bg.png); 3 background-repeat: no-repeat; 4}

 

This will not be repeated.

Div can be understood as a rectangular box. Its top left corner is the vertex, and the top left corner of the background image is also the top left corner. When the div loads the background image, it will overlap the two vertices. The coordinates of the vertices are (0, 0 ). Looking at the picture without understanding it is very simple...

SetDivMake it the same as the width and height of a thumbnail.You can !!

Let's take a look at the background-position attribute. It has two parameters: pixels moving horizontally and pixels moving vertically.Negative number. The big picture does not move, and the div can only move to the right or down. Just remember that the pixels moving in the two directions are expressed as negative numbers!

Therefore, you only need to find the horizontal moving pixel and vertical moving pixel of the small image relative to the vertex in the upper left corner of the big image. You don't need any professional tools for cooking. It's very convenient to use. You can cut it from the top-left vertex of the big image to the vertex of the small image. It's almost the pixel at a glance, and then debug it, this is basically done.

In this example, the displacement of A small image is-25px-374px, and the size of A small image is 24px 16px. Therefore,The css style is as follows:

1 .btn{2     background:url(bg.png);3     background-repeat:no-repeat;4     background-position:-25px -374px;5     height:16px;6     width:24px;7 }

 

The effect is as follows:

In this way, even the thumbnail is shown! Easy !!

I want to explain the problem first. There is + 1 on the image, and I wrote + 1 on the hyperlink. This is because the text content is not written on the image in many cases, which is too flexible, text is text. In order to give you a complete demonstration, I wrote another + 1, and then I will process it!

First, center + 1, which is divided into horizontal and vertical centers. The horizontal center hyperlink requires text-align: center on the div. This attribute is for subnodes; in vertical center div, you only need to set the line-height attribute of tag a to be the same as that of div.The style is as follows:

 1 .btn{ 2     background:url(bg.png); 3     background-repeat:no-repeat; 4     background-position:-25px -374px; 5     height:16px; 6     width:24px; 7     text-align:center; 8 } 9 .btn a{10     line-height:16px;11 }

 

The effect is as follows:

 

Next, there is another problem. We can find that only when you move the mouse over the + 1 text will the mouse become a hand style to respond to the event.

This is obviously not what we want. It should be that when the mouse is moved into the image, it can be changed to a hand when the mouse is moved into the div, and it can also respond to events.

This is also simple. You only need to add the display: block attribute to the tag (Hyperlink.

In addition, this underline is quite confusing. Use text-decoration: none; to remove the attribute, which is very common.

The style is as follows:

 1 .btn{ 2     background:url(bg.png); 3     background-repeat:no-repeat; 4     background-position:-25px -374px; 5     height:16px; 6     width:24px; 7     text-align:center; 8 } 9 .btn a{10     line-height:16px;11     display:block;12     text-decoration:none;13 }

 

The last thing left is switching the background when the mouse moves in.

In this example, a tag is set in the div, And the mouse is moved to the background, of course, it refers to moving the mouse to the div, and the background is also changed to the background of the div, not the tag !!

Therefore, hover must be called on the div label. The div style is btn, so it is written as. btn: hover {}.

To change the background, you also need to find the background image, which requires a small image, that is, the B image shown above.

The displayed figure is A small image. The smaller image B and the smaller image A are on the same horizontal line. Therefore, the moving pixels in the vertical direction are the same, the horizontal direction difference is not the horizontal pixel of A small image plus the width of A small image.

After testing, the displacement of the B Small graph is-50px-374px, so you don't have to worry about the size. It must be the same as that of the small graph.

Position background-position:-50px-374px of the Small B image and put it in. btn: hover.

The style is as follows:

 1 .btn{ 2     background:url(bg.png); 3     background-repeat:no-repeat; 4     background-position:-25px -374px; 5     height:16px; 6     width:24px; 7     text-align:center; 8 } 9 .btn a{10     line-height:16px;11     display:block;12     text-decoration:none;13 }14 .btn:hover{15     background-position:-50px -374px;16 }

 

Final effect-before moving the mouse:

Final result-move the mouse over:

Well, the tutorial is over now. This is just a simple demonstration of a complete production process. There are still many details in the process, such as browser compatibility and CSS optimization, this requires the reader to explore.

In fact, the small dish has been talking about CSS tips. The real technology is called CSS Sprite technology, and Chinese people are used to being CSS genie.

This technology has both advantages and disadvantages. The advantage is that because all images are put together, only one image is requested during a request, which reduces the number of interactions with the server and solves the problem of hover delayed loading. The disadvantage is that it is difficult to control, scalability is not good, and there will be changes in the future, it can be said that it is a drag-and-drop, and sometimes the background is broken because of different screen resolutions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.