背景图像上的文字删除其悬停状态

时间:2017-01-06 18:41:33

标签: html css html5 css3

我在图像上面有这个文字。当我将鼠标悬停在文本上时,我为背景图像创建的悬停不起作用。有人会有解决方案吗?

的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;
}

谢谢!

2 个答案:

答案 0 :(得分:2)

<a href="#" class="headline link--no-decor">移到backround元素中,这样就不会阻止hover事件。

为防止.background-container:after阻止a.headline悬停,我们需要将position: relativez-index: 1分配给a.headline

&#13;
&#13;
.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;
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

.background-container:hover:after上添加悬停在此.card:hover选择器上的鼠标悬停,并选择像这样的背景 - 投币器<{1}}

这应该有效,

.card:hover a .background-container:after

查看完整代码

.card:hover a .background-container:after {
      background: linear-gradient(20deg,rgba(255, 0, 0,.6),rgba(255, 255, 0,.6));
      border: 2px solid red;
}
.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));
}
.card:hover a .background-container:after {
  background: linear-gradient(20deg, rgba(255, 0, 0, .6), rgba(255, 255, 0, .6));
  border: 2px solid red;
}
.background {
  height: 200px;
}