简体   繁体   English

CSS3背景大小和背景位置问题

[英]CSS3 background-size and background-position issue

I'm programming a little WP theme and need a little help with the new CSS3 background-size property. 我正在编写一个WP主题,需要一些新的CSS3 background-size属性的帮助。

I have a DIV which contains an image as background. 我有一个DIV,其中包含一个图像作为背景。 The image itself is set bigger than the div to present a little cut-out of the original image: 图像本身设置得比div大,以显示原始图像的一点切口:

.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%. 但是,如果大小≥100%,则调用background-position属性会变得棘手。 I put together a little JS/CSS Playground for testing: 我把一个小JS / CSS游乐场放在一起进行测试:

  • If the image is ≤ 99% wide, less background-position-x means the image goes left. 如果图像宽度≤99%,则背景位置x越小意味着图像向左移动。
  • If the image is == 100% wide, background-position-x does nothing 如果图像= = 100%宽,则background-position-x不执行任何操作
  • If the image is ≥ 101% wide, less background-position-x means the image goes right. 如果图像宽度≥101%,则背景位置x越小意味着图像正确。

For the following case I calculated: 对于以下情况,我计算了:

  • Big container: 350x350px 大容器:350x350px
  • Image: 540x359px 图片:540x359px
  • Means: (100/350) * 540 ≙ 154% width 意思是:(100/350)* 540≙154%宽度
  • (100/350) * 359 ≙ 102% height. (100/350)* 359≙102%的高度。
  • Also: position-x: -28% and position-y: -0.28% 另外:position-x:-28%和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. 但正如我所说,较少的背景位置-x(-28%)意味着图像正确,图像的位置错误。

It would have to be moved left, because I calculated -28% "left margin". 它必须向左移动,因为我计算了-28%“左边距”。 So I did a bit trial and error and it turned out that the right position would be at something around 50%. 所以我做了一些试验和错误,结果发现正确的位置将达到50%左右。

Is this still an CSS3 issue, or is it my complete misunderstanding of the functionality of this property? 这仍然是一个CSS3问题,还是我完全误解了这个属性的功能?

EDIT: here is an JSFiddle too 编辑: 这里也是一个JSFiddle

Here is a picture to illustrate my target… 这是一张图片来说明我的目标...... 在此输入图像描述

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 这里的关键是50%,因为背景位置总是以图像为中心,而您不需要任何计算

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. 如果为属性指定10%,则意味着图像左侧10%处的点位于容器左侧10%处。

formula for this 这个公式

How to convert from percentage to px (as requested). 如何从百分比转换为px(根据要求)。 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 %. 假设你有一个大小为n的容器,图像大于f的因子你指定一个x %的背景位置。 We take this as an unitary factor a being a=x*100 我们把它作为一个单一因子a a = x * 100

The position to match in the container is an . 在容器中,以匹配位置是一个 The position to match in the image is afn . 图像中匹配的位置是afn The position of the image from the container is the difference, afn-an , that can be given as an(f-1) . 来自容器的图像的位置是差值, afn-an ,可以作为(f-1)给出

That explains why: 这解释了原因:

The apparent result of the property is inverted when f > 1 . 当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) 当f = 1时,结果为nil(图像大小与容器相同)

Now to convert that to space percentage, you just divide by the size of the container ( n ) to give 现在将其转换为空间百分比,您只需要除以容器( n )的大小即可

a(f-1) 一个(F-1)

or divide by the size of the image (fn) to give 或除以图像的大小(fn)给出

a(f-1)/f 一个(F-1)/ F

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM