4

I'm programming a little WP theme and need a little help with the new CSS3 background-size property.

I have a DIV which contains an image as background. The image itself is set bigger than the div to present a little cut-out of the original image:

.imageContainer {
    background-size:154% 102%;
    background-position-x:-28%;
    background-position-y:0.28%;
}

So far everything is fine. But invoking the background-position property gets tricky if the size is ≥ 100%. I put together a little JS/CSS Playground for testing:

  • If the image is ≤ 99% wide, less background-position-x means the image goes left.
  • If the image is == 100% wide, background-position-x does nothing
  • If the image is ≥ 101% wide, less background-position-x means the image goes right.

For the following case I calculated:

  • Big container: 350x350px
  • Image: 540x359px
  • Means: (100/350) * 540 ≙ 154% width
  • (100/350) * 359 ≙ 102% height.
  • Also: position-x: -28% and position-y: -0.28%

So I rendered a page (incl. automatic calculations) and the size is about the right size. But as I said, less background-position-x (-28%) means the image goes right, the image has the wrong position.

It would have to be moved left, because I calculated -28% "left margin". So I did a bit trial and error and it turned out that the right position would be at something around 50%.

Is this still an CSS3 issue, or is it my complete misunderstanding of the functionality of this property?

EDIT: here is an JSFiddle too

Here is a picture to illustrate my target… enter image description here

Julian F. Weinert
  • 7,474
  • 7
  • 59
  • 107
  • 1
    If you could put together a http://jsfiddle.net demonstrating the issue with links to real images it would make it much easier for us to help you. Also for the record `background-position: 0 0` is the valid syntax. `background-postion-x` and `background-postion-y` are not actually part of the W3C spec and are not supported in Firefox or Opera. – tw16 Feb 23 '13 at 18:09
  • You'r right about background-position-x / y. I thought it would be easier to read like this. I'm developing on Safari so it will work. But I'll remove this syntax when I'm going online. So this is kind of production state :) I'll put a fiddle together in a few minutes. – Julian F. Weinert Feb 23 '13 at 18:21
  • There you go http://jsfiddle.net/FJ3Ub/ – Julian F. Weinert Feb 23 '13 at 18:33

1 Answers1

5

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

vals
  • 61,425
  • 11
  • 89
  • 138
  • So thats the thing about this property I didn't find documented. This is a bit strange and doesn't seem to be quite well-considered... But we need to use it as it is :) So can you also give me a hint how to convert this **match** percentage into **space** percentage? And: is the behaviour with px the same? – Julian F. Weinert Feb 24 '13 at 05:42
  • I guess it is an extension of the property keywords. When you specify center, you are positioning the center of the image in the center of the container. When you are specifying right, you are positioning the right of the image on the right of the container... The absolute values (px, or whatever) work the way that you would expect them :-) I will try to give you the formula that you ask – vals Feb 24 '13 at 08:37
  • I think **a=x/100** or **x=a/100**. – Mohsenme Jun 20 '16 at 11:24
  • And the position of the image from the container should be specified as **an-afn=an(1-f)** in pixels. http://codepen.io/Mohsenme/pen/KMgBew – Mohsenme Jun 20 '16 at 11:57
  • someone visualized this here https://css-tricks.com/i-like-how-percentage-background-position-works/ – commonpike Dec 03 '17 at 21:39