How to Create a CSS Triangle (Border Trick Explained)
CSS triangles are created by exploiting how borders meet at corners. When an element has zero width and height, its borders form triangles at each corner. By making three borders transparent and one colored, you get a single triangle pointing in any direction. It's the most elegant CSS hack that's survived into modern web development.
- Create CSS triangles using the border trick.
- The Border Trick Explained.
- Covers triangles in every direction.
- Covers practical uses.
- Covers modern alternatives.
The Border Trick Explained
When you set width and height to 0, an element becomes just its borders. Each border forms a triangle meeting at the center:
.triangle-down {
width: 0;
height: 0;
border-left: 20px solid transparent;
border-right: 20px solid transparent;
border-top: 20px solid #00FFD1;
}
The colored border (top) forms a downward-pointing triangle. The transparent side borders define the triangle's width. Change which border is colored to change direction.
Triangles in Every Direction
Point up: color border-bottom, transparent left+right. Point down: color border-top, transparent left+right. Point left: color border-right, transparent top+bottom. Point right: color border-left, transparent top+bottom. For diagonal triangles, color two adjacent borders and make the others transparent.
transform and opacity for smooth 60fps performance. These properties are handled by the GPU compositor and skip expensive layout recalculations.Practical Uses
Tooltip arrows (positioned with ::before or ::after pseudo-elements). Dropdown menu indicators. Breadcrumb separators. CSS-only accordion expand/collapse icons. Section divider shapes. The CSS Triangle Generator lets you set direction, size, and color visually, then copy the CSS.
width, height, top, or left. These trigger layout recalculations on every frame and can drop performance below 60fps.Modern Alternatives
CSS clip-path: polygon() creates triangles (and any other shape) without the border hack:
.triangle { clip-path: polygon(50% 0%, 0% 100%, 100% 100%); }
This is more readable and supports background images, gradients, and hover effects. Use clip-path for decorative shapes; use the border trick for small indicators like tooltip arrows where simplicity matters.
Frequently Asked Questions
How do I create a triangle in CSS?
Can I create an equilateral triangle in CSS?
Is clip-path better than the border trick?
Use the CSS Triangle Generator — free, no signup required.
⚡ Open CSS Triangle Generator