A hover effect where an avatar appears to pop forward from inside a circle, first described by Temani Afif on CSS-Tricks, can be built from a single <img> element and fewer than 20 lines of CSS. The construction relies on a radial gradient for the circle, a transform: scale() on hover, and outline plus mask to sell the illusion that the head passes through a hole.

Starting from the markup

The demo uses a single image, no wrapper elements. A square image file with a transparent background works best.

<img src="" alt="">
Designed by Cang

Breaking the effect into parts

Three things happen visually, and each maps to one CSS mechanism:

  • The image grows on hover, via transform: scale().
  • A circle sits behind the avatar, drawn with a hard-stop radial-gradient.
  • A border at the bottom of the circle makes the avatar look as if it sits behind it; this uses outline.

A CSS variable, --b, holds the thickness of the "border" that the gradient's hard color stops describe. The scale factor lives in --f, defaulting to 1.

img {
  width: 280px;
  aspect-ratio: 1;
  cursor: pointer;
  transition: .5s;
}
img:hover {
  transform: scale(1.35);
}

Keeping the circle steady while the image grows

Since the element itself is scaled, the gradient scaled with it, so the gradient's size must shrink as the image grows. The background is centered with width calc(100%/var(--f)) and height 100%. At --f: 1 nothing changes and the gradient fills the container; as --f rises, the element grows and the gradient contracts.

img {
  --b: 5px; /* border width */

  width: 280px;
  aspect-ratio: 1;
  background:
    radial-gradient(
      circle closest-side,
      #ECD078 calc(99% - var(--b)),
      #C02942 calc(100% - var(--b)) 99%,
      #0000
    );
  cursor: pointer;
  transition: .5s;
}
img:hover {
  transform: scale(1.35);
}
radial-gradient(
  circle closest-side,
  #ECD078 calc(99% - var(--b)),
  #C02942 calc(100% - var(--b)) 99%,
  lightblue
);
background: radial-gradient() 50% / calc(100% / var(--f)) 100% no-repeat;

Drawing the bottom border with outline

A border-bottom attempt could not be matched to the circle's size. The working approach is outline together with outline-offset. The outline must land on the circle created by the gradient, and it must be drawn from outside the box toward the inside, so the offset is negative. Because the element is scaled, the result is divided by --f, and the border thickness is then subtracted. A border-radius on the bottom side lets the outline follow the gradient's curvature.

img {
  --s: 280px; /* image size */
  --b: 5px; /* border thickness */
  --c: #C02942; /* border color */
  --f: 1; /* initial scale */

  width: var(--s);
  aspect-ratio: 1;
  cursor: pointer;
  border-radius: 0 0 999px 999px;
  outline: var(--b) solid var(--c);
  outline-offset: calc((1 / var(--f) - 1) * var(--s) / 2 - var(--b));
  background: 
    radial-gradient(
      circle closest-side,
      #ECD078 calc(99% - var(--b)),
      var(--c) calc(100% - var(--b)) 99%,
      #0000
    ) 50% / calc(100% / var(--f)) 100% no-repeat;
  transform: scale(var(--f));
  transition: .5s;
}
img:hover {
  --f: 1.35; /* hover scale */
}
Diagram of the background transition.
Offset = ((f - 1) * S/2) / f = (1 - 1/f) * S/2
Offset = (1/f - 1) * S/2
outline-offset: calc((1 / var(--f) - 1) * var(--s) / 2) - var(--b));

Trimming the top with padding and mask

Top padding pushes the outline clear of the avatar's head; the value is not derived from a formula but proportional to the element's size so the spacing stays consistent. This adds padding to the element, so the background must be told where to stop with the content-box value.

img {
  --s: 280px; /* image size */
  --b: 5px;   /* border thickness */
  --c: #C02942; /* border color */
  --f: 1; /* initial scale */

  width: var(--s);
  aspect-ratio: 1;
  padding-block-start: calc(var(--s)/5);
  /* etc. */
}
img:hover {
  --f: 1.35; /* hover scale */
}
background:
  radial-gradient(
    circle closest-side,
    #ECD078 calc(99% - var(--b)),
    var(--c) calc(100% - var(--b)) 99%,
    #0000
  ) 50%/calc(100%/var(--f)) 100% no-repeat content-box;

The mask property shows the visible region, which has two parts: a circle at the bottom matching the radial gradient's dimension and curvature, and a rectangle at the top covering the area inside the outline. The outline sits outside the rectangular area, so only the bottom arc of it survives.

Showing how the mask applies to the bottom portion of the circle.
img {
  --s: 280px; /* image size */
  --b: 5px; /* border thickness */
  --c: #C02942; /* border color */
  --f: 1; /* initial scale */

  --_g: 50% / calc(100% / var(--f)) 100% no-repeat content-box;
  --_o: calc((1 / var(--f) - 1) * var(--s) / 2 - var(--b));

  width: var(--s);
  aspect-ratio: 1;
  padding-top: calc(var(--s)/5);
  cursor: pointer;
  border-radius: 0 0 999px 999px;
  outline: var(--b) solid var(--c);
  outline-offset: var(--_o);
  background: 
    radial-gradient(
      circle closest-side,
      #ECD078 calc(99% - var(--b)),
      var(--c) calc(100% - var(--b)) 99%,
      #0000) var(--_g);
  mask:
    linear-gradient(#000 0 0) no-repeat
    50% calc(-1 * var(--_o)) / calc(100% / var(--f) - 2 * var(--b)) 50%,
    radial-gradient(
      circle closest-side,
      #000 99%,
      #0000) var(--_g);
  transform: scale(var(--f));
  transition: .5s;
}
img:hover {
  --f: 1.35; /* hover scale */
}

The two mask layers

The mask reuses a radial-gradient() like the background's; its shared parts are consolidated in a variable, --_g. A linear-gradient() supplies the top rectangle: its width is the gradient's width minus twice the border thickness, its height is 50% of the element, and it is placed at the horizontal center, offset from the top by the same value the outline uses.

--_g: 50% / calc(100% / var(--f)) 100% no-repeat content-box;

mask:
  radial-gradient(
    circle closest-side,
    #000 99%,
    #0000) var(--_g);
--_g: 50% / calc(100% / var(--f)) 100% no-repeat content-box;

mask:
  linear-gradient(#000 0 0) no-repeat
    50% calc(-1 * var(--_o)) / calc(100% / var(--f) - 2 * var(--b)) 50%,
  radial-gradient(
    circle closest-side,
    #000 99%,
    #0000) var(--_g);
calc(100% / var(--f) - 2 * var(--b))
--_o: calc((1 / var(--f) - 1) * var(--s) / 2 - var(--b));

Those offsets have opposite signs: the outline needs a negative offset to move inside the box, while the gradient needs a positive one to move down from the top, which is why the value in --_o is multiplied by -1 for the mask.

What the exercise yields

The result is a hover animation on one HTML <img> and less than 20 lines of CSS, built from gradients, gradients with hard stops, masking, the outline property's offset behavior, and transformations. More HTML could have simplified the CSS; the constraint is the point. Readers working with a real photo of their own can submit a demo to the collection under the original article.