多个CSS背景,图像上的颜色,被忽略

发布于 2024-11-04 06:40:27 字数 137 浏览 0 评论 0原文

这个多背景 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

花开柳相依 2024-11-11 06:40:27

解决方案是使用:

{-moz-linear-gradient(top, rgba(0, 0, 0, 0.5) 0%, rgba(0, 0, 0, 0.5) 100%), url(bg.png) repeat 0 0;}  

而不是:

rgba(0, 0, 0, 0.5)

The solutions is using:

{-moz-linear-gradient(top, rgba(0, 0, 0, 0.5) 0%, rgba(0, 0, 0, 0.5) 100%), url(bg.png) repeat 0 0;}  

instead of:

rgba(0, 0, 0, 0.5)
咋地 2024-11-11 06:40:27

CSS3 背景中 background 的语法为 [; , ]*,这意味着零个或多个 ,然后是单个 ,彼此之间用逗号分隔。请参阅​​http://www.w3.org/TR/css3-background/#the-background

定义为:

<final-bg-layer> = <bg-image> || <bg-position> [ / <bg-size> ]? || 
                   <repeat-style> || <attachment> || <box>{1,2} ||
                   <'background-color'>

为:(

 <bg-layer> = <bg-image> || <bg-position> [ / <bg-size> ]? ||
              <repeat-style> || <attachment> || <box>{1,2}

处的两个定义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-background

A <final-bg-layer> is defined as:

<final-bg-layer> = <bg-image> || <bg-position> [ / <bg-size> ]? || 
                   <repeat-style> || <attachment> || <box>{1,2} ||
                   <'background-color'>

whereas a <bg-layer> is:

 <bg-layer> = <bg-image> || <bg-position> [ / <bg-size> ]? ||
              <repeat-style> || <attachment> || <box>{1,2}

(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.

离线来电— 2024-11-11 06:40:27

您应该注意,由于渐变被视为图像,因此可以接受并且可以放入具有相同顶部和底部颜色的渐变。

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.

我还不会笑 2024-11-11 06:40:27

它应该是 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 ,

不忘初心 2024-11-11 06:40:27

奇怪的是,它似乎归结为参数的顺序;背景图像然后背景颜色:

background: url('http://davidrhysthomas.co.uk/linked/astrid_avatar.png') no-repeat 50% 50%, rgba(255,180,0,0.8);

有效(JS Fiddle demo ),而背景颜色然后背景图像:

background: rgba(255,180,0,0.8), url('http://davidrhysthomas.co.uk/linked/astrid_avatar.png') no-repeat 50% 50%;

不(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:

background: url('http://davidrhysthomas.co.uk/linked/astrid_avatar.png') no-repeat 50% 50%, rgba(255,180,0,0.8);

Works (JS Fiddle demo), while background-color then background-image:

background: rgba(255,180,0,0.8), url('http://davidrhysthomas.co.uk/linked/astrid_avatar.png') no-repeat 50% 50%;

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.

与往事干杯 2024-11-11 06:40:27

离开 Oscar 的很好的解决方案(谢谢!),以下是我如何使用 SASS/Compass 来实现它以自动添加浏览器前缀。

@include background( linear-gradient( color-stops(rgba(255, 66, 78, 0.25), rgba(255, 66, 78, 0.25)) ), image-url('/img/cardboard_flat.png') );

这支持 Webkit、Firefox,但不支持 IE9(因为渐变)。然后我想起了用于生成 PNG 的很棒的指南针 rgbapng Ruby gem: https://github.com/aaronrussell/compass- rgbapng

@include background( png_base64( rgba(255, 66, 78, 0.25) ), image-url('/img/cardboard_flat.png') );

现在,它支持 IE9+ 和其他支持多背景的浏览器。

如果您仍然需要 IE8 支持,您可以使用多背景 polyfill,或者设置 ::after 伪元素的样式并绝对定位它,z-index 为 -1:

html {
  height: 100%;
}

body {
  background: url('/img/cardboard_flat.png');
  position: relative;
  padding: 1px 0;
  min-height: 100%;

  &:after {
    content: "";
    position: absolute;
    background: png_base64( rgba(255, 66, 78, 0.25) );
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    z-index: -1;
  }
}

Going off of Oscar's nice solution (thanks!), here is how I implemented it using SASS/Compass to automate browser prefixing

@include background( linear-gradient( color-stops(rgba(255, 66, 78, 0.25), rgba(255, 66, 78, 0.25)) ), image-url('/img/cardboard_flat.png') );

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

@include background( png_base64( rgba(255, 66, 78, 0.25) ), image-url('/img/cardboard_flat.png') );

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:

html {
  height: 100%;
}

body {
  background: url('/img/cardboard_flat.png');
  position: relative;
  padding: 1px 0;
  min-height: 100%;

  &:after {
    content: "";
    position: absolute;
    background: png_base64( rgba(255, 66, 78, 0.25) );
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    z-index: -1;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文