The CSS clip-path Property
1 April 2025 | Category: CSS
The clip-path
property in CSS allows you to define a clipping path to hide parts of an element, creating custom shapes. This is useful for applying non-rectangular shapes to elements like images, divs, and other HTML elements, allowing you to make creative and unique designs on your webpage.
With clip-path
, you can create various shapes such as circles, polygons, ellipses, and more, and apply them to elements to control the visible portion of the content.
🔹 1. Syntax of clip-path
The clip-path
property allows you to define the clipping region, specifying which part of the element should be visible. The basic syntax of the clip-path
property is:
element {
clip-path: shape;
}
Where shape
can be any of the following:
- A predefined shape (like
circle()
,ellipse()
, orpolygon()
). - A URL pointing to an SVG path (more advanced usage).
🔹 2. Common Shapes for clip-path
1. circle()
The circle()
function defines a circular clipping path. You can specify the circle’s radius and position (optional).
- Syntax:
clip-path: circle(radius at position);
radius
: Defines the size of the circle.position
: (Optional) Defines the position of the circle (by default, it is centered in the element).
Example:
img {
clip-path: circle(50% at center);
}
This creates a circular clipping path where the image is clipped into a circle, with the center as the clipping point.
2. ellipse()
The ellipse()
function defines an elliptical clipping path. Similar to circle()
, you can specify the radii for the width and height and their position.
- Syntax:
clip-path: ellipse(radiusX radiusY at position);
radiusX
: The radius of the ellipse along the X-axis.radiusY
: The radius of the ellipse along the Y-axis.position
: (Optional) Defines the position of the ellipse.
Example:
img {
clip-path: ellipse(50% 30% at center);
}
This clips the image into an elliptical shape, with different horizontal and vertical radii, centered in the element.
3. polygon()
The polygon()
function allows you to define custom shapes by specifying a series of points that make up the vertices of the polygon.
- Syntax:
clip-path: polygon(x1 y1, x2 y2, x3 y3, ...);
x
,y
: Coordinates for each point, relative to the element’s size (in percentage).
Example:
div {
clip-path: polygon(50% 0%, 100% 100%, 0% 100%);
}
This creates a triangular clipping path. The three points form a triangle with vertices at the top middle (50% 0%), bottom right (100% 100%), and bottom left (0% 100%).
4. inset()
The inset()
function creates a rectangular clipping path with optional rounding. It clips the element inwards by the specified amount of pixels or percentages from each edge.
- Syntax:
clip-path: inset(top right bottom left);
top
: Distance from the top edge.right
: Distance from the right edge.bottom
: Distance from the bottom edge.left
: Distance from the left edge.
Example:
div {
clip-path: inset(10% 20% 30% 40%);
}
This creates a rectangular clipping path, where the element is clipped inward by 10% from the top, 20% from the right, 30% from the bottom, and 40% from the left.
5. url()
You can also use a custom SVG path as the clipping path by referencing an external SVG file or an embedded SVG.
- Syntax:
clip-path: url(#id);
#id
: A reference to an SVG element that defines a path.
Example:
<svg width="0" height="0">
<defs>
<clipPath id="clip1">
<circle cx="50" cy="50" r="50" />
</clipPath>
</defs>
</svg>
<img src="image.jpg" style="clip-path: url(#clip1);" />
In this example, the image is clipped using the circular path defined in the embedded SVG with the id="clip1"
.
🔹 3. Transitioning with clip-path
You can use CSS transitions with the clip-path
property to create animated effects. For example, you can animate an element from a clipped shape to a fully visible shape on hover or any other interaction.
Example:
div {
width: 200px;
height: 200px;
background-color: blue;
clip-path: circle(0% at center);
transition: clip-path 0.5s ease;
}
div:hover {
clip-path: circle(50% at center);
}
In this example, when you hover over the div
, the clipping path expands from 0%
to 50%
, transitioning smoothly to reveal more of the content.
🔹 4. Browser Support and Performance Considerations
Browser Support:
clip-path
is supported by most modern browsers (Chrome, Firefox, Safari, Edge, and Opera). However, Internet Explorer does not support this property.
For older browsers, you might need to consider using fallback techniques or other approaches like SVG for clipping.
Performance Considerations:
While clip-path
is generally efficient for most use cases, complex shapes and animations (especially on large images or elements) may lead to performance issues. When applying multiple clipping paths or using intricate shapes, performance testing should be considered, especially for mobile devices.
🔹 5. Conclusion
The clip-path
property is a powerful tool for creating non-rectangular shapes and clipping images or other HTML elements in creative ways. Whether you’re creating circles, polygons, or custom shapes using SVG, clip-path
allows you to craft unique and engaging designs on your website.
Key Takeaways:
clip-path
enables custom clipping of elements into shapes like circles, ellipses, polygons, and more.- You can use CSS functions like
circle()
,ellipse()
,polygon()
, andinset()
for clipping. - Transitions with
clip-path
can create smooth animations for interactive effects. - Ensure compatibility with modern browsers, and be mindful of performance when applying complex clipping paths.
By incorporating clip-path
into your web design, you can create visually interesting effects and layouts without relying on images or complex graphic tools!