How to put a colored layer over an image?

Asked

Viewed 14,273 times

4

div {
  background: #000;
  width: 640px;
  height: 640px;
  opacity: 0.2;
  position: absolute;
  z-index: 99;
}
<span>
  <div></div>
  <img src="https://scontent.cdninstagram.com/t51.2885-15/e15/11909170_999395153465997_38853384_n.jpg?ig_cache_key=MTA1NTI1MTUxNDA0OTkxNDkyMg%3D%3D.2" alt="">
</span>

Illustration:

Photo without colorful background:

inserir a descrição da imagem aqui

Photo with colorful background

inserir a descrição da imagem aqui

My gambit worked, but I wanted to learn the right way.

  • 1

    Dude, this is a feature of the image, you need to edit it using GIMP, Photoshop..., to achieve this effect. You also need to "color" the background of the "parent" element, where the image is contained. But apparently you are already doing this. So your problem is with the image itself.

  • 1

    @mauhumor the images above is just to illustrate what I want to do with css.

  • 2

    What exactly do you want to do? That the image takes the "bottom of your site"? or you want to put a semi-transparent layer on top of the image?

  • 1

    I want the image to have a background over it.

  • 2

    So this is the "right way," I think. This is what they do when they show an image, or advertisement, blocking the rest of the site. But still leaving visible. If you want the image to take what is underneath. Then you would have to edit, as I said before.

  • 2

    @mauhumor, it is not necessary to edit the image in photoshop, it can easily give this effect on the image as shown in my reply :)

  • 2

    In my mind background is what’s behind, in your case is a "background in front"? Don’t want to edit the question and make it clearer?

  • 1

    He used the wrong term, wanted a layer in front, was already doing, but was finding the shape not very good.

  • Guys, thank you so much for the answers.

Show 4 more comments

5 answers

7

In addition to the solutions they presented, you can use the multiples backgrounds, just use linear-gradient passing two color values rgba next to the image url:

div {
  height: 262px;
  width: 389px;
  background:
    linear-gradient(
      rgba(0,0,0,.3),rgba(0,0,0,.3)
    ),
    url(https://i.stack.imgur.com/0VvBr.jpg) no-repeat;
}
<div></div>

Another example:

div {
  height: 262px;
  width: 389px
}

div.red {
  background:
    linear-gradient(
      rgba(255,0,0,.3),rgba(255,0,0,.3)
    ),
    url(https://i.stack.imgur.com/0VvBr.jpg) no-repeat
} 

div.green {
  background:
    linear-gradient(
      rgba(0,255,0,.3),rgba(0,255,0,.3)
    ),
    url(https://i.stack.imgur.com/0VvBr.jpg) no-repeat
}

div.blue {
  background:
    linear-gradient(
      rgba(0,0,255,.4),rgba(0,0,255,.4)
    ),
    url(https://i.stack.imgur.com/0VvBr.jpg) no-repeat
}
<div class='red'></div>
<div class='green'></div>
<div class='blue'></div>

6

If your image has transparent parts just set the background normally:

#a{background:orange}
#b{background:green}
#c{background:blue}
<img id="a" src="http://www.gravatar.com/avatar/162a62cf2058d7dd8d549dd6bd3b46f9">
<img id="b" src="http://www.gravatar.com/avatar/162a62cf2058d7dd8d549dd6bd3b46f9">
<img id="c" src="http://www.gravatar.com/avatar/162a62cf2058d7dd8d549dd6bd3b46f9">

4

Your "gambiarra" worked for your test, because the image and the div are in the same size, try changing image. It will get a gray square around the image. That’s because you set the property in the div that the image is and not in the image itself. See:

div {
  background: #000;
  width: 640px;
  height: 640px;
  opacity: 0.2;
  position: absolute;
  z-index: 99;
}
<span>
  <div></div>
  <img src="https://i.stack.imgur.com/0VvBr.jpg" alt="">
</span>

This works, if the image is the same size. Now see if you put the property filter: brightness(50%); image, regardless of the size of the div it will work with the effect you want. See:

img {
    filter: brightness(50%);
}
<span>
  <div></div>
  <img src="https://i.stack.imgur.com/0VvBr.jpg" alt="">
</span>

The estate brightness adjusts the image brightness. You can see the compatibility of filters in this website.

4


Just use the properties :before or :after, in case, it would be better to :after for example, your code would look like this:

CSS

span{
        position:relative;
    }
    div{
        width: 640px;
        height: 640px;
        opacity: 1;
        position: absolute;
    }
    div:after{
        content:'';
        position:absolute;
        top:0;
        bottom:0;
        left:0;
        right:0;
        background-color:rgba(0,0,0,.6);
    }
    div img{
        width:100%;
    }

HTML

<span>
  <div>
     <img src="https://scontent.cdninstagram.com/t51.2885-15/e15/11909170_999395153465997_38853384_n.jpg?ig_cache_key=MTA1NTI1MTUxNDA0OTkxNDkyMg%3D%3D.2" alt="">
  </div>
</span>

Now, to explain better, the properties :before and :after serve so that we can control a single element as if they were layers of it, before and after, :after was set as absolute so that we could use the position according to top,left,right e bottom, the span has to be set as relative, because as you know, position absolute responds to the first parent who goes relativo. Then to set the property :before/:after we need to say, what is the content, hence the content:''; to show that the content is empty and the property is presented to the end user in the correct way, so I give a background-color in the after, and already put the opacidade along with the rgba(0~255,0~255,0~255,0~1).

It’s like I photoshopped a layer above its element, and gave it properties common to an element

3

You can set the property of background-color, but it will make a difference if the image does not overlap the entire background area:

<!DOCTYPE html>
<html>
    <head>
        <style> 
            body {  
                background-image: url("img_tree.gif"), url("img_flwr.gif");
                background-color: #dddddd;
                opacity: 0.5;
                filter: alpha(opacity=50);
            }
        </style>
    </head>
    <body>

    </body>
</html>

A better solution would be to use the Opacity/Transparence property:

<!DOCTYPE html>
<html>
    <head>
        <style>
            img {
                opacity: 0.5;
                filter: alpha(opacity=50); /* For IE8 and earlier */
            }
        </style>
    </head>
    <body>

        <h1>Image Transparency</h1>
        <p>The opacity property specifies the transparency of an element. The lower  the value, the more transparent:</p>

        <p>Image with 50% opacity:</p>
        <img src="img_forest.jpg" alt="Forest" width="170" height="100">

    </body>
</html>

Consult more:

  • was unaware of such property (filter), very good :)

  • Don’t forget to mention filter compatibility: can I use css Filters?

  • I didn’t know this property. Thank you so much for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.