How to overlay image with color in CSS?
Solution 1:
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;
}
Solution 2:
You can do that in one line of CSS.
background: linear-gradient(to top, #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;
width: 100%;
color: white;
font: bold 6.5em/2em monospace;
display: flex;
justify-content: center;
align-items: center;
background: linear-gradient(to top, #3204fdba, #9907facc), url(https://picsum.photos/1280/853/?random=1) no-repeat top center;
}
<header>Hello World</header>
See here CodePen
Solution 3:
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.
Solution 4:
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%);
}
Solution 5:
#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.