[英]How do I add a color overlay to an image using CSS?
im using react, html, css and material ui我正在使用反应,html,css 和材料 ui
I am trying to toggle between color overlay being present and not present with onclick on an image.我试图在图像上的 onclick 存在和不存在颜色叠加之间切换。
below is the javascript:下面是 javascript:
const About = ({imgState, setImgState}) => {
const imgTracker = () => {
if(imgState === true){
setImgState(false);
}
else if(imgState===false){
setImgState(true);
}
}
...
<Box
className={imgState ? 'AboutImg active' : 'AboutImg notactive'}
component="img"
alt="face"
src={img2}
onClick={imgTracker}
/>
and below is the css:以下是 css:
.MidSectionTop{
.MidSectionTopText{
}
.MidSectionImage{
.AboutImg{
height: calc(100vh * 4/12);
width: calc(100vw * 2/12);
&.active{
background: rgba(0, 197, 165, 0.5);
}
&.notactive{
background: none;
}
}
}
}
my problem so far is that the image is overlayed ontop of the background color.到目前为止,我的问题是图像覆盖在背景颜色之上。 I have tried giving z-index to the '&.active' class but it did not work out.我尝试将 z-index 赋予 '&.active' class 但没有成功。 any help grealy appreciated!非常感谢任何帮助! Let me know what else I could provide让我知道我还能提供什么
You can use what's called pseudo-elements ( ::before
or ::after
) for this.您可以为此使用所谓的伪元素( ::before
或::after
)。
.AboutImg{
position: relative;
&::after{
position:absolute;
left:0;
top:0;
right:0;
bottom:0;
display:block;
content: '';
}
&.active::after{
background: rgba(0, 197, 165, 0.5);
}
&.notactive::after{
background: none;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.