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

前端 未结 10 2320
一生所求
一生所求 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 07:48

    And to add to this answer, make sure the image itself has a transparent background.

    0 讨论(0)
  • 2020-12-04 07:49

    Gecko has a weird bug where setting the background-color for the html selector will cover up the background-image of the body element even though the body element in effect has a greater z-index and you should be able to see the body's background-image along with the html background-color based purely on simple logic.

    Gecko Bug

    Avoid the following...

    html {background-color: #fff;}
    body {background-image: url(example.png);}
    

    Work Around

    body {background-color: #fff; background-image: url(example.png);}
    
    0 讨论(0)
  • 2020-12-04 07:50

    Make half of the image transparent so the background colour is seen through it.

    Else simply add another div taking up 50% up the container div and float it either left or right. Then apply either the image or the colour to it.

    0 讨论(0)
  • 2020-12-04 07:56

    Hello everyone I tried another way to combine background-image and background-color together:

    HTML

    <article><canvas id="color"></canvas></article>
    

    CSS

    article {
      height: 490px;
      background: url("Your IMAGE") no-repeat center cover;
      opacity:1;
    } 
    
    canvas{
      width: 100%; 
      height: 490px; 
      opacity: 0.9;
    }
    

    JAVASCRIPT

    window.onload = init();
    
    var canvas, ctx;
    
    function init(){
      canvas = document.getElementeById('color'); 
      ctx = canvas.getContext('2d'); 
      ctx.save();  
      ctx.fillstyle = '#00833d'; 
      ctx.fillRect(0,0,490,490);ctx.restore();
    }
    

    Please let me know if it worked for you Thanks

    0 讨论(0)
  • 2020-12-04 07:57

    It's perfectly possible to use both a color and an image as background for an element.

    You set the background-color and background-image styles. If the image is smaller than the element, you need to use the background-position style to place it to the right, and to keep it from repeating and covering the entire background you use the background-repeat style:

    background-color: green;
    background-image: url(images/shadow.gif);
    background-position: right;
    background-repeat: no-repeat;
    

    Or using the composite style background:

    background: green url(images/shadow.gif) right no-repeat;
    

    If you use the composite style background to set both separately, only the last one will be used, that's one possible reason why your color is not visible:

    background: green; /* will be ignored */
    background: url(images/shadow.gif) right no-repeat;
    

    There is no way to specifically limit the background image to cover only part of the element, so you have to make sure that the image is smaller than the element, or that it has any transparent areas, for the background color to be visible.

    0 讨论(0)
  • 2020-12-04 07:58

    use

    background:red url(../images/samle.jpg) no-repeat left top;
    
    0 讨论(0)
提交回复
热议问题