我如何使用CSS3渐变为我的背景颜色,然后应用背景图像应用某种轻透明纹理?


当前回答

我有一个实现,我需要把这个技术更进一步,并想概述我的工作。下面的代码做同样的事情,但使用SASS、Bourbon和一个图像精灵。

    @mixin sprite($position){
        @include background(url('image.png') no-repeat ($position), linear-gradient(#color1, #color2));
    }
    a.button-1{
        @include sprite(0 0);
    }
    a.button-2{
       @include sprite (0 -20px);  
    }
    a.button-2{
       @include sprite (0 -40px);  
    }

SASS和Bourbon负责跨浏览器代码,现在我所要声明的就是每个按钮的精灵位置。很容易将此原则扩展到按钮的活动状态和悬停状态。

其他回答

我也是这么想的。虽然背景-颜色和背景-图像存在于对象内的单独层中——这意味着它们可以共存——但CSS渐变似乎利用了背景-图像层。

据我所知,border-image似乎比多背景有更广泛的支持,所以这可能是一种替代方法。

http://articles.sitepoint.com/article/css3-border-images

更新:更多的研究。看来佩特拉·格里戈罗娃在这里有东西在工作> http://petragregorova.com/demos/css-gradient-and-bg-image-final.html

要意识到的一件事是,第一个定义的背景图像在堆栈中最上面。最后定义的图像将位于最底部。这意味着,要在图像后面有一个背景渐变,你需要:

身体{ 背景-图像:url(“http://www.skrenta.com/images/stackoverflow.jpg”),线性渐变(红,黄); Background-image: url("http://www.skrenta.com/images/stackoverflow.jpg"), -webkit-gradient(线性,左上,左下,从(红色),到(黄色)); Background-image: url("http://www.skrenta.com/images/stackoverflow.jpg"), -moz-linear-gradient(顶部,红色,黄色); }

您还可以为图像定义背景位置和背景大小。 我写了一篇关于CSS3渐变可以做的有趣事情的博文

如果你想要一个在中心只有一个背景图像的渐变,你可以用一行代码做到,像这样:

body {
  background:  url(logo.png) no-repeat fixed center center, linear-gradient(#00467f, #a5cc82) fixed;
}

我有一个实现,我需要把这个技术更进一步,并想概述我的工作。下面的代码做同样的事情,但使用SASS、Bourbon和一个图像精灵。

    @mixin sprite($position){
        @include background(url('image.png') no-repeat ($position), linear-gradient(#color1, #color2));
    }
    a.button-1{
        @include sprite(0 0);
    }
    a.button-2{
       @include sprite (0 -20px);  
    }
    a.button-2{
       @include sprite (0 -40px);  
    }

SASS和Bourbon负责跨浏览器代码,现在我所要声明的就是每个按钮的精灵位置。很容易将此原则扩展到按钮的活动状态和悬停状态。

作为一个可靠的方法,你可以只做一个背景图像是500x5像素,在你的css使用:

background-img:url(bg.jpg) fixed repeat-x;
background:#<xxxxxx>;

其中xxxxxx对应与最终渐变颜色相匹配的颜色。

你也可以把它固定在屏幕的底部,让它与初始渐变颜色相匹配。