Text over background image removes its hover state

Imalea

I have this text on top of an image. When I hover on the text, the hover I created for the background image doesn't work. Would anyone have a solution for this?

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>

And the 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;
}

Thanks!

Ori Drori

Move <a href="#" class="headline link--no-decor"> into the backround element, so it won't block the hover event.

To prevent the .background-container:after from block the a.headline hover, we need to assign position: relative, and z-index: 1 to a.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>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Hover over text removes border of <img> above it

From Dev

Navigation Hover Over Background Image

From Dev

background-image hover state

From Dev

background-image hover state

From Dev

Slide text over image on hover

From Dev

Set background and hover over image on button

From Dev

Overlapping text over background image

From Dev

Background Image Appearing Over Text

From Dev

Hover over image text slide out

From Dev

Move Text Up Over Image On Hover

From Dev

Hover Over Text, Image Fades In & Out

From Dev

On image hover display text-div over it

From Dev

HTML Text Appears Over Image On Hover

From Dev

Hover over image text slide out

From Dev

Move Text Up Over Image On Hover

From Dev

Hover text over post image in wordpress

From Dev

On image hover display text-div over it

From Dev

Hover effect over div with image and text

From Dev

Add hover text over a small image

From Dev

How to blur background image on hover but not the text

From Dev

on hover animate the background image but not the text on top

From Dev

fade background image and then show text(not faded) on hover

From Dev

Css blur on background image :hover but not on text

From Dev

Image hover changes background colour and adds text

From Dev

Maintaining the hover state background color while hovering its submenu items

From Dev

Change a div`s background image when hover over the image

From Dev

How can I stop an image from losing its opaque property when I hover over some text above it?

From Dev

Iterating over each image in a list and define a hover state using jQuery

From Dev

GD Lib - Text with background over image