How to overlay image with color in CSS?

前端 未结 11 771
轻奢々
轻奢々 2020-12-14 13:51

Objective

I want a color overlay on this header element. How can I do this with CSS?

Code

HTML

相关标签:
11条回答
  • 2020-12-14 14:40

    If you don't mind using absolute positioning, you can position your background image, and then add an overlay using opacity.

    div {
        width:50px;
        height:50px;
        background:   url('http://images1.wikia.nocookie.net/__cb20120626155442/adventuretimewithfinnandjake/images/6/67/Link.gif');
        position:absolute;
        left:0;
        top:0;
    }
    
    .overlay {
       background:red;
       opacity:.5;
    }
    

    See here: http://jsfiddle.net/4yh9L/

    0 讨论(0)
  • 2020-12-14 14:42

    If you want to just add a class to add the overlay:

    span {
      padding: 5px;
    }
    
    .green {
      background-color: green;
      color: #FFF;
    }
    
    .overlayed {
      position: relative;
    }
    
    .overlayed::before {
      content: ' ';
      z-index: 1;
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
      background-color: #00000080;
    }
    
    .stand-out {
      position: relative;
      z-index: 2;
    }
    <span class="green overlayed">with overlay</span>
    <span class="green">without overlay</span>
    <br>
    <br>
    <span class="green overlayed">
      <span class="stand-out">I stand out</span>
    </span>

    Important: the element you put the overlayed class on needs to have a position set. If it doesn't, the ::before element will take the size of some other parent element. In my example I've set the position to "relative" via the .overlayed rule, but in your use case you might need "absolute" or some other value.

    Also, make sure that the z-index of the overlayed class is higher than the ones of the eventual child elements of the container, unless you actually want for those to "stand out" and not be overlayed (as with the span with the stand-out class, in my snippet).

    0 讨论(0)
  • 2020-12-14 14:44

    You could use the hue-rotate function in the filter property. It's quite an obscure measurement though, you'd need to know how many degrees round the colour wheel you need to move in order to arrive at your desired hue, for example:

    header {
        filter: hue-rotate(90deg);
    }
    

    Once you'd found the correct hue, you could combine the brightness and either grayscale or saturate functions to find the correct shade, for example:

    header {
        filter: hue-rotate(90deg) brightness(10%) grayscale(10%);
    }
    

    The filter property has a vendor prefix in Webkit, so the final code would be:

    header {
      -webkit-filter: hue-rotate(90deg) brightness(10%) grayscale(10%);
              filter: hue-rotate(90deg) brightness(10%) grayscale(10%);
    }
    
    0 讨论(0)
  • 2020-12-14 14:44

    Use mutple backgorund on the element, and use a linear-gradient as your color overlay by declaring both start and end color-stops as the same value.

    Note that layers in a multi-background declaration are read much like they are rendered, top-to-bottom, so put your overlay first, then your bg image:

    #header {
      background: 
        linear-gradient(to bottom, rgba(100, 100, 0, 0.5), rgba(100, 100, 0, 0.5)) cover,
        url(../img/bg.jpg) 0 0 no-repeat fixed;
      height: 100%;
      overflow: hidden;
      color: #FFFFFF
    }
    
    0 讨论(0)
  • 2020-12-14 14:45

    You may use negative superthick semi-transparent border...

    .red {
        outline: 100px solid rgba(255, 0, 0, 0.5) !important;
        outline-offset: -100px;
        overflow: hidden;
        position: relative;
        height: 200px;
        width: 200px;
    }
    <div class="red">Anything can be red.</div>
    <h1>Or even image...</h1>
    <img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.png?v=9c558ec15d8a" class="red"/>

    This solution requires you to know exact sizes of covered object.

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