The magical use of CSS clip-path
attribute: Explore a variety of creative techniques and application cases. This article will share a series of interesting effects achieved using clip-path
attribute, hoping to inspire you to apply it in your project or make creative attempts.
This is the third article about clip-path
published by the author on CSS-Tricks. If you want to know the background, you can first read the following article:
- Create interactive effects using CSS
clip-path
- Create interactive effects using CSS
clip-path
, Part 2
This article will introduce new ideas!
Creative One: Double cut
A clever trick is to use clip-path
multiple times to crop content. This may sound obvious, but in fact few people use this concept.
For example, let's look at an expanded menu:
clip-path
can only be applied once to a single DOM node. A node cannot have multiple active instances of the same CSS rules at the same time, which means that each instance has only one clip-path
. However, there is no upper limit on the number of times a combined crop node is combined. For example, we can cut a<div> Placed in another cropped<code><div> Inside, and so on. In the ancestral relationship of the DOM node, we can apply as many independent crops. This is exactly what is done in the above demonstration. I have one cropped node fill another cropped node. The parent node acts as the boundary, and the child nodes fill the parent node when scaling. This produces an unusual effect of a rounded menu. Think of it as an advanced method of <code>overflow: hidden
.
Of course, you can think that SVG is more suitable for this purpose. Compared with clip-path
, SVG can achieve more functions. This includes smooth zoom. If clip-path
fully supports the Bezier curve, the situation will be different. But at the time of writing, that's not the case. Anyway, clip-path
is very convenient. With a node, a CSS rule, you can start. As far as the above demonstration is concerned, clip-path
does the job and is therefore a viable option.
I made a short video to explain how the menu works internally:
Creative Two: Scale Clipping Path
Another (less obvious) trick is to use clip-path
for scaling. We can actually use CSS transition to animate clip-path
!
The transition system is amazing in how it is built. In my opinion, its addition is one of the biggest leaps in the development of Web technology in recent years. It supports transitions of various values. clip-path
is an acceptable value that we can animation. Animation usually means interpolation between two extreme values. For clip-path
, this means interpolation between two completely different paths. This is where the fine animation system of the web shows its advantages. It works not only for single values, but also for animated value sets.
When animating clip-path
, each coordinate is interpolated separately . This is very important. It makes clip-path
animation look coherent and smooth.
Let's take a look at the demo. Click on the image to restart the effect:
In this demo, I used clip-path
transition. It is used to scale from one clip-path
covering a small area to another huge clip-path
. The minimum version of clip-path
is much smaller than the resolution—in other words, it is not visible to the naked eye when applied. The other extreme value is slightly larger than the viewport. At this zoom level, there is no visible cropping, as all cropping occurs outside the visible area. Animating between these two different clip-path
will have interesting effects. The cropped shape appears to show what's behind it when scaled.
You may have noticed that this demo uses different shapes. In this case, I used the logo of the popular sneaker brand. This gives you an idea of ??how it works in a more realistic scenario.
Similarly, here is a video that explains the technical details in detail:
Creative Three: Crop overlay
Another idea is to create highlighting effects using clip-path
. For example, suppose we want to use clip-path
to create the active state of a menu.
The clipping path above extends between different menu options when animates. Additionally, we used an interesting shape to make the UI stand out.
This demo uses a modified copy of the same content, where the copy is located at the top of the existing content. It is in the exact same position as the menu unit and is used as the active state. Essentially, it looks like any other regular activity state of the menu. The difference is that it is created using clip-path
instead of using fancy CSS styles on HTML elements.
Use clip-path
to create some unusual effects here. The slanted shape is one aspect, but we also get the stretching effect. The menu has two independent crops—one on the left and one on the right—which allows the crop to be animated at different times using transition delays. The result is a very relaxed stretch animation. Since the default easing is nonlinear, delays can lead to a slight rubber band effect.
The second trick here is to apply delays according to direction. If the active state needs to move to the right, the right side needs to start the animation first and vice versa. I get direction awareness by using a little JavaScript to apply the correct class based on clicks.
Creative Four: Slice Slicing
How often do you see circularly expanding menus on the web? Ridiculous, right! ? Well, clip-path
not only makes it possible, it's quite simple.
The menus we usually see contain links arranged in single-line or even drop-down menus, just like the first trick we've seen before. What we do here is put these links in arcs instead of rectangles. Of course, using rectangles is the traditional way. The idea here is to explore more mobile-friendly interactions and keep two specific UX principles in mind:
- A clear goal, easy to click with your thumb
- Changes occur near focus – where your visual focus is
This demonstration is not specifically aimed at clip-path
. I just happened to use clip-path
to create the pen. Again, like the previous expandable menu demo, this is a handy question. Using clip-path
and 50% border radius, I immediately got the required arc.
Creative Five: Toggle Button
The toggle button always wows web developers like us. It seems like a new toggle button explanation will be introduced every week. OK, this is mine:
This demo is a remake of Dribbble screenshot by Oleg Frolov. It combines all three techniques described in this article. These are:
- Double cut
- Scaling clip path
- Crop overlays
All of these switches seem to have one thing in common. They consist of an oval background and a circle, similar to a real mechanical switch. The way this toggle button works is to enlarge the circular clip-path
inside the circular container. The container trims the content through overflow: hidden
, that is, double crops.
Another key part of the demo is the use of two alternating versions in the tag. They are original versions and their yin-yang inversion mirrored copies. Using two versions instead of one version is, taking the risk of repetition, a convenient problem. With both versions, we just need to create a transition for the first version. We can then repeat most of this for the second version. At the end of the transition, the toggle button switches to the opposite version. Since the inverted version is the same as the previous end state, changes cannot be found. The advantage of this technique is to reuse parts of the animation. The disadvantage is that there will be stutter when the animation is interrupted. This happens when the user presses the toggle button before the animation is complete.
Let's take a look behind the scenes again:
Conclusion
You might think: Exploration is one thing, but what about production? Can I use clip-path
on the website I am currently working on? Is it ready to go into production?
Well, there is no easy answer to this question. Among other things, there are two more issues that need to be carefully studied:
- Browser support
- performance
At the time of writing, about 93% of browsers support it according to caniuse. I think we are on the verge of mass adoption. Note that this number takes into account the WebKit prefix.
There is also the argument from IE, but that is really not an argument to me. I can't see what the extra effort for IE means. Should you create workarounds for unsafe browsers? Your users are better off using a modern browser. Of course, there are some rare situations that need to be considered legacy. But in these cases, you may not consider using modern CSS at all.
So what about performance? Well, as things get more and more, the performance gets tricky, but I won't say anything will stop us from using clip-path
today. Performance that is always measured is important. On average, clip-path
may have a greater performance impact than other CSS rules. But remember that the practice we are introducing here is advice, not law. Think of them as suggestions. Develop the habit of measuring performance.
Go ahead and cut your page into pieces. See what happens!
The above is the detailed content of Clipping, Clipping, and More Clipping!. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

There are three ways to create a CSS loading rotator: 1. Use the basic rotator of borders to achieve simple animation through HTML and CSS; 2. Use a custom rotator of multiple points to achieve the jump effect through different delay times; 3. Add a rotator in the button and switch classes through JavaScript to display the loading status. Each approach emphasizes the importance of design details such as color, size, accessibility and performance optimization to enhance the user experience.

To deal with CSS browser compatibility and prefix issues, you need to understand the differences in browser support and use vendor prefixes reasonably. 1. Understand common problems such as Flexbox and Grid support, position:sticky invalid, and animation performance is different; 2. Check CanIuse confirmation feature support status; 3. Correctly use -webkit-, -moz-, -ms-, -o- and other manufacturer prefixes; 4. It is recommended to use Autoprefixer to automatically add prefixes; 5. Install PostCSS and configure browserslist to specify the target browser; 6. Automatically handle compatibility during construction; 7. Modernizr detection features can be used for old projects; 8. No need to pursue consistency of all browsers,

Use the clip-path attribute of CSS to crop elements into custom shapes, such as triangles, circular notches, polygons, etc., without relying on pictures or SVGs. Its advantages include: 1. Supports a variety of basic shapes such as circle, ellipse, polygon, etc.; 2. Responsive adjustment and adaptable to mobile terminals; 3. Easy to animation, and can be combined with hover or JavaScript to achieve dynamic effects; 4. It does not affect the layout flow, and only crops the display area. Common usages are such as circular clip-path:circle (50pxatcenter) and triangle clip-path:polygon (50%0%, 100 0%, 0 0%). Notice

Themaindifferencesbetweendisplay:inline,block,andinline-blockinHTML/CSSarelayoutbehavior,spaceusage,andstylingcontrol.1.Inlineelementsflowwithtext,don’tstartonnewlines,ignorewidth/height,andonlyapplyhorizontalpadding/margins—idealforinlinetextstyling

Setting the style of links you have visited can improve the user experience, especially in content-intensive websites to help users navigate better. 1. Use CSS's: visited pseudo-class to define the style of the visited link, such as color changes; 2. Note that the browser only allows modification of some attributes due to privacy restrictions; 3. The color selection should be coordinated with the overall style to avoid abruptness; 4. The mobile terminal may not display this effect, and it is recommended to combine it with other visual prompts such as icon auxiliary logos.

TheCSSPaintingAPIenablesdynamicimagegenerationinCSSusingJavaScript.1.DeveloperscreateaPaintWorkletclasswithapaint()method.2.TheyregisteritviaregisterPaint().3.ThecustompaintfunctionisthenusedinCSSpropertieslikebackground-image.Thisallowsfordynamicvis

To create responsive images using CSS, it can be mainly achieved through the following methods: 1. Use max-width:100% and height:auto to allow the image to adapt to the container width while maintaining the proportion; 2. Use HTML's srcset and sizes attributes to intelligently load the image sources adapted to different screens; 3. Use object-fit and object-position to control image cropping and focus display. Together, these methods ensure that the images are presented clearly and beautifully on different devices.

Different browsers have differences in CSS parsing, resulting in inconsistent display effects, mainly including the default style difference, box model calculation method, Flexbox and Grid layout support level, and inconsistent behavior of certain CSS attributes. 1. The default style processing is inconsistent. The solution is to use CSSReset or Normalize.css to unify the initial style; 2. The box model calculation method of the old version of IE is different. It is recommended to use box-sizing:border-box in a unified manner; 3. Flexbox and Grid perform differently in edge cases or in old versions. More tests and use Autoprefixer; 4. Some CSS attribute behaviors are inconsistent. CanIuse must be consulted and downgraded.
