Ceejay
Ceejay

Reputation: 7267

How to add the overlay for the background image

I want to add an overlay over the div which has the background image. the divs will be generated from the php. Each div will have an background image.

When i try to add the gradient overlay on top of the div having background image, i couldn't see any changes happening.

How can i do this?

here is what i have tried

Html

<div class="image-box" style="background-image:url(images/stacks.jpg);"></div>
<div class="image-box" style="background-image:url(images/random.jpg);"></div>
<div class="image-box" style="background-image:url(images/laos.jpg);"></div>
<div class="image-box" style="background-image:url(images/thai.jpg);"></div>
<div class="image-box" style="background-image:url(images/phils.jpg);"></div>
<div class="image-box" style="background-image:url(images/tabs.jpg);"></div>

Css

.image-box:hover{
    background-image:linear-gradient(to bottom, rgba(9, 37, 70, 0.1),rgba(9, 37, 70, 0.4),rgba(9, 37, 70, 0.8));
}

Upvotes: 2

Views: 57

Answers (1)

Joykal Infotech
Joykal Infotech

Reputation: 1876

May this will help you. Try it:

Initially, I have added a pseudo-element ::after which acts as an overlay and assigned the size same as image-box and initially its display: none; but on hover changed to display: block; which applies the overlay on hover only.

.image-box {
  position: relative;
  display: inline-block;
  height: 200px;
  width: 200px;
}
.image-box::after {
    position: absolute;
    top: 0;
    left: 0;
    display: none;
    height: 100%;
    width: 100%;
    background:linear-gradient(to bottom, rgba(9, 37, 70, 0.1),rgba(9, 37, 70, 0.4),rgba(9, 37, 70, 0.8));
    content: '';
}
.image-box {
  position: relative;
  display: inline-block;
  height: 200px;
  width: 200px;
}
.image-box:hover::after {
    display: block;
}
<div class="image-box" style="background-image:url(https://cdn.pixabay.com/photo/2018/06/30/09/31/background-image-3507320__340.jpg);"></div>
<div class="image-box" style="background-image:url(https://cdn.pixabay.com/photo/2018/06/30/09/31/background-image-3507320__340.jpg);"></div>
<div class="image-box" style="background-image:url(https://cdn.pixabay.com/photo/2018/06/30/09/31/background-image-3507320__340.jpg);"></div>
<div class="image-box" style="background-image:url(https://cdn.pixabay.com/photo/2018/06/30/09/31/background-image-3507320__340.jpg);"></div>
<div class="image-box" style="background-image:url(https://cdn.pixabay.com/photo/2018/06/30/09/31/background-image-3507320__340.jpg);"></div>
<div class="image-box" style="background-image:url(https://cdn.pixabay.com/photo/2018/06/30/09/31/background-image-3507320__340.jpg);"></div>

Upvotes: 2

Related Questions