April 12, 2024
Sliding 3D Image Frames In CSS

In a previous article, we played with CSS masks to create cool hover effects where the main challenge was to rely only on the <img> tag as our markup. In this article, pick up where we left off by “revealing” the image from behind a sliding door sort of thing — like opening up a box and finding a photograph in it.

This is because the padding has a transition that goes from s – 2*b to 0. Meanwhile, the background transitions from 100% (equivalent to –s) to 0. There’s a difference equal to 2*b. The background covers the entire area, while the padding covers less of it. We need to account for this.

Ideally, the padding transition would take less time to complete and have a small delay at the beginning to sync things up, but finding the correct timing won’t be an easy task. Instead, let’s increase the padding transition’s range to make it equal to the background.

img {
–h: calc(var(–s) – var(–b));
padding-top: min(var(–h), var(–s) – 2*var(–b));
transition: –h 1s linear;
}
img:hover {
–h: calc(-1 * var(–b));
}

The new variable, –h, transitions from s – b to -b on hover, so we have the needed range since the difference is equal to –s, making it equal to the background and clip-path transitions.

The trick is the min() function. When –h transitions from s – b to s – 2*b, the padding is equal to s – 2*b. No padding changes during that brief transition. Then, when –h reaches 0 and transitions from 0 to -b, the padding remains equal to 0 since, by default, it cannot be a negative value.

It would be more intuitive to use clamp() instead:

padding-top: clamp(0px, var(–h), var(–s) – 2*var(–b));

That said, we don’t need to specify the lower parameter since padding cannot be negative and will, by default, be clamped to 0 if you give it a negative value.

We are getting much closer to the final result!

First, we increase the border’s thickness on the left and bottom sides of the image:

img {
–b: 10px; /* the image border */
–d: 30px; /* the depth */

border: solid #0000;
border-width: var(–b) var(–b) calc(var(–b) + var(–d)) calc(var(–b) + var(–d));
}

Second, we add a conic-gradient() on the background to create darker colors around the box:

background:
conic-gradient(at left var(–d) bottom var(–d),
#0000 25%,#0008 0 62.5%,#0004 0)
var(–c);

Notice the semi-transparent black color values (e.g., #0008 and #0004). The slight bit of transparency blends with the colors behind it to create the illusion of a dark variation of the main color since the gradient is placed above the background color.

And lastly, we apply a clip-path to cut out the corners that establish the 3D box.

clip-path: polygon(var(–d) 0, 100% 0, 100% calc(100% – var(–d)), calc(100% – var(–d)) 100%, 0 100%, 0 var(–d));

See the Pen The image within a 3D box by Temani Afif.

Now that we see and understand how the 3D effect is built let’s put back the things we removed earlier, starting with the padding:

See the Pen Putting back the padding animation by Temani Afif.

It works fine. But note how we’ve introduced the depth (–d) to the formula. That’s because the bottom border is no longer equal to b but b + d.

–h: calc(var(–s) – var(–b) – var(–d));
padding-top: min(var(–h),var(–s) – 2*var(–b) – var(–d));

Let’s do the same thing with the linear gradient. We need to decrease its size so it covers the same area as it did before we introduced the depth so that it doesn’t overlap with the conic gradient:

See the Pen Putting back the gradient animation by Temani Afif.

We are getting closer! The last piece we need to add back in from earlier is the clip-path transition that is combined with the box-shadow. We cannot reuse the same code we used before since we changed the clip-path value to create the 3D box shape. But we can still transition it to get the sliding result we want.

The idea is to have two points at the top that move up and down to reveal and hide the box-shadow while the other points remain fixed. Here is a small video to illustrate the movement of the points.

See that? We have five fixed points. The two at the top move to increase the area of the polygon and reveal the box shadow.

img {
clip-path: polygon(
var(–d) 0, /* –> var(–d) calc(-1*(var(–s) – var(–d))) */
100% 0, /* –> 100% calc(-1*(var(–s) – var(–d))) */

/* the fixed points */
100% calc(100% – var(–d)), /* 1 */
calc(100% – var(–d)) 100%, /* 2 */
0 100%, /* 3 */
0 var(–d), /* 4 */
var(–d) 0); /* 5 */
}

And we’re done! We’re left with a nice 3D frame around the image element with a cover that slides up and down on hover. And we did it with zero extra markup or reaching for pseudo-elements!

See the Pen 3D image with reveal effect by Temani Afif.

And here is the first demo I shared at the start of this article, showing the two sliding variations.

See the Pen Image gift box (hover to reveal) by Temani Afif.

This last demo is an optimized version of what we did together. I have written most of the formulas using the variable –h so that I only update one value on hover. It also includes another variation. Can you reverse-engineer it and see how its code differs from the one we did together?

One More 3D Example

Want another fancy effect that uses 3D effects and sliding overlays? Here’s one I put together using a different 3D perspective where the overlay splits open rather than sliding from one side to the other.

See the Pen Image gift box II (hover to reveal) by Temani Afif.

Your homework is to dissect the code. It may look complex, but if you trace the steps we completed for the original demo, I think you’ll find that it’s not a terribly different approach. The sliding effect still combines the padding, the object-* properties, and clip-path but with different values to produce this new effect.

Conclusion

I hope you enjoyed this little 3D image experiment and the fancy effect we applied to it. I know that adding an extra element (i.e., a parent <div> as a wrapper) to the markup would have made the effect a lot easier to achieve, as would pseudo-elements and translations. But we are here for the challenge and learning opportunity, right?

Limiting the HTML to only a single element allows us to push the limits of CSS to discover new techniques that can save us time and bytes, especially in those situations where you might not have direct access to modify HTML, like when you’re working in a CMS template. Don’t look at this as an over-complicated exercise. It’s an exercise that challenges us to leverage the power and flexibility of CSS.

Leave a Reply

Your email address will not be published. Required fields are marked *

Send