【问题标题】:How can I tint a background image with CSS?如何使用 CSS 为背景图像着色?
【发布时间】:2012-08-20 05:10:12
【问题描述】:

我有一个通过 CSS 设置的背景图片。

html {
background-image: url('../img/cello.jpg');
background-attachment: fixed;
background-size: 100%;
}

我计划为网站的不同页面设置不同的背景图片:因此文字清晰易读很重要。现在,为了确保易读性,我在中间的#main 内容框有一个半透明的黑色背景:

#main {
background: rgba(0, 0, 0, 0.5);
}

不过,我真正想做的是在整个背景图像上加上那种半透明的背景,因为黑盒子看起来有点笨重。我尝试制作一个包含整个 HTML 文档的 <div id=#tint>,并将 rgba(0, 0, 0, 0.5) 提供给#tint,但这根本不起作用——我要么什么都没有改变,要么我可以让整个背景变成一个简单的灰色,根本看不到背景图像。这根本不可能吗?

【问题讨论】:

    标签: css background tint


    【解决方案1】:

    使用background-blend-mode 进行简单的色调

    您可以使用background-blend-mode css 属性:

    .background-tint {
      background-color: rgba(200,100,0,.5); /* Tint color */
      background-blend-mode: multiply;
    }
    

    将它放在任何带有背景图像的元素上,你就可以开始了。

    现代浏览器不支持(包括 IE 11)很好地支持该属性。对于不支持的浏览器,您可以使用polyfill

    Working demo


    其他选项

    filter 用于复杂的色调

    您可以使用filter css 属性:

    .background-tint {
      filter: sepia(100%) saturate(200%) brightness(70%) hue-rotate(330deg);
    }
    

    把它放在任何带有背景图片的元素上,你就可以开始了。 为了改变颜色改变hue-rotate的值。

    现代浏览器很好地支持该属性,包括 IE 11。

    Working demo

    使用平面线性渐变和多重背景叠加

    .background-tint {
      background-image: 
        linear-gradient( rgba(0,0,0,.5), rgba(0,0,0,.5) ),
        url('http://placehold.it/420')
    }
    

    我认为这是使用最广泛的技术,但它有硬编码的缺点,即你不能只学习一个类,将其粘贴在一个元素上并进行着色。

    你可以把它变成一个 less 或 sass 的 mixin,比如:

    less

    .background-tint(@tint-color, @image-url) {
      background-image: 
        linear-gradient( @tint-color, @tint-color ),
        url( @image-url )
    }
    

    sass

    @mixin background-tint($tint_color, $image_url) {
      background-image: 
        linear-gradient( $tint_color, $tint_color ),
        url( $image_url )
    }
    

    Working demo

    使用透明背景

    .background-tint { position: relative; }
    
    .background-tint::after {
      content: "";
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(0,0,0,.5);
    }
    

    这种方法的优点是可以在大多数浏览器上运行,并且只是一个不错的类,您可以添加到任何元素中。 不利的一面是,如果您在该元素中还有其他任何内容,则必须将其包装在具有某种定位的 div 中 position: relative 效果最好。

    例子:

    <div class="background-tint">
      <div class="u-relative">Some text here</div>
    </div>
    
    .u-relative { position: relative; }
    

    Working Demo

    【讨论】:

    • 只有一个没有在div中缝合字体颜色的工作
    • @pjammer “下水道”是什么意思?我测试了所有示例,字体的颜色就是您设置的 color 参数。
    • 注意:如果您正在寻找最多的浏览器支持,请使用线性渐变。 caniuse.com/#feat=css-gradients
    • 非常好的解决方案。不幸的是,这总是创建一个完整的填充。体育如果我有一个部分透明的背景图像,覆盖颜色也将填充背景图像的透明区域。所以乘法并不完全像一个色调。
    • @Thomasn 感谢您指出这一点。我使用filter 属性添加了另一种方法,该方法应该只允许覆盖非透明区域。缺点是色调的颜色不容易选择。
    【解决方案2】:

    我认为您需要创建一个覆盖元素(可能是div),它具有所需的半透明背景。比如:

    .overlay {
        z-index: 1;
        height: 100%;
        width: 100%;
        position: fixed;
        overflow: auto;
        top: 0px;
        left: 0px;
        background: rgba(0, 0, 0, 0.7); /*can be anything, of course*/
    }
    

    当然还有一个小演示:little link

    【讨论】:

    • 好的,这很有帮助,谢谢--但是,虽然我的背景图像现在已成功变暗,但现在我在中心的内容框/列无法看到...嗯,这不是一个框/列,因为它只是给&lt;body&gt; 10% 30% 的边距。现在这些内容已经消失了。我对网页设计的经验还不够丰富,不知道为什么。
    • @user1623053 您需要将所有内容嵌套在覆盖层div 中——将其视为body编辑: 哦,好吧,给body 边距是个坏主意。使用另一个元素并给它相同的边距。
    • 好的,我所有的内容都嵌套在叠加层 div 中,并且我已经将边距移动到一个专门的 div 中。这两个 div 都嵌套在 body 中,但在其他任何东西之外。现在我的叠加色调效果很好,但我的内容没有边距......
    • @user1623053 能否分享一个演示或指向您网站的链接(如果它已上线)?
    • 好吧,我刚刚把整个页面打进了 Jsfiddle:jsfiddle.net/TqNqT/15
    【解决方案3】:

    这对我很有用:

    https://css-tricks.com/tinted-images-multiple-backgrounds/

    .tinted-image {
      background: 
        /* top, transparent red, faked with gradient */ 
        linear-gradient(
          rgba(255, 0, 0, 0.45), 
          rgba(255, 0, 0, 0.45)
        ),
        /* bottom, image */
        url(image.jpg);
    }
    

    在另一个答案的基础上,您可以使用现有的颜色来做到这一点,而不是像:

    linear-gradient(
      fade(@brand-primary, 50%),
      fade(@brand-primary, 50%)
    ),
    

    【讨论】:

    • 这在包括IE11 + edge在内的所有主流浏览器上都支持,在IE或Edge上不支持使用background-blend-mode的第二个响应"caniuse.com/#feat=css-gradients
    【解决方案4】:

    它将是 overlay 属性 https://dvcs.w3.org/hg/FXTF/rawfile/tip/compositing/index.html#blendingoverlay 但这是一个草稿。不要依赖它

    【讨论】:

      【解决方案5】:

      试试不透明度:

      opacity:0.4;
      filter:alpha(opacity=40); /* For IE8 and earlier */
      

      【讨论】:

      • 没有变化。我真的不明白——rgba 在主要内容框上工作得很好。
      猜你喜欢
      • 1970-01-01
      • 2012-01-01
      • 2020-12-07
      • 2017-10-11
      • 2017-12-13
      • 1970-01-01
      • 1970-01-01
      • 2012-01-17
      • 2021-04-08
      相关资源
      最近更新 更多