As far as I'm aware of, you'll need two things to achieve what you're looking for:
- A parent element for
<img />
so we can add an overlay using :after
. For this example I will use the figure
element.
- A little bit of Javascript to locate all the empty alts & add a class to its parent with the overlay you want.
Here is an example:
HTML
<figure class="custom-figure">
<img src="https://via.placeholder.com/200x200" alt="">
</figure>
<figure class="custom-figure">
<img src="https://via.placeholder.com/200x200" alt="123">
</figure>
CSS
.custom-figure{
display: inline-block;
margin: 10px;
}
.custom-figure--overlay{
position: relative;
}
.custom-figure--overlay:after{
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-image: linear-gradient(120deg, #eaee44, #33d0ff);
opacity: .7;
}
JS
const addOverlayToEmptyAlt = () => {
const images = document.querySelectorAll(".custom-figure > img");
for (let each of images) {
const alt = each.getAttribute("alt").trim();
if (alt === "") {
each.closest(".custom-figure").classList.add("custom-figure--overlay");
}
}
};
addOverlayToEmptyAlt();
Live example on Codepen