简体   繁体   English

使用background-size时忽略CSS background-position

[英]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): 无论如何,在您的具体示例中,绝对值分别等于元素的widthheight属性(注意:不是实际的图像尺寸):

div {
    background: url(image.png) no-repeat 250px 100px/100%;
    height: 100px;   /* half height of image */
    width: 250px;    /* half width of image */
}

Updated fiddle 更新了小提琴

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.

相关问题 CSS3背景大小和背景位置问题 - CSS3 background-size and background-position issue CSS背景大小和背景位置问题 - CSS Background-Size and Background-Position Issue CSS背景大小封面和背景位置 - CSS background-size cover and background-position 当背景大小为100%时,如何使用百分比为背景位置设置动画? - How to animate background-position using percentages when background-size is 100%? CSS 失败 - 背景位置、背景重复、背景大小、背景附件 - CSS Fail - background-position, background-repeat, background-size, background-attachment 使用 background-size=cover 的背景图像时,如何可靠地使用背景位置的相对单位? - How can you reliably use relative units for background-position when using a background-image with background-size=cover? 背景位置的背景大小不会缩放位置? - background-size with background-position doesn't scale the position? AngularJS的Internet Explorer背景位置和背景大小 - Internet explorer background-position and background-size by AngularJS Opera是否错误处理背景位置和/或背景大小与百分比值? - Is Opera mishandling background-position and/or background-size with percentage values? img 标签上的背景大小和背景位置 - Background-size and background-position on an img tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM