I added an event listener to the "learn more" div and it doesn’t work. When I inspected that div element and went to the "Event Listeners" tab, it shows that it has the event listener attached to it. I thought the "vert-gradient" div (see code) prevents it from being triggered, but when I deleted it, the problem remained.
let lms = document.querySelectorAll(".lm");
lms.forEach((lm) => {
console.log(lm); //this works
lm.addEventListener("click", (event) => {
event.preventDefault();
console.log("click!"); //this doesn't work!
});
});
.border {
width: 354px;
height: 404px;
background: linear-gradient(rgba(0, 0, 0, 0) 0%, rgba(212, 119, 66, 1) 81%);
border-radius: 27px;
position: relative;
z-index: -3;
pointer-events: none;
}
.prod {
width: 350px;
height: 400px;
border-radius: 25px;
margin: 2px;
background-repeat: no-repeat;
position: relative;
z-index: -2;
pointer-events: none;
background: #0000ff;
}
.prod h2 {
font-weight: 600;
font-size: 40px;
line-height: 48px;
bottom: 78px;
}
.prod h3 {
color: #d47742;
font-weight: 600;
font-size: 30px;
line-height: 36px;
bottom: 32px;
}
.prod h2,
.prod h3 {
position: absolute;
left: 30px;
}
.vert-gradient {
background: linear-gradient(
rgba(0, 0, 0, 0) 0%,
rgba(19, 19, 19, 0.7) 55%,
rgb(19, 19, 19) 84%
);
position: absolute;
width: 350px;
height: 400px;
top: 0;
z-index: -1;
border-radius: 25px;
pointer-events: none;
}
.lm {
height: 30px;
display: flex;
align-items: center;
position: absolute;
right: 10px;
bottom: 5px;
}
.lm span {
line-height: 24px;
font-size: 20px;
}
.lm img {
width: 30px;
}
<div class="border" cat="b">
<div class="prod">
<h2>Bread</h2>
<h3>1,50€</h3>
<div class="lm">
<span>Learn More</span>
</div>
<div class="vert-gradient"></div>
</div>
</div>
I tried to remove the "border" div but the problem remained. I tried to attach the event listener to the "prod" div, but it isn’t triggered either. event.preventDefault();
doesn’t do anything.
3
Answers
Not sure what you are trying to achieve with this HTML structure and CSS . It’s a bit odd.
But first-of-all you are using
pointer-events:none
on both of your item’s ancestors. Which disables the click event on thelm
child.Second, you use
z-index
on your ancestors which is lower than 0 and lower than thevert-gradient
z-index. That means, thevert-gradient
will go on top.Without any other context on what you are trying to achieve. Here’s a working example that shows exactly why what I said above is making your element not clickable.
Again, you should rethink your CSS and HTML structure because the ones you have are not correct at all.
It is not working properly because of your CSS. You have set
pointer-events
both elementsdiv.border
anddiv.prod
asnone
which isdeactivating the click-target
that is pointer events will not work on both div and setting thez-index
of both the div is keeping them behind the body element in stack order of element due to which click is not happening on.lm
div but on body.To fix this,
remove pointer-events from css and set z-index of .border as 1 and .prod as 2
.See demo below:-
It was a CSS problem, preventing the click from hitting the intended target. I started paring it away til I got to this working example: