我如何使用CSS3渐变为我的背景颜色,然后应用背景图像应用某种轻透明纹理?
当前回答
如果你必须在IE 9 (HTML 5 & HTML 4.01 Strict)中让渐变和背景图像一起工作,添加以下属性声明到你的css类中,它应该可以做到:
filter: progid:DXImageTransform.Microsoft.gradient(GradientType=0, startColorstr='#000000', endColorstr='#ff00ff'), progid:DXImageTransform.Microsoft.AlphaImageLoader(src='[IMAGE_URL]', sizingMethod='crop');
注意,您使用了filter属性,并且progid有两个实例:[val],在用分号关闭属性值之前,用逗号分隔。这是小提琴。还要注意,当你看小提琴时,梯度延伸到圆角之外。我没有解决其他不使用圆角。还要注意,当在src [IMAGE_URL]属性中使用相对路径时,该路径是相对于文档页面而不是css文件(参见源代码)。
这篇文章(http://coding.smashingmagazine.com/2010/04/28/css3-solutions-for-internet-explorer/)引导我找到了这个解决方案。它对特定于ie的CSS3非常有帮助。
其他回答
如果你必须在IE 9 (HTML 5 & HTML 4.01 Strict)中让渐变和背景图像一起工作,添加以下属性声明到你的css类中,它应该可以做到:
filter: progid:DXImageTransform.Microsoft.gradient(GradientType=0, startColorstr='#000000', endColorstr='#ff00ff'), progid:DXImageTransform.Microsoft.AlphaImageLoader(src='[IMAGE_URL]', sizingMethod='crop');
注意,您使用了filter属性,并且progid有两个实例:[val],在用分号关闭属性值之前,用逗号分隔。这是小提琴。还要注意,当你看小提琴时,梯度延伸到圆角之外。我没有解决其他不使用圆角。还要注意,当在src [IMAGE_URL]属性中使用相对路径时,该路径是相对于文档页面而不是css文件(参见源代码)。
这篇文章(http://coding.smashingmagazine.com/2010/04/28/css3-solutions-for-internet-explorer/)引导我找到了这个解决方案。它对特定于ie的CSS3非常有帮助。
我总是使用以下代码使其工作。这里有一些注意事项:
如果你把图片URL放在渐变前面,这张图片将会显示在渐变上面。
.background-gradient { 背景:url('http://trungk18.github.io/img/trungk18.png') no-repeat, -moz-linear-gradient(135deg, #6ec575 0, #3b8686 100%); 背景:url('http://trungk18.github.io/img/trungk18.png') no-repeat, -webkit-gradient(135deg, #6ec575 0, #3b8686 100%); 背景:url('http://trungk18.github.io/img/trungk18.png') no-repeat, -webkit-linear-gradient(135deg, #6ec575 0, #3b8686 100%); 背景:url('http://trungk18.github.io/img/trungk18.png') no-repeat, -o-linear-gradient(135deg, #6ec575 0, #3b8686 100%); 背景:url('http://trungk18.github.io/img/trungk18.png') no-repeat, -ms-linear-gradient(135deg, #6ec575 0, #3b8686 100%); 背景:url('http://trungk18.github.io/img/trungk18.png')无重复,线性梯度(135deg, #6ec575 0, #3b8686 100%); 身高:500 px; 宽度:500 px; } < div class = " background-gradient " > < / div >
如果你把渐变放在图片URL之前,这张图片会显示在渐变下面。
.background-gradient { 背景:-moz-linear-gradient(135deg, #6ec575 0, #3b8686 100%), url('http://trungk18.github.io/img/trungk18.png') no-repeat; Background: -webkit-gradient(135deg, #6ec575 0, #3b8686 100%), url('http://trungk18.github.io/img/trungk18.png') no-repeat; 背景:-webkit-linear-gradient(135deg, #6ec575 0, #3b8686 100%), url('http://trungk18.github.io/img/trungk18.png') no-repeat; 背景:-o-linear-gradient(135deg, #6ec575 0, #3b8686 100%), url('http://trungk18.github.io/img/trungk18.png') no-repeat; 背景:-ms-linear-gradient(135deg, #6ec575 0, #3b8686 100%), url('http://trungk18.github.io/img/trungk18.png') no-repeat; 背景:线性梯度(135deg, #6ec575 0, #3b8686 100%), url('http://trungk18.github.io/img/trungk18.png') no-repeat; 宽度:500 px; 身高:500 px; } < div class = " background-gradient " > < / div >
这种技术就像我们在这里描述的有多个背景图像一样
如果你想要一个在中心只有一个背景图像的渐变,你可以用一行代码做到,像这样:
body {
background: url(logo.png) no-repeat fixed center center, linear-gradient(#00467f, #a5cc82) fixed;
}
如果你有奇怪的错误下载背景图像使用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);
}
我用那种方法解决问题。我在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% }