CSS
The CSS clip-path
property defines which parts of an element remain visible while hiding the rest. Developers use it to create unique shapes, mask images, and animate UI elements without modifying HTML structure. This property allows for creative layouts and interactive designs.
The clip-path
property works by setting a shape that clips an element. You can use basic shapes like circles, polygons, and ellipses, or define custom clipping paths with SVG. Here’s the general syntax:
element {
clip-path: shape-type(arguments);
}
.circle-clip {
width: 200px;
height: 200px;
background: blue;
clip-path: circle(50%);
}
This code trims the element into a perfect circle, keeping only the area inside the shape visible.
You can define complex clipping shapes using the polygon()
function.
.triangle-clip {
width: 150px;
height: 150px;
background: red;
clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
}
This example creates a triangle by specifying three points in a percentage-based coordinate system.
Standard HTML elements default to a rectangular shape. The clip-path
property allows designers to break this convention by shaping elements into circles, hexagons, and other forms.
.hexagon-clip {
width: 250px;
height: 250px;
background: purple;
clip-path: polygon(25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%, 0% 50%);
}
This technique is useful for avatars, buttons, or section dividers.
Using CSS animations, you can transition between different clip-path shapes to create engaging effects.
@keyframes reveal {
from {
clip-path: circle(0%);
}
to {
clip-path: circle(50%);
}
}
.animated-clip {
width: 200px;
height: 200px;
background: green;
animation: reveal 2s ease-in-out forwards;
}
This animation smoothly expands the clipped area, progressively revealing the content.
You can apply clip-path
to images to shape them dynamically without modifying the original file.
.image-clip {
width: 300px;
height: 200px;
background: url('image.jpg') center/cover;
clip-path: ellipse(50% 40%);
}
This approach provides a flexible alternative to cropping images in Photoshop or other graphic design tools.
By applying clip-path to section dividers, you can create dynamic page transitions.
.section-divider {
width: 100%;
height: 100px;
background: orange;
clip-path: polygon(0 100%, 100% 0%, 100% 100%);
}
Instead of a conventional rectangular border, you can clip parts of it to create unique edge effects.
.border-clip {
width: 200px;
height: 100px;
background: lightblue;
border: 5px solid black;
clip-path: inset(10px round 20px);
}
This example trims the border and rounds the clipped corners.
You can change an element’s shape when a user hovers over it.
.hover-clip {
width: 200px;
height: 100px;
background: orange;
transition: clip-path 0.3s ease;
}
.hover-clip:hover {
clip-path: polygon(10% 0%, 90% 0%, 100% 50%, 90% 100%, 10% 100%, 0% 50%);
}
This hover effect creates an engaging UI experience.
Clip-path values use a coordinate system where (0 0)
represents the top-left corner and (100% 100%)
represents the bottom-right. Using percentages ensures responsiveness across different screen sizes.
.responsive-clip {
clip-path: polygon(10% 10%, 90% 10%, 90% 90%, 10% 90%);
}
The clip-path-origin
property determines whether the clipping path aligns with the content box, padding box, or border box.
.origin-clip {
clip-path: circle(40%);
clip-path-origin: content-box;
}
You can reference an SVG clipping path inside a CSS file.
svg {
width: 200px;
height: 200px;
clip-path: url(#custom-shape);
}
This method allows for intricate shapes and masks.
Complex clipping paths can slow down rendering, especially in animations. Use simpler shapes and hardware-accelerated transitions whenever possible to maintain smooth performance.
The clip-path
property provides a powerful way to manipulate element visibility, enabling innovative designs without extra HTML elements. Experiment with different values to achieve visually engaging effects.
Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.