CSS3 background-size and background-position issue

為{幸葍}努か 提交于 2019-11-30 22:17:00

The key to your problems is that percentage that you specify gives the point where the container and the image match. This point is calculated both in the image and in the container.

So, if you want the image centered, that means that the center of the image is in the center of the container. So, this is the value that you find by trial and error.

The key here is 50% as background-position always gets the image centered, and you don't need any of your calculations

If you specify 10% for the property, that would mean that the point at 10% from the left in the image is positioned at the point at 10% from the left in the container.

formula for this

How to convert from percentage to px (as requested). Lets say that you have a container o size n and the image is greater by a factor of f You specify a background position of x%. We take this as an unitary factor a being a=x*100

The position to match in the container is an. The position to match in the image is afn. The position of the image from the container is the difference, afn-an , that can be given as an(f-1).

That explains why:

The apparent result of the property is inverted when f > 1 . (the image is bigger than the container.

The result is nil when f = 1 (the image is the same size than the container)

Now to convert that to space percentage, you just divide by the size of the container (n) to give

a(f-1)

or divide by the size of the image (fn) to give

a(f-1)/f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!