Details on the usage of background-position in css

Source: Internet
Author: User
Tags html page
You can easily understand the origin position, that is, the background-position in the upper left corner of the block element indicates that the offset origin of the image and the coordinate origin is not dynamic, the X coordinate of the image is shifted to the right in the upper left corner of the regular Image. If the X coordinate is negative, the Y coordinate in the upper left corner of the image is shifted to the lower left corner of the regular image, if it is negative, there is a formula for calculating the percentage of up translation in the upper left corner: x axis (container width? Image width) * percent of characters Y axis (container height? Image height) * Here is the percentage of characters ?? Are you sure you want to handle the issue? South Korea is just a hit when the ship's key was so it is just a drag of the ship is willing to renew the pen? Why N? When I buy a Japanese business, I want to buy a Japanese business. Why? Why? You can throw a zombie on the street, copy it, and slide it on the reef !! Next let's take a look at the CSS background-position syntax: background-position: length | length background-position: position | position value: length :? Percent | length value composed of floating point numbers and unit identifiers. See length unit position :? Top | center | bottom | left | center | right Description: sets or retrieves the background image position of an object. The background-image attribute must be specified first. This attribute positioning is not affected by the object's patch attribute (padding) settings. The default value is 0% 0%. In this case, the background image is located in the upper left corner of the content area of the object that does not include the patch. If only one value is specified, the value is used for abscissa. The default ordinate value is 50%. If two values are specified, the second value is used for ordinate. If the value is set to right center, right will overwrite the center value as the abscissa value, so the background image will be located on the right. The corresponding script feature is backgroundPosition. Note: the image size used in this article is 300px * 120px. In order to clearly express which part of the image is hidden, the image size is evenly divided into 9 equal parts. At the same time, the container area of the background image draws a green border to clearly display the container range. 1. background-position: 0 0; the upper left corner of the background image is aligned with the upper left corner of the container element. This setting is consistent with that of background-position: left top; or background-position: 0% 0%. Example :. container {width: 300px; height: 150px; background: transparent url(bg.jpg) no-repeat scroll 0 0; border: 5px solid green;} the effect is shown in Figure 1: figure 12. This attribute is not affected by the object's patch attribute (padding) settings. For example, if we add a padding value to the container element, the upper left corner of the background image is still aligned with the upper left corner of the container element. In this case, only the height and width of the container elements are affected. . Container {width: 300px; height: 150px; background: transparent url(bg.jpg) no-repeat scroll 0 0; border: 5px solid green; padding: 50px;} the effect is shown in Figure 2: figure 23. background-position:-70px-40px; the image uses the upper left corner of the container as the reference to offset 70px to the left and 40px to the top, for example :. container {width: 300px; height: 150px; background: transparent url(bg.jpg) no-repeat scroll-70px-40px; border: 5px solid green;} the effect is shown in Figure 3: figure 34. background-position: 70px 40px; the image uses the upper left corner of the container as the reference, and the offset to the right is 7. 0px, offset 40px down, for example :. container {width: 300px; height: 150px; background: transparent url(bg.jpg) no-repeat scroll 70px 40px; border: 5px solid green;} result Figure 4: Figure 45, background-position: 50% 50%; the image is centered horizontally and vertically. This is equivalent to the background-position: center. Equivalent to x: {container (container) width-background image width} * x percentage, the excess part is hidden. Equivalent to y: {container (container) height-background image height} * y percentage. The excess part is hidden. Example :. container {width: 300px; height: 150px; background: transparent url(bg.jpg) no-repeat scroll 50% 50%; border: 5px solid green;} x = (300-210) * 50% = 45px; y = (150-120) * 50% = 15px; Figure 5: Figure 5 expands to both ends as the excess part is exceeded, therefore, we can first create an image with a width that is sufficiently wide and set the horizontal value to 50%. This can be used to adapt to different browsers, so that the image horizontally fills the browser window and is centered. Replace margin: 50 auto. 6. background-position:-50%-50%; equivalent to x:-{container (container) width-background image width} * x %. The excess part is hidden. Equivalent to y:-{container (container) height-background image height} * y percentage. The excess part is hidden. . Container {width: 300px; height: 150px; background: transparent url(bg.jpg) no-repeat scroll-50%-50%; border: 5px solid green;} the effect is shown in Figure 6: figure 67. background-position: 100% 100%; the image is in the lower-right corner of the container element, which is equivalent to background-position: right bottom. Example:. container {width: 300px; height: 150px; background: transparent url(bg.jpg) no-repeat scroll 100% 100%; border: 5px solid green;} learn more about CSS background-position
X: 0, y: 0
X:-50px, y:-50px
X: 100px, y: 100px
In the HTML page, there is a gray background DIV block of 162*162 and three background images with different positions are added. In the browser, the effect is as follows: as shown in the figure above, when it is background-position: 0 0; the upper left corner of the image overlaps with the upper left corner of the DIV. When we locate the image in background-position:-50px-50px, we find that the image is moved to the upper left corner, it is also centered on the upper left corner of the DIV, and the image moves 50PX to the left and 50PX to the right. the third example is the effect of moving the graph with the upper value. this should be easy to understand. in summary, all of the above are based on the 0, 0 points of the DIV as the reference point. If the DIV area is interpreted as a coordinate axis, the image is negative to the left, right, and bottom is a positive value. it seems that you can't remember so much, so I will tell you what you should remember! In practice, most of the situations are that we know that the position of an image is the yellow point in the figure above. We should calculate its length on the figure to get that the two values are both 150PX, then we need to write background-position:-150px-150px to define the position of the graph, the volume method on the graph is shown in the figure below (this figure is equivalent to an effect chart or a design drawing). Remember this figure and you will surely be able to grasp the negative value of the background image.
Related Article

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.