[英]CSS background-position ignored when using background-size
I'm trying to hide a background-image
(that is scaled with background-size
) using background-position
. 我正在尝试使用
background-position
隐藏background-image
(使用background-size
缩放)。
I'm purposely not using visibility
or anything that will cause a relayout as such has been causing minor fluctuations in the rendered output when toggling back into view. 我故意不使用
visibility
或任何会导致重新布局的东西,因为在切换回视图时,渲染输出会引起轻微波动。 Additionally, my current application limits me from using pseudo-elements as a hack/workaround. 此外,我当前的应用程序限制我使用伪元素作为黑客/解决方法。
div {
background: url(image.png) no-repeat 200% 200%/100%;
height: 100px; /* half height of image */
width: 250px; /* half width of image */
}
Unfortunately, the image is not getting positioned. 不幸的是,图像没有定位。 If the
/100%
is removed, the positioning works correctly. 如果删除了
/100%
,则定位正常。
jsfiddle: http://jsfiddle.net/prometh/60jtpust/ jsfiddle: http : //jsfiddle.net/prometh/60jtpust/
Update: 更新:
Curiously, it works when the background-size
is 50%: http://jsfiddle.net/60jtpust/8/ 奇怪的是,当
background-size
为50%时,它可以工作: http : //jsfiddle.net/60jtpust/8/
The problem is that 问题是
3.9.
3.9。 Sizing Images: the ' background-size ' property
调整图片大小:“ background-size ”属性
A percentage is relative to the background positioning area.
相对于背景定位区域的百分比。
And 和
3.6.
3.6。 Positioning Images: the ' background-position ' property
定位图像:' background-position '属性
Percentages: refer to size of background positioning area minus size of background image, [...] where the size of the image is the size given by ' background-size '.
百分比:指背景定位区域的大小减去背景图像的大小,[...]图像的大小是' background-size '给出的大小 。
Therefore, if you use a background-size
of 100%
, the percentage is background-position
will refer to 0
. 因此,如果您使用
100%
的background-size
,则background-position
的百分比将表示0
。 So it will be 0
. 所以它将是
0
。
Percentage values for background-position
have funky behavior with relation to background-size
, which I explain in depth, complete with a sliding puzzle analogy, in this answer . background-position
百分比值具有与background-size
相关的时髦行为,我在这个答案中进行了深入解释,并附有滑动拼图类比。 Unfortunately, because the background image fits the box exactly due to background-size: 100%
, you won't be able to position it using percentage values. 不幸的是,因为背景图像完全由于
background-size: 100%
而适合框,所以您将无法使用百分比值来定位它。 From the final paragraph of my answer: 从我的答案的最后一段:
If you want to position a background image whose size is 100% of the background area, you won't be able to use a percentage, since you can't move a tile that fits its frame perfectly (which incidentally would make for either the most boring puzzle or the perfect prank).
如果要定位大小为背景区域100%的背景图像,则无法使用百分比,因为您无法完美地移动适合其框架的图块(顺便提一下最无聊的谜题或完美的恶作剧)。 This applies whether the intrinsic dimensions of the background image match the dimensions of the element, or you explicitly set
background-size: 100%
.无论背景图像的内在尺寸是否与元素的尺寸匹配,或者您明确设置
background-size: 100%
。 So, to position the image, you will need to use use an absolute value instead (forgoing the sliding-puzzle analogy altogether).因此,要定位图像,您需要使用绝对值(完全放弃滑动拼图类比)。
The reason it works with background-size: 50%
is because the image is now given space to move around. 它适用于
background-size: 50%
的原因background-size: 50%
是因为图像现在有空间可以移动。 At the same time, the sliding puzzle analogy now falls flat because the percentage values you've set for background-position
are greater than 100%... 与此同时,滑动拼图类比现在变得平淡,因为您为
background-position
设置的百分比值大于100%......
Anyway, in your specific example, the absolute values are equal to your element's width
and height
properties respectively (note: not the actual image dimensions): 无论如何,在您的具体示例中,绝对值分别等于元素的
width
和height
属性(注意:不是实际的图像尺寸):
div {
background: url(image.png) no-repeat 250px 100px/100%;
height: 100px; /* half height of image */
width: 250px; /* half width of image */
}
If you cannot hardcode these values, eg if you need this effect to apply across elements of different sizes, then unfortunately you will not be able to use background-position
to hide the image. 如果您无法对这些值进行硬编码,例如,如果您需要将此效果应用于不同大小的元素,那么很遗憾,您将无法使用
background-position
来隐藏图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.