Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Css text overlay on hover

Hi there, I'm building a blog and I want my post list page to preview my posts with images that have a text overlay with a dark background on hover.

Below you can see a snippet of the css I applied for the image only. What I have to next, is a mystery to me.

========================================================================

html: <figure class="post-preview"> <img src="img/appartment-kali.jpg" alt=""> </figure>

css: .post-preview { max-width: 100%; max-height: 200px; overflow: hidden }

1 Answer

Steven Parker
Steven Parker
243,318 Points

There are quite a few ways to do what you describe.

Here''s one that involves another element positioned directly over your figure that becomes visible on hover:

code.html
<figure class="post-preview">
  <img src="img/appartment-kali.jpg" alt="">
  <div class="overlay">Something like this?</div>
</figure>
styles.css
.post-preview {
  max-width: 100%;
  max-height: 200px;
  overflow: hidden;
  position: relative;   /* new stuff from here down... */
}
.overlay {
  display: none;
  text-align: center;
  background: rgba(20, 20, 40, .8);
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  color: white;
  font-size: 1.5rem;
}
.post-preview:hover .overlay {
  display: block;
}

Hi thanks Steven,, thanks a lot! this really helped me out.. Did some customizations and it looks like I want.