简体   繁体   English

CSS颜色叠加

[英]CSS Color Overlay

Unfortunately I haven't found an answer to this generic question that fits my needs, so I present this to you. 不幸的是,我没有找到适合我需求的通用问题的答案,因此我向您介绍。

The problem: 问题:

I have an image that needs to be "overlayed" with a color. 我的图像需要用颜色“覆盖”。 Below I have the code. 下面有代码。

HTML: HTML:

<a href=""><img class="portfolio-item" src="https://m1.behance.net/profiles12/3455485/projects/14323837/1cdda4b7d6bad96ceee53e6bad98b8e4.png"></a>

CSS: CSS:

.portfolio-item {
    top: 100px;
    height: 200px;
    width: 200px;
    position: absolute;
    left: 0px;
    border-radius:25px;
}

.hover {
    background-color: rgba(48, 48, 48, 0.9);
    overflow: hidden;
    z-index: 1;
}

Jquery: jQuery:

$(document).ready(function(){
$(".portfolio-item").hover(function(){
                        $(".portfolio-item").addClass("hover");
                    }, function(){
                        $(".portfolio-item").removeClass("hover");
                    });
});

Let me know if you can help. 让我知道您能否提供帮助。

You dont need to use jQuery 您不需要使用jQuery

.portfolio-item {
    top: 100px;
    height: 200px;
    width: 200px;
    position: absolute;
    left: 0px;
    border-radius:25px;
}

.portfolio-item:hover {
    background-color:cyan;

    z-index: 1;
}

I used it ,and background color is also changing in browser ,I know your png image is not transparant ,use other Transparant image,and it will work. 我使用了它,并且浏览器中的背景色也发生了变化,我知道您的png图像不是透明的,请使用其他透明图像,它将起作用。

Check this fiddle http://jsfiddle.net/sajith/sha5E/ 检查这个小提琴http://jsfiddle.net/sajith/sha5E/

HTML 的HTML

<a class="bg" href=""><img class="portfolio-item" src="https://m1.behance.net/profiles12/3455485/projects/14323837/1cdda4b7d6bad96ceee53e6bad98b8e4.png"></a>

Javascript Java脚本

$(document).ready(function(){
    $(".portfolio-item").hover(function(){
        $(".portfolio-item").addClass("hover");
    }, function(){
        $(".portfolio-item").removeClass("hover");
    });
});

CSS 的CSS

.portfolio-item, .bg {
    height: 200px;
    width: 200px;
    left: 0px;
    border-radius:25px;
    position:absolute;
}

.hover {
    overflow: hidden;
    z-index: 1;
    opacity:0.1;
}
.bg {
    background-color: rgba(48, 48, 48, 0.9);
    top: 100px;
    display:inline-block;
}

jQuery isn't needed. 不需要jQuery。 You can use the ::after pseudoelement. 您可以使用::after伪元素。

HTML: HTML:

<div class="portfolio-item">
    <img  src="https://m1.behance.net/profiles12/3455485/projects/14323837/1cdda4b7d6bad96ceee53e6bad98b8e4.png" />
</div>

CSS: CSS:

.portfolio-item { //container
    position: absolute;
    top: 100px;
    left: 0px;
}
.portfolio-item img { //image styling
    height: 200px;
    width: 200px;
    border-radius:25px;
}
.portfolio-item:after { //contains the 'overlay'
    position:absolute;
    display: block;
    content: "";
    top: 0px;
    left: 0px;
    height: 200px;
    width: 200px;
    background: rgba(48, 48, 48, 0.9);
    z-index: 101;
    opacity: 0;
    border-radius:25px;
}
.portfolio-item:hover:after {
    opacity: 1;
}

Fiddle: http://jsfiddle.net/Shiazure/wJheZ/ 小提琴: http : //jsfiddle.net/Shiazure/wJheZ/

.container {
  width : 200px;
  height: 200px;
  position: relative;
}

.overlay {
  background: red;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: .4;  
}

try wrapping it like this and adding a div with a class overlay inside the container 尝试像这样包装它,并在容器内添加带有类覆盖的div

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM