首页 文章

如何在同一元素上组合背景图像和CSS3渐变?

提问于
浏览
1110

如何为我的 background-color 使用CSS3渐变然后应用 background-image 来应用某种浅透明纹理?

16 回答

  • 19

    我以这种方式解决了这个问题 . 我在HTML中定义了渐变,在Body中定义了背景图像

    html {
      background-image: -webkit-gradient(linear, left bottom, right top, color-stop(0.31, rgb(227, 227, 227)), color-stop(0.66, rgb(199, 199, 199)), color-stop(0.83, rgb(184, 184, 184)));
      background-image: -moz-linear-gradient(left bottom, rgb(227, 227, 227) 31%, rgb(199, 199, 199) 66%, rgb(184, 184, 184) 83%);
      height: 100%
    }
    body {
      background: url("http://www.skrenta.com/images/stackoverflow.jpg");
      height: 100%
    }
    
  • 0

    要意识到的是,第一个定义的背景图像位于堆栈的最顶层 . 最后定义的图像将位于最下方 . 这意味着,要在图像后面有背景渐变,您需要:

    body {
        background-image: url("http://www.skrenta.com/images/stackoverflow.jpg"), linear-gradient(red, yellow);
        background-image: url("http://www.skrenta.com/images/stackoverflow.jpg"), -webkit-gradient(linear, left top, left bottom, from(red), to(yellow));
        background-image: url("http://www.skrenta.com/images/stackoverflow.jpg"), -moz-linear-gradient(top, red, yellow);
      }
    

    您还可以定义图像的背景位置和背景大小 . 我整理了一篇关于你可以用CSS3 gradients做的一些有趣事情的博客文章

  • 13

    我想制作带有背景图像,背景渐变组合的span按钮 .

    http://enjoycss.com/帮助完成了我的工作任务 . 只有我必须删除一些自动生成的额外CSS . 但这是非常好的网站 Build 您的临时工作 .

    #nav a.link-style span {
        background: url("../images/order-now-mobile.png"), -webkit-linear-gradient(0deg, rgba(190,20,27,1) 0, rgba(224,97,102,1) 51%, rgba(226,0,0,1) 100%);
        background: url("../images/order-now-mobile.png"), -moz-linear-gradient(90deg, rgba(190,20,27,1) 0, rgba(224,97,102,1) 51%, rgba(226,0,0,1) 100%);
        background: url("../images/order-now-mobile.png"), linear-gradient(90deg, rgba(170,31,0,1) 0, rgba(214,18,26,1) 51%, rgba(170,31,0,1) 100%);
        background-repeat: no-repeat;
        background-position: 50% 50%;
        border-radius: 8px;
        border: 3px solid #b30a11;
    }
    
  • -1

    我的解决方案

    background-image: url(IMAGE_URL); /* fallback */
    
    background-image: linear-gradient(to bottom, rgba(0,0,0,0.7) 0%,rgba(0,0,0,0.7) 100%), url(IMAGE_URL);
    
  • 3

    我试图做同样的事情 . 虽然背景颜色和背景图像存在于对象内的不同层上 - 这意味着它们可以共存 - 但CSS渐变似乎可以选择背景图像层 .

    据我所知,边界图像似乎比多个背景有更广泛的支持,所以也许这是另一种方法 .

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

    更新:更多的研究 . 看来Petra Gregorova在这里工作 - > http://petragregorova.com/demos/css-gradient-and-bg-image-final.html

  • 1370

    作为一种可靠的方法,您可以在 css 使用中制作一个500x5像素的背景图像:

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

    其中 xxxxxx 对应于与最终渐变颜色匹配的颜色 .

    您也可以将其修复到屏幕底部并使其与初始渐变颜色匹配 .

  • 0

    如果你必须在IE 9(HTML 5和HTML 4.01严格)中一起使用渐变和背景图像,请将以下属性声明添加到css类中,它应该可以解决这个问题:

    filter: progid:DXImageTransform.Microsoft.gradient(GradientType=0, startColorstr='#000000', endColorstr='#ff00ff'), progid:DXImageTransform.Microsoft.AlphaImageLoader(src='[IMAGE_URL]', sizingMethod='crop');
    

    请注意,在使用分号关闭属性值之前,使用 filter 属性并且有两个 progid:[val] 实例用逗号分隔 . 这是fiddle . 另请注意,当您看到小提琴时,渐变会延伸到圆角之外 . 对于没有使用圆角的其他人,我没有解决方法 . 另请注意,在src [IMAGE_URL]属性中使用相对路径时,路径相对于文档页面而不是css文件(请参阅source) .

    这篇文章(http://coding.smashingmagazine.com/2010/04/28/css3-solutions-for-internet-explorer/)是我解决这个问题的原因 . 它对IE特定的CSS3非常有帮助 .

  • 0

    我有一个实现,我需要将这项技术更进一步,并想要概述我的工作 . 下面的代码做了同样的事情,但使用了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负责交叉浏览器代码,现在我必须声明的是每个按钮的精灵位置 . 对于按钮活动和悬停状态,很容易扩展此主体 .

  • 77

    如果您希望图像完全融合在一起,由于单独的HTTP请求,它们看起来不像元素单独加载,那么请使用此技术 . 这里我们在同一个加载的元素上加载两个东西...只需确保先将预渲染的32位透明png图像/纹理转换为base64字符串,然后在background-image css调用中使用它 . 我使用这种技术将晶圆外观纹理与标准rgba透明度/线性渐变css规则融合 . 比分层多个艺术品更好,浪费对移动设备不利的HTTP请求 .

    div.imgDiv   {
          background: linear-gradient(to right bottom, white, rgba(255,255,255,0.95), rgba(255,255,255,0.95), rgba(255,255,255,0.9), rgba(255,255,255,0.9), rgba(255,255,255,0.85), rgba(255,255,255,0.8) );
          background-image: url("data:image/png;base64,INSERTIMAGEBLOBHERE");
     }
    
  • 10

    对于我的响应式设计,我在盒子右侧的下拉框向下箭头(垂直手风琴),接受百分比作为位置 . 最初的向下箭头是"position: absolute; right: 13px;" . 凭借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%);
    > filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#eaeaea',GradientType=0 );

    附:对不起,不知道如何处理过滤器 .

  • 14

    如果您在下载背景图像时遇到奇怪错误,请使用W3C链接检查器:https://validator.w3.org/checklink

    这是我使用的现代mixins(信用:PSA: don't use gradient generators):

    .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);
    }
    
  • 0

    我总是使用以下代码使其工作 . 有一些注意事项:

    • 如果在渐变之前放置图像URL,则此图像将按预期显示为梯度 .
    .background-gradient {
      background: url('http://trungk18.github.io/img/trungk18.png') no-repeat, -moz-linear-gradient(135deg, #6ec575 0, #3b8686 100%);
      background: url('http://trungk18.github.io/img/trungk18.png') no-repeat, -webkit-gradient(135deg, #6ec575 0, #3b8686 100%);
      background: url('http://trungk18.github.io/img/trungk18.png') no-repeat, -webkit-linear-gradient(135deg, #6ec575 0, #3b8686 100%);
      background: url('http://trungk18.github.io/img/trungk18.png') no-repeat, -o-linear-gradient(135deg, #6ec575 0, #3b8686 100%);
      background: url('http://trungk18.github.io/img/trungk18.png') no-repeat, -ms-linear-gradient(135deg, #6ec575 0, #3b8686 100%);
      background: url('http://trungk18.github.io/img/trungk18.png') no-repeat, linear-gradient(135deg, #6ec575 0, #3b8686 100%);
      height: 500px;
      width: 500px;
    }
    
    <div class="background-gradient"></div>
    
    • 如果在图像URL之前放置渐变,则此图像将显示为渐变的 under .
    .background-gradient {
      background: -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;
      background: -webkit-linear-gradient(135deg, #6ec575 0, #3b8686 100%), url('http://trungk18.github.io/img/trungk18.png') no-repeat;
      background: -o-linear-gradient(135deg, #6ec575 0, #3b8686 100%), url('http://trungk18.github.io/img/trungk18.png') no-repeat;
      background: -ms-linear-gradient(135deg, #6ec575 0, #3b8686 100%), url('http://trungk18.github.io/img/trungk18.png') no-repeat;
      background: linear-gradient(135deg, #6ec575 0, #3b8686 100%), url('http://trungk18.github.io/img/trungk18.png') no-repeat;
      width: 500px;
      height: 500px;
    }
    
    <div class="background-gradient"></div>
    

    这种技术与我们有多个背景图像相同,如描述here

  • 2

    你可以简单地输入:

    background: linear-gradient(
        to bottom,
        rgba(0,0,0, 0),
        rgba(0,0,0, 100)
      ),url(../images/image.jpg);
    
  • 40

    如果您还想为图像设置 background position ,则可以使用此方法:

    background-color: #444; // fallback
    background: url('PATH-TO-IMG') center center no-repeat; // fallback
    
    background: url('PATH-TO-IMG') center center no-repeat, -moz-linear-gradient(top, @startColor, @endColor); // FF 3.6+
    background: url('PATH-TO-IMG') center center no-repeat, -webkit-gradient(linear, 0 0, 0 100%, from(@startColor), to(@endColor)); // Safari 4+, Chrome 2+
    background: url('PATH-TO-IMG') center center no-repeat, -webkit-linear-gradient(top, @startColor, @endColor); // Safari 5.1+, Chrome 10+
    background: url('PATH-TO-IMG') center center no-repeat, -o-linear-gradient(top, @startColor, @endColor); // Opera 11.10
    background: url('PATH-TO-IMG') center center no-repeat, linear-gradient(to bottom, @startColor, @endColor); // Standard, IE10
    

    或者你也可以创建一个LESS mixin(bootstrap风格):

    #gradient {
        .vertical-with-image(@startColor: #555, @endColor: #333, @image) {
            background-color: mix(@startColor, @endColor, 60%); // fallback
            background-image: @image; // fallback
    
            background: @image, -moz-linear-gradient(top, @startColor, @endColor); // FF 3.6+
            background: @image, -webkit-gradient(linear, 0 0, 0 100%, from(@startColor), to(@endColor)); // Safari 4+, Chrome 2+
            background: @image, -webkit-linear-gradient(top, @startColor, @endColor); // Safari 5.1+, Chrome 10+
            background: @image, -o-linear-gradient(top, @startColor, @endColor); // Opera 11.10
            background: @image, linear-gradient(to bottom, @startColor, @endColor); // Standard, IE10
        }
    }
    
  • 0

    这是我创建的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寻找最初的解 .

  • 2

    多个背景!

    body {
      background: #eb01a5;
      background-image: url("IMAGE_URL"); /* fallback */
      background-image: url("IMAGE_URL"), linear-gradient(#eb01a5, #d13531); /* W3C */
    }
    

    这两行是任何不做渐变的浏览器的后备 . 请参阅下面的IE <9堆叠图像的注意事项 .

    • 第1行设置平坦的背景颜色 .

    • 第2行设置背景图像后备 .

    最后一行为可以处理它们的浏览器设置背景图像和渐变 .

    • 第3行适用于所有相对现代的浏览器 .

    几乎所有当前浏览器都支持多个背景图像和CSS背景 . 有关浏览器支持,请参阅http://caniuse.com/#feat=css-gradients . 有关您不需要多个浏览器前缀的好帖子,请参阅http://codepen.io/thebabydino/full/pjxVWp/

    Layer Stack

    应该注意,第一个定义的图像将位于堆栈的最顶层 . 在这种情况下,图像位于渐变的TOP上 .

    有关背景分层的详细信息,请参阅http://www.w3.org/TR/css3-background/#layering .

    Stacking images ONLY (no gradients in the declaration) For IE < 9

    IE9及以上版本可以以相同的方式堆叠图像 . 您可以使用它为ie9创建渐变图像,但就个人而言,我不会很好Conditional HTML element以及您的后备代码:

    .lte9 #target{ background-image: url("IMAGE_URL"); }
    

    Background position, sizing etc.

    适用于单个图像的其他属性也可以用逗号分隔 . 如果仅提供1个值,则将应用于包括渐变的所有堆叠图像 . background-size: 40px; 会将图像和渐变限制为40px的高度和宽度 . 但是,使用 background-size: 40px, cover; 将使图像为40px,渐变将覆盖该元素 . 要仅将设置应用于一个图像,请为另一个图像设置默认值: background-position: 50%, 0 0;browsers that support it使用 initialbackground-position: 50%, initial;

    您也可以使用背景速记,但这会删除后备颜色和图像 .

    body{
        background: url("IMAGE_URL") no-repeat left top, linear-gradient(#eb01a5, #d13531);
    }
    

    这同样适用于背景位置,背景重复等 .

相关问题