How to overlay image with color in CSS?

前端 未结 11 770
轻奢々
轻奢々 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:26

    You can do that in one line of CSS.

      background: linear-gradient(to right, #3204fdba, #9907facc), url(https://picsum.photos/1280/853/?random=1) no-repeat top center;
    

    Also hover on the color in VS Code, and click on the color to be a hex color, and you can change the colors opacity easy, instead of the rgba (rgba(48, 3, 252, 0.902), rgba(153, 7, 250, 0.902)), It can be short to (#3204fde6, #9907fae6)

    header{
       height: 100vh;
       color: white;
       font: bold 2em/2em monospace;
       display: flex;
       justify-content: center;
       align-items: center;
      
      background: linear-gradient(to right,#3204fdba, #9907facc), url(https://picsum.photos/1280/853/?random=1) no-repeat top center;
    }
    <header>is simply dummy text of the printing and<br> typesetting industry.</header>

    See here CodePen

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

    In helpshift, they used the class home-page as

    HTML

    <div class="page home-page">...</div>
    

    CSS

    .home-page {
        background: transparent url("../images/backgrounds/image-overlay.png") repeat 0 0;
        background: rgba(39,62,84,0.82);
        overflow: hidden;
        height: 100%;
        z-index: 2;
    }
    

    you can try similar like this

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

    You can also add an additional class with such settings. Overlay will not overlap content and no additional tag is needed

    .overlay {
      position: relative;
      z-index: 0;
    }
    
    .overlay::after {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: red;
        opacity: .6;
        /* !!! */
        z-index: -1;
    }
    

    https://codepen.io/zeroox003/pen/yLYbpOB

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

    Here's a creative idea using box-shadow:

    #header {
        background-image: url("apple.jpg");
        box-shadow: inset 0 0 99999px rgba(0, 120, 255, 0.5);
    }
    

    What's happening

    1. The background sets the background for your element.

    2. The box-shadow is the important bit. It basically sets a really big shadow on the inside of the element, on top of the background, that is semi-transparent

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

    You should use rgba for overlaying your element with photos.rgba is a way to declare a color in CSS that includes alpha transparency support. you can use .row as an overlayer like this:

    #header {
        background: url(../img/bg.jpg) 0 0 no-repeat fixed;
        height: 100%;
        overflow: hidden;
        color: #FFFFFF
     }
    
    .row{
        background: rgba(39,62,84,0.82);
        overflow: hidden;
        height: 100%;
        z-index: 2;
    }
    
    0 讨论(0)
  • 2020-12-14 14:35
    #header.overlay {
        background-color: SlateGray;
        position:relative;
        width: 100%;
        height: 100%;
        opacity: 0.20;
        -moz-opacity: 20%;
        -webkit-opacity: 20%;
        z-index: 2;
    }
    

    Something like this. Just add the overlay class to the header, obviously.

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