Keywords: CSS corner cut | pseudo-element | clip-path | CSS transform | linear gradient
Abstract: This article comprehensively explores various methods for implementing corner cut effects using pure CSS, with detailed analysis of pseudo-element border techniques, CSS clip-path, CSS transforms, and linear gradients. Through in-depth examination of CSS code implementations for each method, combined with browser compatibility and practical application requirements, it provides front-end developers with a complete guide to corner cut effects. The article also discusses the advantages and disadvantages of different approaches and looks forward to potential native CSS support for corner cuts in the future.
Overview of Corner Cut Effects
In web design, corner cut effects are a common visual design technique that creates unique visual appeal by cutting or folding a corner of an element. This design originally came from folded corner markers on paper documents and is now widely used in buttons, cards, dialogs, and other UI elements.
Pseudo-element Border Method
When the parent element has a solid color background, pseudo-elements combined with CSS border properties can be used to create corner cut effects. This method leverages the triangle-drawing characteristics of CSS borders, creating visual corner cuts by setting borders of different colors.
Core code for implementing top-left corner cut:
.cut-corner {
height: 300px;
background: #ff6b6b;
position: relative;
}
.cut-corner::before {
content: '';
position: absolute;
top: 0;
right: 0;
border-top: 80px solid #ffffff;
border-left: 80px solid #ff6b6b;
width: 0;
}
The working principle of this code: A ::before pseudo-element creates an absolutely positioned rectangle, then utilizes border characteristics. When both width and height of an element are 0, borders display as triangles. By setting border-top to white (same as background color) and border-left to red (same as parent element background color), it creates the visual effect of a cut top-left corner.
CSS Clip-Path Method
CSS clip-path property provides a more modern and flexible approach to corner cut implementation. It uses SVG paths or basic shapes to define the visible area of an element, supporting clipping of arbitrarily complex shapes.
Clip-path code for top-left corner cut:
.clip-corner {
width: 200px;
min-height: 200px;
-webkit-clip-path: polygon(0 0, 0 100%, 100% 100%, 100% 25%, 75% 0);
clip-path: polygon(0 0, 0 100%, 100% 100%, 100% 25%, 75% 0);
background: #74b9ff;
}
The polygon() function accepts a series of coordinate points, connecting these points in sequence to form the clipping area. In this example, five coordinate points correspond to: top-left starting point, bottom-left, bottom-right, right side point 25% from top, top point 75% from left, ultimately forming a shape with the top-left corner cut.
CSS Transform Method
Using CSS transforms enables more complex corner cut effects, particularly when transparent backgrounds or dynamic effects are needed. This method creates corner cuts by rotating and positioning pseudo-elements.
Implementation code for transparent corner cut effect:
.transform-corner {
position: relative;
width: 50%;
margin: 0 auto;
overflow: hidden;
padding: 20px;
}
.transform-corner::after {
content: '';
position: absolute;
width: 1100%;
height: 1100%;
top: 20px;
right: -500%;
background: rgba(255, 255, 255, 0.8);
transform-origin: 54% 0;
transform: rotate(45deg);
z-index: -1;
}
The key to this method lies in creating an enormous pseudo-element, then covering the area to be cut through rotation and precise positioning. Setting z-index: -1 ensures content displays above the cut corner, while overflow: hidden hides portions extending beyond the container.
Linear Gradient Method
For simple corner cut effects, CSS linear gradients can be used as background images. This approach requires no additional HTML elements or pseudo-elements.
Code example using gradients for corner cuts:
.gradient-corner {
height: 100px;
width: 200px;
background-image:
linear-gradient(to bottom left, transparent 50%, #e17055 50%),
linear-gradient(#e17055, #e17055),
linear-gradient(#e17055, #e17055);
background-size: 25px 25px, 100% 100%, 100% 100%;
background-position: 100% 0%, -25px 0%, 100% 25px;
background-repeat: no-repeat;
}
This method uses multiple overlapping gradients: the first gradient creates a 45-degree triangular cut corner, while the other two gradients fill the remaining areas. Through precise control of each gradient's size and position, the complete corner cut effect is formed.
CSS Mask Method
CSS mask property provides another approach to corner cut effects, particularly suitable for rounded corner cuts or complex shapes.
Creating corner cut masks with radial gradients:
.mask-corner {
mask: radial-gradient(40px at 40px 40px, #0000 98%, #000) -40px -40px;
}
This code creates a circular mask at the element's top-left corner, with radial gradient parameters controlling the cut corner's size and shape. This method is especially suitable for smooth rounded corner cut effects.
Method Comparison and Selection
Different corner cut implementation methods have their own advantages and disadvantages:
Pseudo-element Border Method: Good compatibility, simple implementation, but only suitable for solid color background scenarios.
CSS Clip-Path: High flexibility, supports arbitrary shapes, but requires consideration of browser compatibility, especially older versions.
CSS Transforms: Supports transparent backgrounds and complex animations, but implementation is relatively complex, requiring precise position and size calculations.
Linear Gradients: Concise implementation, no additional elements, but only suitable for simple corner cut effects with limited border support.
CSS Mask: Powerful functionality, supports complex shapes, but browser support is relatively new.
Future Outlook
The CSS specification once proposed the border-corner-shape property specifically for handling border corner shapes. Although this property is not yet widely supported, it represents the future development direction of CSS in shape processing. Developers should monitor relevant specification progress to use more native methods for corner cut effects in the future.
Best Practice Recommendations
When selecting corner cut implementation methods, consider the following factors:
1. Browser Compatibility Requirements: If the project needs to support older browser versions, the pseudo-element border method is the safest choice.
2. Design Complexity: For simple corner cuts, gradient or pseudo-element methods are sufficient; complex shapes recommend using clip-path.
3. Performance Considerations: Clip-path and mask perform well in modern browsers, but performance impact should still be considered when used extensively.
4. Maintainability: Code readability and maintainability are also important selection criteria.
By appropriately selecting implementation methods, developers can efficiently achieve desired corner cut effects in different scenarios while ensuring code quality and performance.