The key to your problems is that the percentage you specify gives the point at which the container and image match . This point is calculated both in the image and in the container.
So, if you want the image centered, it means that the center of the image is in the center of the container. Thus, this is the value that you discover as a result of trial and error.
The key here is 50%, because the background-position always gets a centered image, and you don't need any of your calculations
If you specify 10% for the property, this will mean that the point at 10% on the left in the image will be at the point with 10% on the left in the container.
for this
How to convert from percent to px (on request). Suppose you have a container of size n and the image is larger in f . You specify the background position x %. We take this as the unit factor a , which is a = x * 100
Matching item in container a . The position corresponding to the image, afn . The position of the image from the container is the difference, afn-a , which can be specified as a (f-1) .
This explains why:
The obvious result of the property is inverted for f> 1. (the image is larger than the container.
The result is nil when f = 1 (image is the same size as the container)
Now, to convert this percentage to space, you simply divide by the size of the container ( n ) to give
A (F-1)
or divide the image size (fn) to give
a (F-1) / f
source share