3

我正在尝试在背景图像上叠加 CSS 渐变。我已经让它在 Firefox 中工作,但在 safari 和 chrome 中我只得到背景图像,没有渐变。

(我使用http://www.colorzilla.com/gradient-editor/生成了渐变代码,然后在每一行的末尾简单地添加了 url() )

更新:似乎我的问题可能是 webkit 显示图像后面的渐变,而不是像我需要的那样在顶部,而 Firefox 确实如此。

  background: -moz-linear-gradient(top, rgba(0, 0, 0, 0) 0%, rgba(99, 130, 169, 0.7) 100%), url(app/bg.jpg) no-repeat center center fixed; /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0, 0, 0, 0)), color-stop(100%, rgba(99, 130, 169, 0.7))), url(app/bg.jpg) no-repeat center center fixed; /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top, rgba(0, 0, 0, 0) 0%,rgba(0,0,0,0.16) 100%), url(app/bg.jpg) no-repeat center center fixed; /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(top, rgba(0, 0, 0, 0) 0%,rgba(99, 130, 169, 0.7) 100%), url(app/bg.jpg) no-repeat center center fixed; /* Opera 11.10+ */
  background: -ms-linear-gradient(top, rgba(0, 0, 0, 0) 0%,rgba(99, 130, 169, 0.7) 100%), url(app/bg.jpg) no-repeat center center fixed; /* IE10+ */
  background: linear-gradient(to bottom, rgba(0, 0, 0, 0) 0%,rgba(99, 130, 169, 0.7) 100%), url(app/bg.jpg) no-repeat center center fixed; /* W3C */
4

3 回答 3

7

在这种情况下,我建议使用速记符号,它会使阅读和维护变得更加容易(并且可以解决问题)。这是一个工作示例(和一个演示:http: //jsfiddle.net/joshnh/yyy7V/):

.foo {
    background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, rgba(0, 0, 0, 0)), color-stop(100%, rgba(99, 130, 169, 0.7))), url('http://lorempixel.com/400/300/'); /* Chrome,Safari4+ */
    background-image: -webkit-linear-gradient(top, transparent, rgba(99,130,169,.7)), url('http://lorempixel.com/400/300/'); /* Chrome10+,Safari5.1+ */
    background-image:    -moz-linear-gradient(top, transparent, rgba(99,130,169,.7)), url('http://lorempixel.com/400/300/'); /* FF3.6+ */
    background-image:     -ms-linear-gradient(top, transparent, rgba(99,130,169,.7)), url('http://lorempixel.com/400/300/'); /* IE10+ */
    background-image:      -o-linear-gradient(top, transparent, rgba(99,130,169,.7)), url('http://lorempixel.com/400/300/'); /* Opera 11.10+ */
    background-image:         linear-gradient(to bottom, transparent, rgba(99,130,169,0.7)), url('http://lorempixel.com/400/300/'); /* W3C */

    background-repeat: no-repeat;
    background-position: 50% 50%;
    background-attachment: fixed;
}
于 2012-09-17T23:50:03.137 回答
1

图像在 WebKit 中的渐变后面。只是 WebKit 代码使用了另一种颜色,rgba(0,0,0,0.16),这比用于其他浏览器的颜色更难发现rgba(99,130,169,0.7), 。

于 2012-09-18T12:46:22.967 回答
0

我相信这里的问题来自您的 CSS 的排序。 试试 Chris Coyier 的方法。

.gradient-bg {
   background-color: #1a82f7; 
   background-image: url(images/fallback-gradient.png); 
   background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#2F2727), to(#1a82f7));
   background-image: -webkit-linear-gradient(top, #2F2727, #1a82f7); 
   background-image:    -moz-linear-gradient(top, #2F2727, #1a82f7);
   background-image:     -ms-linear-gradient(top, #2F2727, #1a82f7);
   background-image:      -o-linear-gradient(top, #2F2727, #1a82f7);
}
于 2012-09-17T23:40:27.697 回答