gpt4 book ai didi

html - 背景图像上的文字移除了它的悬停状态

转载 作者:太空宇宙 更新时间:2023-11-03 22:47:18 25 4
gpt4 key购买 nike

我在图片上放置了这段文字。当我将鼠标悬停在文本上时,我为背景图像创建的悬停不起作用。有人对此有解决方案吗?

JSfiddle: https://jsfiddle.net/marineboudeau/h177pdne/4/

<div class="card">
<a href="#">
<div class="background-container">
<div class="background" style="background: url('https://unsplash.it/300/200/?random') no-repeat; background-size: cover; background-position: center;"></div>
</div>
</a>
<a href="#" class="headline link--no-decor">
<h2>Headline</h2>
</a>
</div>

还有 CSS:

.card {
width: 300px;
height: 200px;
}

a {
text-decoration: none;
}

h2 {
color: white;
font-family: Helvetica, sans-serif;
}

h2:hover {
color: yellow;
}

.headline {
padding: 0 22px;
position: relative;
margin-top: -80px;
display: flex;
flex-direction: row;
align-items: center;
}

.background-container {
top: 0;
left: 0;
width: 100%;
height: 100%;
display: block;
position: relative;
}

.background-container:after {
content: "gradient mask for image";
overflow: hidden;
position: absolute;
text-indent: -9999rem;
font-size: 0;
line-height: 0;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: linear-gradient(20deg ,rgba(0,0,0,.8), rgba(0,0,0,0));
}

.background-container:hover:after {
background: linear-gradient(20deg,rgba(255, 0, 0,.6),rgba(255, 255, 0,.6));
border: 2px solid red;
}

.background {
height: 200px;
}

谢谢!

最佳答案

移动<a href="#" class="headline link--no-decor">进入backround元素,因此它不会阻止 hover事件。

为了防止 .background-container:after从 block a.headline悬停,我们需要分配 position: relative , 和 z-index: 1a.headline .

.card {
width: 300px;
height: 200px;
}

a {
text-decoration: none;
}

h2 {
color: white;
font-family: Helvetica, sans-serif;
}

h2:hover {
color: yellow;
}

.headline {
position: relative;
z-index: 1;
padding: 0 22px;
position: relative;
margin-top: -80px;
display: flex;
flex-direction: row;
align-items: center;
}

.background-container {
top: 0;
left: 0;
width: 100%;
height: 100%;
display: block;
position: relative;
}

.background-container:after {
content: "gradient mask for image";
overflow: hidden;
position: absolute;
text-indent: -9999rem;
font-size: 0;
line-height: 0;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: linear-gradient(20deg ,rgba(0,0,0,.8), rgba(0,0,0,0));
}

.background-container:hover:after {
background: linear-gradient(20deg,rgba(255, 0, 0,.6),rgba(255, 255, 0,.6));
border: 2px solid red;
}

.background {
height: 200px;
}
<div class="card">
<a href="#">
<div class="background-container">
<div class="background" style="background: url('https://unsplash.it/300/200/?random') no-repeat; background-size: cover; background-position: center;"></div>
<a href="#" class="headline link--no-decor">
<h2>Headline</h2>
</a>
</div>
</a>
</div>

关于html - 背景图像上的文字移除了它的悬停状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41512199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com