多个CSS背景,图像上的颜色,被忽略
这个多背景 CSS 行有什么问题吗? Firefox 4 会忽略它(就像出现语法错误时一样)。
background: rgba(255,0,0,0.2), url("static/menubg.jpg");
What's wrong with this multiple background CSS line. Firefox 4 ignores it (as it does when there's a syntax error).
background: rgba(255,0,0,0.2), url("static/menubg.jpg");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
解决方案是使用:
而不是:
The solutions is using:
instead of:
CSS3 背景中
background
的语法为[; , ]*
,这意味着零个或多个
,然后是单个。
,彼此之间用逗号分隔。请参阅http://www.w3.org/TR/css3-background/#the-background
定义为:而
为:(处的两个定义href="http://www.w3.org/TR/css3-background/#ltbg-layergt">http://www.w3.org/TR/css3-background/#ltbg-layergt ) 。
或者简单来说,只有最低的背景层可以包含背景颜色。所以是的,你的 CSS 实际上是一个语法错误。
哦,看起来 https://developer.mozilla.org/en/css/multiple_backgrounds 有一些错误它。我已经修好了它们。
The syntax for
background
in CSS3 Backgrounds is[ <bg-layer> , ]* <final-bg-layer>
, which means zero or more<bg-layer>s
and then a single<final-bg-layer>
, separated from each other by commas. See http://www.w3.org/TR/css3-background/#the-backgroundA
<final-bg-layer>
is defined as:whereas a
<bg-layer>
is:(both definitions at http://www.w3.org/TR/css3-background/#ltbg-layergt ).
Or in simple terms, only the lowest background layer can include a background color. So yes, your CSS is in fact a syntax error.
Oh, and looks like https://developer.mozilla.org/en/css/multiple_backgrounds had some errors in it. I've fixed them.
您应该注意,由于渐变被视为图像,因此可以接受并且可以放入具有相同顶部和底部颜色的渐变。
You should note that because gradients are treated as images it is acceptable and works to put in a gradient that has the same top and bottom colour.
它应该是
background: rgba(255,0,0,0.2) url("static/menubg.jpg");
,不带,
It should be
background: rgba(255,0,0,0.2) url("static/menubg.jpg");
without the,
奇怪的是,它似乎归结为参数的顺序;背景图像然后背景颜色:
有效(JS Fiddle demo ),而背景颜色然后背景图像:
不(JS Fiddle)。
以上测试是在 Chromium 11 和 Firefox 4 上进行的,均在 Ubuntu 11.04 上进行。
Edited to note that this does, indeed, come down to the order; as definitively answered in @Boris' answer.
Oddly enough it seems to come down to the order of the parameters; the background-image then background-color:
Works (JS Fiddle demo), while background-color then background-image:
Does not (JS Fiddle).
The above tested on Chromium 11 and Firefox 4, both on Ubuntu 11.04.
Edited to note that this does, indeed, come down to the order; as definitively answered in @Boris' answer.
离开 Oscar 的很好的解决方案(谢谢!),以下是我如何使用 SASS/Compass 来实现它以自动添加浏览器前缀。
这支持 Webkit、Firefox,但不支持 IE9(因为渐变)。然后我想起了用于生成 PNG 的很棒的指南针 rgbapng Ruby gem: https://github.com/aaronrussell/compass- rgbapng
现在,它支持 IE9+ 和其他支持多背景的浏览器。
如果您仍然需要 IE8 支持,您可以使用多背景 polyfill,或者设置 ::after 伪元素的样式并绝对定位它,z-index 为 -1:
Going off of Oscar's nice solution (thanks!), here is how I implemented it using SASS/Compass to automate browser prefixing
This supports Webkit, Firefox, but not IE9 (because of the gradient). Then I remembered the awesome compass rgbapng Ruby gem for generating PNGs: https://github.com/aaronrussell/compass-rgbapng
Now, this supports IE9+ and the rest of the browsers that support multiple backgrounds.
If you still need IE8 support, you could either use a multi-background polyfill, or style an ::after pseudo element and absolutely position it, with a z-index of -1: