← Back to CSS Course | Chapter 8: Animations & Effects | Lesson 12 of 22

CSS Hover Overlays

A hover overlay is a caption, icon, or tinted layer that sits invisibly on top of an image until the cursor arrives, then fades or slides into view — a common pattern in photo galleries and product grids.
Syntax
css
.container {
  position: relative;
}
.overlay {
  position: absolute;
  inset: 0;
  opacity: 0;
  transition: opacity duration;
}
.container:hover .overlay {
  opacity: 1;
}

The Overlay Structure

A hover overlay is typically built from a positioned wrapper (position: relative) containing the image plus a second element (position: absolute, same size) sitting directly on top of it, initially hidden with opacity: 0 or a transform that pushes it out of view.

Note:
  • Accepted values:
  • position: absolute — layers the overlay on the container
  • inset: 0 — stretches it over the whole box
  • background: rgba(...) — semi-transparent color
  • opacity: 0 — hidden until hover

Example: The Overlay Structure

css
<style>
.wrap {
  position: relative;
}
.overlay {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.6);
  color: white;
  opacity: 0;
}
.wrap:hover .overlay {
  opacity: 1;
}
</style>
<div class="wrap">
  <img src="https://placehold.co/200x150" alt="Base">
  <div class="overlay">Overlay layer, hidden by default</div>
</div>

Revealing the Overlay on Hover

The overlay's visible state is triggered by a :hover selector on the wrapper, not on the overlay itself, so that hovering anywhere over the image (not just the exact overlay area) reveals it — combined with a transition for a smooth fade or slide.

Note:
  • Accepted values:
  • opacity: 0 to 1 — fade in
  • visibility: hidden to visible — toggles visibility
  • transition: opacity 0.3s — smooth reveal
  • :hover — the trigger state

Example: Revealing the Overlay on Hover

css
<style>
.wrap {
  position: relative;
}
.overlay {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.6);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.3s;
}
.wrap:hover .overlay {
  opacity: 1;
}
</style>
<div class="wrap">
  <img src="https://placehold.co/200x150" alt="Base">
  <div class="overlay">Revealed by hovering the wrapper</div>
</div>

Text Captions on Overlays

Placing a caption inside the overlay element lets it fade in alongside a semi-transparent background tint, a common pattern for photo galleries where the caption names the image without cluttering the default view.

Note:
  • Accepted values:
  • display: flex — lays out the caption
  • align-items: center — vertical centering
  • justify-content: center — horizontal centering
  • text-align: center — centers the text lines

Example: Text Captions on Overlays

css
<style>
.wrap {
  position: relative;
}
.caption {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.5);
  color: white;
  opacity: 0;
  transition: opacity 0.3s;
}
.wrap:hover .caption {
  opacity: 1;
}
</style>
<div class="wrap">
  <img src="https://placehold.co/200x150" alt="Photo">
  <div class="caption">Photo Title</div>
</div>

Icon Reveal Overlays

Instead of full opacity text, some overlays reveal just a small icon (like a magnifying glass or a play button) centered over the image, often combined with a slight zoom or darken effect on the image itself underneath.

Note:
  • Accepted values:
  • opacity: 0 to 1 — fade in
  • visibility: hidden to visible — toggles visibility
  • transition: opacity 0.3s — smooth reveal
  • :hover — the trigger state

Example: Icon Reveal Overlays

css
<style>
.wrap {
  position: relative;
}
.icon {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  opacity: 0;
  transition: opacity 0.3s;
}
.wrap:hover .icon {
  opacity: 1;
}
</style>
<div class="wrap">
  <img src="https://placehold.co/200x150" alt="Photo">
  <span class="icon">🔍</span>
</div>

Sliding vs Fading Overlays

An overlay can either fade in via opacity or slide in via a transform: translateY() that moves it from just outside the box into place — sliding tends to feel more directional and is common for caption bars that rise from the bottom edge.

Note:
  • Accepted values:
  • transform: translateY(100%) — starts below
  • transform: translateY(0) — slides into place
  • opacity: 0 to 1 — fades instead of slides
  • overflow: hidden — clips the sliding overlay

Example: Sliding vs Fading Overlays

css
<style>
.wrap {
  display: flex;
  gap: 10px;
  overflow: hidden;
}
.fade {
  background: lightblue;
  padding: 10px;
  opacity: 0;
  transition: opacity 0.3s;
}
.slide {
  background: lightgreen;
  padding: 10px;
  transform: translateY(100%);
  transition: transform 0.3s;
}
.wrap:hover .fade {
  opacity: 1;
}
.wrap:hover .slide {
  transform: translateY(0);
}
</style>
<div class="wrap">
  <div class="fade">Fades in</div>
  <div class="slide">Slides up from below</div>
</div>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting position: relative on the container, so the absolute overlay does not sit on the image.
  2. Using display: none for the overlay, which cannot be animated; use opacity or transform.
  3. Putting text in the overlay with low contrast against the image, so the caption is hard to read.
Browser Support

Supported in all modern browsers: Chrome, Firefox, Safari, Edge and Opera.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.