Skip to content
Tutorials Center
My learning

Topics

Lesson 54 of 139 ยท Backgrounds and Effects

Image Reflections and Hover Effects

Create a mirror reflection under an image with -webkit-box-reflect or a portable mask-image version, and build zoom, grayscale and caption hover effects the accessible way.

A glossy product shot that seems to stand on a shiny table. A gallery where photos start in black and white and bloom into color when you point at them. These effects are small, but they make a page feel finished, and each one is only a handful of CSS lines.

This lesson has two parts. First you will draw a reflection under an image, once with the quick but non-standard -webkit-box-reflect and once with a portable version built from a flipped copy and a mask. Then you will build three hover effects (zoom, grayscale-to-color and a sliding caption) and make them behave on touch screens and for people who prefer less motion. You will use transforms, transitions and filters throughout.

The quick way: -webkit-box-reflect

-webkit-box-reflect paints a mirrored copy of an element next to it. You give it a direction (above, below, left or right), an optional offset, and an optional mask image that fades the reflection out:

CSS
img {
  -webkit-box-reflect: below 4px linear-gradient(transparent 55%, rgba(255, 255, 255, 0.4));
}

Read it left to right: reflect below, leave a 4px gap, and use a gradient as the mask. Where the mask is transparent, the reflection is hidden; where it is partly white, you see it faintly. The mask is written for the image before it is mirrored, so "transparent at the top, faint at the bottom" gives a natural result: the bottom edge of the image is the part nearest the reflection, so that is where the reflection is strongest, and it fades away from there.

A reflection with -webkit-box-reflect

The reflection is purely visual: it takes no space in the layout, cannot be selected or focused, and screen readers ignore it. That is a good thing for decoration, but note the first point. Since it takes no space, whatever sits below the image can overlap the reflection, so leave room with margin.

-webkit-box-reflect is a non-standard property. Chromium browsers and Safari support it, but Firefox does not, and it is not on a standards track. Check current support before relying on it. Without it the page simply shows no reflection, which is a graceful failure for decoration, but the portable method below works everywhere.

The portable way: a flipped copy with a mask

Every browser can do the same job with three ordinary tools: a second copy of the image, transform: scaleY(-1) to flip it vertically, and mask-image with a gradient to fade it out.

The second copy is decoration, so it gets an empty alt="" and aria-hidden="true"; assistive technology then reads the picture once. If the image is a CSS background instead of an <img>, you can draw the reflection from a pseudo-element with the same background-image.

A reflection that works in every browser

The gradient is the same one you used with -webkit-box-reflect. It describes the unflipped element, and transform flips the element together with its mask: the first 55% (the top of the original, hidden) ends up farthest from the real image, and the faint bottom edge ends up right next to it. That is the natural look. If your reflection fades the wrong way, you have reversed the gradient.

Unlike -webkit-box-reflect, the mirror here is a real element with a real box. It takes up space in the layout, you can shift it with margin, blur it with filter: blur(1px), or tilt it with more transforms. That control is worth the extra markup.

Hover effect 1: zoom inside a frame

The classic zoom grows the image on hover while the frame stays still. Two parts make it work: the wrapper has overflow: hidden to clip the growing image, and the image has transform: scale() with a transition.

CSS
.zoom { overflow: hidden; border-radius: 12px; }
.zoom img { display: block; width: 100%; transition: transform 0.4s ease; }
.zoom:hover img { transform: scale(1.1); }

Use transform, not width or height: transforms are cheap, because they do not change the layout around the image. The image can also be scaled from a different point with transform-origin, for instance transform-origin: 30% 20% to zoom toward a face.

Hover effect 2: grayscale to color

Combine filter: grayscale() with a transition:

CSS
.gallery img { filter: grayscale(1); transition: filter 0.3s; }
.gallery img:hover { filter: grayscale(0); }

It gives a gallery a calm, uniform look at rest, and lets the color appear as the visitor explores. You can chain more functions, such as grayscale(1) brightness(0.8), and animate to none.

Hover effect 3: a caption that slides up

Here the caption starts below the frame's edge, hidden by overflow: hidden, and slides up with a transform. This effect is the one that most needs care for keyboard users, so the editor below shows it together with the other two, and with the two accessibility rules you should always add.

Three hover effects, with focus and reduced-motion support

Look at the two accessibility rules at the end of the CSS.

  • Do not rely on hover. The selectors list :focus-visible next to :hover, so a keyboard user tabbing through the links sees the same effects. And @media (hover: none), which matches touch-first devices, reveals the caption permanently, because a phone has no pointer to hover with. See hover and pointer media features.
  • Respect reduced motion. Some people get dizzy from zooming and sliding motion. @media (prefers-reduced-motion: reduce) lets them opt out system-wide, and your CSS honors it by removing the transitions.

Anything that matters, such as a name, a price or a call to action, must not be hidden only behind a hover. Treat hover as a bonus.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What happens to the layout when you use -webkit-box-reflect: below?

    The reflection is painted but is not part of the layout, so you need margin if you want space for it.
  2. 2. Why is the portable mirror image given alt="" aria-hidden="true"?

    The copy exists only for the visual effect. An empty alt plus aria-hidden keeps it out of the accessibility tree.
  3. 3. In the flipped-copy reflection, why does the mask gradient start with transparent?

    transform flips the element together with its mask, so the first color of the gradient ends up farthest from the real image.
  4. 4. Which pair makes a zoom-on-hover effect stay inside its frame?

    The frame clips whatever grows past its edge, and a transform changes how the image is painted without disturbing the layout.
  5. 5. Why should hover effects also be written for :focus-visible and @media (hover: none)?

    Not everyone has a mouse. Giving keyboard focus the same styles, and showing important content without hover on touch devices, keeps the page usable for all.

Summary

  • -webkit-box-reflect: below 6px <mask> draws a quick reflection in Chromium and Safari; it is non-standard, takes no layout space and is missing in Firefox.
  • The portable reflection is a second, decorative copy (alt="", aria-hidden), flipped with transform: scaleY(-1) and faded with a gradient mask-image. Remember that the mask flips with the element.
  • Zoom effects need overflow: hidden on the frame and transform: scale() with a transition on the image.
  • filter: grayscale() plus a transition gives the gray-to-color gallery effect.
  • Pair every :hover with :focus-visible, add @media (hover: none) fallbacks, and turn motion off under prefers-reduced-motion.

Next: Blend Modes โ€” mix colors between layers and elements like a graphics editor.