Skip to main content
CSS Tip

Split and assemble an image using CSS mask

Split an image into pieces using the mask property, then show it fully on hover. A single-element implementation using less than 10 CSS declarations.

Assemble a broken image using CSS

img {
--g: 15px; /* control the gap */

width: 280px;
aspect-ratio: 1;
box-sizing: border-box;
--_g: var(--g)/calc(50% - var(--g)) calc(50% - var(--g))
no-repeat conic-gradient(#000 0 0);
mask:
left var(--_i,) top var(--_g),
bottom var(--_i,) left var(--_g),
top var(--_i,) right var(--_g),
right var(--_i,) bottom var(--_g);
transition: .3s linear;
}
img:hover {
--_i: var(--g);
padding: var(--g);
}

See the Pen Image assembly hover effect by Temani Afif (@t_afif) on CodePen.

Another simpler version without the moving effect:

img {
--_g: 10%/45% 45% no-repeat conic-gradient(#000 0 0);
mask:
left var(--_i,) top var(--_g),
bottom var(--_i,) left var(--_g),
top var(--_i,) right var(--_g),
right var(--_i,) bottom var(--_g);
transition: .3s linear;
}
img:hover {
--_i: 10%;
}

See the Pen Image mask hover effect by Temani Afif (@t_afif) on CodePen.


More CSS Tips