纯CSS悬停导致与文本的颜色叠加

时间:2022-12-04 20:23:01

This is my first post on Stack Overflow, but I've been finding answers on here for years. This is one I can't seem to find though.

这是我在Stack Overflow上的第一篇文章,但我已经在这里找到了多年的答案。这是我似乎无法找到的。

I'm wondering if there is a way to do the following using only HTML and CSS.

我想知道是否有办法只使用HTML和CSS执行以下操作。

I have a fluid layout, and I want to keep the images fluid. I currently have each image set up to fill their respective divs by using the max-height:100% and max-width:100% properties.

我有一个流畅的布局,我想保持图像流畅。我目前通过使用max-height:100%和max-width:100%属性来设置每个图像来填充它们各自的div。

On hover, I would like the image to have a slightly transparent color overlay, and reveal some text and a button.

在悬停时,我希望图像具有略微透明的颜色叠加,并显示一些文本和按钮。

I'm able to do this if I define the image/div width, but I cannot figure out how to do it while keeping the image so it re-sizes dynamically.

如果我定义了图像/ div宽度,我就能做到这一点,但我无法弄清楚如何在保持图像的同时动态调整大小。

Here's a Fiddle of 4 images in a fluid layout. Any help would be GREATLY appreciated, I feel like I'm overlooking something.

这是流畅布局中4个图像的小提琴。任何帮助都会非常感激,我觉得我忽略了一些东西。

And the code:

和代码:

HTML

<body>
    <div id="container">
        <div class="span4 red">
            <img src="http://img.thesun.co.uk/aidemitlum/archive/01500/SNE0125Q---_149991_1500286a.jpg">
        </div>
        <div class="span4 blue">
            <img src="http://img.thesun.co.uk/aidemitlum/archive/01500/SNE0125Q---_149991_1500286a.jpg">
        </div>
        <div class="span4 green">
            <img src="http://img.thesun.co.uk/aidemitlum/archive/01500/SNE0125Q---_149991_1500286a.jpg">
        </div>
        <div class="span4 purple">
            <img src="http://img.thesun.co.uk/aidemitlum/archive/01500/SNE0125Q---_149991_1500286a.jpg">
        </div>
    </div>
</body>

CSS

body { margin:0;}
#container { width:100%; font-size:0;}
img { max-width:100%; max-height:100%;}
.span4 { width:25%; display:block;}
.red { background-color: red; float:left; overflow:hidden;}
.blue { background-color: blue; float:left; overflow:hidden;}
.green { background-color: green; float:left; overflow:hidden;}
.purple { background-color: purple; float:left; overflow:hidden;}

2 个解决方案

#1


5  

DEMO

html

<div class="span4 red">
    <img src="http://img.thesun.co.uk/aidemitlum/archive/01500/SNE0125Q---_149991_1500286a.jpg">
    <div class="overlay">
        <span>Text text text</span>
        <button>Button</button>
    </div>
</div>

css

.span4 {
    width:25%;
    display:block;
    position:relative; /*added*/
}

/*added*/
.overlay {
    position:absolute;
    top:0;
    width:0;
    width:100%;
    height:100%;
    background-color:rgba(0, 0, 0, 0.3);
    display:none;
}
.red:hover .overlay {
    display:block;
}

#2


4  

Try using the :after pseudo selector (note browser compatability)

尝试使用:after伪选择器(注意浏览器兼容性)

http://jsfiddle.net/HGWPZ/1/

.span4 {
    width:25%;
    display:block;
    position: relative;
}

.span4:hover:after
{
    content: '';
    position: absolute;
    top: 0; bottom: 0;
    left: 0; right: 0;
}

.red:hover:after
{
    background: RGBA(255, 0, 0, .3);
}

.blue:hover:after
{
    background: RGBA(0, 0, 255, .3);
}

.green:hover:after
{
    background: RGBA(0, 255, 0, .3);
}

.purple:hover:after
{
    background: RGBA(230, 230, 250, .3);
}

idk what the RGB value for purple is though :p so play around with it

idk虽然紫色的RGB值是什么:p所以玩它

#1


5  

DEMO

html

<div class="span4 red">
    <img src="http://img.thesun.co.uk/aidemitlum/archive/01500/SNE0125Q---_149991_1500286a.jpg">
    <div class="overlay">
        <span>Text text text</span>
        <button>Button</button>
    </div>
</div>

css

.span4 {
    width:25%;
    display:block;
    position:relative; /*added*/
}

/*added*/
.overlay {
    position:absolute;
    top:0;
    width:0;
    width:100%;
    height:100%;
    background-color:rgba(0, 0, 0, 0.3);
    display:none;
}
.red:hover .overlay {
    display:block;
}

#2


4  

Try using the :after pseudo selector (note browser compatability)

尝试使用:after伪选择器(注意浏览器兼容性)

http://jsfiddle.net/HGWPZ/1/

.span4 {
    width:25%;
    display:block;
    position: relative;
}

.span4:hover:after
{
    content: '';
    position: absolute;
    top: 0; bottom: 0;
    left: 0; right: 0;
}

.red:hover:after
{
    background: RGBA(255, 0, 0, .3);
}

.blue:hover:after
{
    background: RGBA(0, 0, 255, .3);
}

.green:hover:after
{
    background: RGBA(0, 255, 0, .3);
}

.purple:hover:after
{
    background: RGBA(230, 230, 250, .3);
}

idk what the RGB value for purple is though :p so play around with it

idk虽然紫色的RGB值是什么:p所以玩它