Why can't I use background image and color together?

前端 未结 10 2322
一生所求
一生所求 2020-12-04 07:33

What I am trying to do is to show both background-color and background-image, so that half of my div will cover the right shadow backg

相关标签:
10条回答
  • 2020-12-04 08:02

    To tint an image, you can use CSS3 background to stack images and a linear-gradient. In the example below, I use a linear-gradient with no actual gradient. The browser treats gradients as images (I think it actually generates a bitmap and overlays it) and thus, is actually stacking multiple images.

    background: linear-gradient(0deg, rgba(2,173,231,0.5), rgba(2,173,231,0.5)), url(images/mba-grid-5px-bg.png) repeat;
    

    Will yield a graph-paper with light blue tint, if you had the png. Note that the stacking order might work in reverse to your mental model, with the first item being on top.

    Excellent documentation by Mozilla, here:

    https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_multiple_backgrounds

    Tool for building the gradients:

    http://www.colorzilla.com/gradient-editor/

    Note - doesn't work in IE11! I'll post an update when I find out why, since its supposed to.

    0 讨论(0)
  • 2020-12-04 08:06
    background:url(directoryName/imageName.extention) bottom left no-repeat; 
    background-color: red;
    
    0 讨论(0)
  • 2020-12-04 08:11

    Actually there is a way you can use a background color with a background image. In this case, the background part will be filled with that specified color instead of a white/transparent one.

    In order to achieve that, you need to set the background property like this:

    .bg-image-with-color {
        background: url("example.png") no-repeat, #ff0000;
    }
    

    Note the comma and the color code after no-repeat; this sets the background color you wish.

    I discovered this in this YouTube video, however I'm not affiliated with that channel or video in any means.

    0 讨论(0)
  • 2020-12-04 08:13

    Here's an example of using background-image and background-color together:

    .box {
      background-image: repeating-linear-gradient( -45deg, rgba(255, 255, 255, .2), rgba(255, 255, 255, .2) 15px, transparent 15px, transparent 30px);
      width: 100px;
      height: 100px;
      margin: 10px 0 0 10px;
      display: inline-block;
    }
    <div class="box" style="background-color:orange"></div>
    <div class="box" style="background-color:green"></div>
    <div class="box" style="background-color:blue"></div>

    0 讨论(0)
提交回复
热议问题