MalcolmInTheCenter
MalcolmInTheCenter

Reputation: 1605

How do I keep text on image after hovering on overlay?

I have an overlay on an image with text on it.
When I hover over it the overlay disappears but so does the text. I want the text to stay on top of the image, how can I edit my code to do that?
This is my css code:

div.homepage-popular-categories {
    position: relative;
    display: inline-block;
}

div.homepage-popular-categories p {
  margin: 0;
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  color: #eeeeec;
  background: rgba(0,0,0,0.5);
  transition: opacity 0.5s;
  opacity: 1;
  text-align: center;
  font-family: sans-serif;
  font-size: 1.2em;
  font-weight: bold;
}

div.homepage-popular-categories p:before {
  content: '';
  display: inline-block;
  height: 100%;
  vertical-align: middle;
}
div.homepage-popular-categories p:hover {
  opacity: 0;
  transition: opacity 0.5s;
  cursor: default;
}

Here's my fiddle:
https://jsfiddle.net/7xax9p5e/

Upvotes: 0

Views: 22

Answers (1)

Michał Perłakowski
Michał Perłakowski

Reputation: 92719

Instead of using opacity transition, use background transition.

div.homepage-popular-categories {
  position: relative;
  display: inline-block;
}

div.homepage-popular-categories p {
  margin: 0;
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  color: #eeeeec;
  background: rgba(0,0,0,0.5);
  transition: background 0.5s;
  opacity: 1;
  text-align: center;
  font-family: sans-serif;
  font-size: 1.2em;
  font-weight: bold;
}

div.homepage-popular-categories p:before {
  content: '';
  display: inline-block;
  height: 100%;
  vertical-align: middle;
}
div.homepage-popular-categories p:hover {
  background: rgba(0,0,0,0);
  cursor: default;
}
<div class="homepage-popular-categories">
	<img src="http://www.eonline.com/eol_images/Entire_Site/20131022/rs_560x415-131122130228-1024.family-guy.jpg">
	<p>Family Guy</p>
	
</div>

Upvotes: 3

Related Questions