我如何使用CSS3渐变为我的背景颜色,然后应用背景图像应用某种轻透明纹理?
当前回答
我也是这么想的。虽然背景-颜色和背景-图像存在于对象内的单独层中——这意味着它们可以共存——但CSS渐变似乎利用了背景-图像层。
据我所知,border-image似乎比多背景有更广泛的支持,所以这可能是一种替代方法。
http://articles.sitepoint.com/article/css3-border-images
更新:更多的研究。看来佩特拉·格里戈罗娃在这里有东西在工作> http://petragregorova.com/demos/css-gradient-and-bg-image-final.html
其他回答
下面是我创建的一个MIXIN,用来处理人们可能喜欢使用的所有东西:
.background-gradient-and-image (@fallback, @imgUrl, @background-position-x, @background-position-y, @startColor, @endColor) {
background: @fallback;
background: url(@imgUrl) @background-position-x @background-position-y no-repeat; /* fallback */
background: url(@imgUrl) @background-position-x @background-position-y no-repeat, -webkit-gradient(linear, left top, left bottom, from(@startColor) @background-position-x @background-position-y no-repeat, to(@endColor)); /* Saf4+, Chrome */
background: url(@imgUrl) @background-position-x @background-position-y no-repeat, -webkit-linear-gradient(top, @startColor, @endColor); /* Chrome 10+, Saf5.1+ */
background: url(@imgUrl) @background-position-x @background-position-y no-repeat, -moz-linear-gradient(top, @startColor, @endColor); /* FF3.6+ */
background: url(@imgUrl) @background-position-x @background-position-y no-repeat, -ms-linear-gradient(top, @startColor, @endColor); /* IE10 */
background: url(@imgUrl) @background-position-x @background-position-y no-repeat, -o-linear-gradient(top, @startColor, @endColor); /* Opera 11.10+ */
background: url(@imgUrl) @background-position-x @background-position-y no-repeat, linear-gradient(top, @startColor, @endColor); /* W3C */
}
可以这样使用:
.background-gradient-and-image (#f3f3f3, "../images/backgrounds/community-background.jpg", left, top, #fafcfd, #f2f2f2);
希望这对你们有帮助。
这要归功于@Gidgidonihah找到了最初的解决方案。
我用那种方法解决问题。我在HTML中定义渐变,在主体中定义背景图像
html { Background-image: -webkit-gradient(线性,左下,右上,颜色停止(0.31,rgb(227, 227, 227)),颜色停止(0.66,rgb(199, 199, 199)),颜色停止(0.83,rgb(184, 184, 184)); 背景-图像:-moz-linear-gradient(左下,rgb(227, 227, 227) 31%, rgb(199, 199, 199) 66%, rgb(184, 184, 184) 83%); 高度:100% } 身体{ 背景:url(“http://www.skrenta.com/images/stackoverflow.jpg”); 高度:100% }
如果你想要一个在中心只有一个背景图像的渐变,你可以用一行代码做到,像这样:
body {
background: url(logo.png) no-repeat fixed center center, linear-gradient(#00467f, #a5cc82) fixed;
}
对于我的响应式设计,我的下拉框在框的右侧(垂直手风琴),接受百分比作为位置。最初,下箭头是“位置:绝对;右:13 px;”。在97%的定位下,它的工作原理如下:
> background: #ffffff;
> background-image: url(PATH-TO-arrow_down.png); /*fall back - IE */
> background-position: 97% center; /*fall back - IE */
> background-repeat: no-repeat; /*fall back - IE */
> background-image: url(PATH-TO-arrow_down.png) no-repeat 97% center;
> background: url(PATH-TO-arrow_down.png) no-repeat 97% center, -moz-linear-gradient(top, #ffffff 1%, #eaeaea 100%);
> background: url(PATH-TO-arrow_down.png) no-repeat 97% center, -webkit-gradient(linear, left top, left bottom, color-stop(1%,#ffffff), color-stop(100%,#eaeaea));
> background: url(PATH-TO-arrow_down.png) no-repeat 97% center, -webkit-linear-gradient(top, #ffffff 1%,#eaeaea 100%);
> background: url(PATH-TO-arrow_down.png) no-repeat 97% center, -o-linear-gradient(top, #ffffff 1%,#eaeaea 100%);<br />
> filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#eaeaea',GradientType=0 );
附注:对不起,我不知道如何处理滤镜。
如果你有奇怪的错误下载背景图像使用W3C链接检查:https://validator.w3.org/checklink
以下是我使用的现代mixin (credit: PSA:不要使用梯度生成器):
.buttonAkc
{
.gradientBackground(@imageName: 'accept.png');
background-repeat: no-repeat !important;
background-position: center right, top left !important;
}
.buttonAkc:hover
{
.gradientBackgroundHover('accept.png');
}
.gradientBackground(@startColor: #fdfdfd, @endColor: #d9d9db, @imageName)
{
background-color: mix(@startColor, @endColor, 60%); // fallback
background-image: url("@{img-folder}/@{imageName}?v=@{version}"); // fallback
background: url("@{img-folder}/@{imageName}?v=@{version}") no-repeat scroll right center, -webkit-linear-gradient(top, @startColor 0%, @endColor 100%) no-repeat scroll left top; // Chrome 10-25, Safari 5.1-6
background: url("@{img-folder}/@{imageName}?v=@{version}") no-repeat scroll right center, linear-gradient(to bottom, @startColor 0%, @endColor 100%) no-repeat scroll left top;
}
.gradientBackgroundHover(@imageName)
{
.gradientBackground(#fdfdfd, #b5b6b9, @imageName);
}