🎨 CSS

CSS Triangle Generator

Generate pure-CSS triangles in 8 directions using the classic border trick or modern clip-path polygon with instant CSS and Tailwind export.

Quick Presets:
Generated Declaration

About CSS Triangles

Quick Answer: The CSS Triangle Generator is a web utility for generating pure CSS triangles across all 8 cardinal and diagonal directions. It supports both the legacy zero-dimension border trick for universal backward compatibility and modern clip-path: polygon() vectors with independent width and height scaling and Tailwind CSS output.

Creating triangles in CSS without images or external icon fonts is a core technique in front-end development. The classic approach uses the browser box-model rendering engine: when adjacent borders meet on an element with zero width and height, the borders intersect along 45-degree diagonal miters. By giving one border a visible color while making adjacent borders transparent, a crisp geometric triangle appears. Alternatively, modern CSS provides clip-path, which cuts arbitrary polygon vertices out of standard block elements with support for background gradients.

Border Trick vs clip-path Technical Comparison

Technical Dimension Classic CSS Border Trick Modern clip-path: polygon()
Element Dimensions Must be width: 0; height: 0; Standard dimensions (width: 40px; height: 40px;)
Background Support Solid colors only (via border-color) Solid colors, gradients, and images
Shadow Support Requires filter: drop-shadow() Supports filter: drop-shadow()
Hover Hitbox Bounds cover full rectangular border box Hitbox clips cleanly to visible triangular area
Browser Support 100% (Universal since IE6) 98%+ (All modern Chromium, Safari, Firefox)

The Mechanics of the Classic CSS Border Trick: Box-Model Miter Geometry

To understand why the CSS border trick works, you must inspect how the browser layout engine constructs element borders. When an element possesses physical width and height, its borders appear as four rectangular frames encasing the central box. However, where two perpendicular borders meet at an exterior corner, the rendering engine cannot simply overlap them without visual bias. Instead, CSS specifications mandate that adjacent borders meet along a 45-degree diagonal seam known as a miter joint.

When you collapse the element content area completely by declaring width: 0; height: 0;, the central rectangle disappears. The four miter joints converge directly at the central geometric point of the zero-size box. The four borders now resemble four triangular pyramids meeting at their points. If you configure three of those border sides with transparent colors and assign a solid color to the remaining side, the transparent borders become invisible while maintaining their structural layout volume. The single colored border appears as a pristine, isolated geometric triangle.

Cardinal Triangles vs. Corner Diagonal Wedges

CSS triangles fall into two distinct structural categories depending on which borders you color and size:

Mathematical Foundations: Equilateral, Isosceles, and Scalene Dimensions

Most basic UI pointers (such as dropdown carets or navigation toggles) are isosceles triangles where the two sloping sides share equal length, but the base width and apex height can vary freely. However, when designing geometric badges or game indicators, an equilateral triangle (where all three sides and all three interior angles equal 60 degrees) is frequently desired.

Calculating the dimensions of an equilateral triangle requires trigonometry. If an equilateral triangle has a base width of w, its exact geometric height h is determined by the Pythagorean theorem:

h = (sqrt(3) / 2) * w, which approximates to h = 0.866025 * w.

In the CSS border trick, for an equilateral triangle pointing up with a total base width of 60px, each flanking transparent border (left and right) must be exactly 30px (w / 2), while the colored bottom border must have a thickness of approximately 52px (60 * 0.866). Declaring mismatched border thicknesses produces stretched isosceles or compressed blunt triangles.

The Modern Paradigm: Vector Clipping with clip-path: polygon()

While the CSS border trick has served front-end developers faithfully since the era of Internet Explorer 6, modern web development increasingly favors the native clip-path: polygon() property. Instead of relying on box-model miter geometry side effects, clip-path defines true vector coordinates directly across the element surface.

To produce an upward-pointing triangle using clip-path, you define three coordinate pairs corresponding to the apex and two bottom corners:

clip-path: polygon(50% 0%, 0% 100%, 100% 100%);

The advantages of the clip-path approach are substantial for contemporary UI design systems:

Visual Enhancements: Drop Shadows, Outlined Borders, and Pseudo-Elements

A frequent challenge when using CSS triangles in production is adding drop shadows or exterior borders, such as a white tooltip arrow outlined with a subtle 1px gray border and soft shadow. Traditional CSS properties fail when applied naively to border-trick elements:

Real-World UI Patterns: Tooltips, Carets, Ribbons, and Breadcrumbs

CSS triangles power several omnipresent user interface components across modern web applications:

Browser Engine Performance: CPU Rasterization vs. GPU Compositing

Performance characteristics differ dramatically between border tricks and clip-path polygons during state changes and micro-interactions. The border trick alters geometric box properties. Modifying border-width during an animation or hover state forces the browser layout engine into a complete Layout Recalculation (reflow), followed by CPU rasterization and texture upload.

In contrast, clip-path: polygon() coordinates are evaluated during the Compositing phase. Modern browser rendering engines (Chromium Blink, WebKit, Gecko) map polygon masks directly into GPU scissor and stencil buffers. Animating clip-path vertices via CSS transitions executes on the GPU thread at a silky 60 to 120 frames per second without burdening the main browser execution thread.

Subpixel Anti-Aliasing and Diagonal Rendering Fixes

Because CSS borders meet along diagonal seams, rendering engines must interpolate fractional pixels along the 45-degree angle. On low-density or legacy displays, this interpolation can occasionally produce faint jagged steps or a microscopic 1px transparent hairline gap between adjacent borders.

Front-end engineers resolve this subpixel rendering artifact with two proven techniques:

Frequently Asked Questions

How does the classic CSS border trick create a triangle without SVG?

By collapsing an HTML element to zero width and zero height and configuring thick borders, the boundaries where adjacent borders meet form diagonal miter angles. Making three border sides transparent leaves one isolated colored wedge that renders as a clean triangle.

Which border side should be colored to point the triangle in a specific direction?

The colored border forms the flat base of the triangle, causing the apex to point in the opposite direction. To create an upward-pointing triangle, color the border-bottom property and make border-left and border-right transparent.

How do corner diagonal triangles differ in border construction from cardinal triangles?

Cardinal triangles use one colored border and two transparent flanking borders. Corner diagonal triangles (such as Top-Right or Bottom-Left) combine two adjacent borders (one colored and one transparent), dividing the rectangular area along a single diagonal seam.

What are the advantages of modern clip-path polygon triangles over the border trick?

clip-path polygon works on non-zero dimensional containers, supports CSS background gradients, accommodates background images, allows true box-shadow and border-radius styling, and scales cleanly in responsive layouts using percentage units.

How can I export CSS triangles to Tailwind CSS utility classes?

Select the Tailwind output format to generate utility class combinations for border tricks (like w-0 h-0 border-l-[10px] border-l-transparent) or clip-path arbitrary classes like [clip-path:polygon(50%_0%,0%_100%,100%_100%)].

Can you add borders or rounded corners to a CSS triangle?

Border-trick triangles cannot accept standard border outlines or rounded corners because the shape itself is formed by border edges. To achieve a outlined or rounded triangle, use SVG or modern clip-path combined with CSS drop-shadow filters.

Why do CSS border triangles sometimes display jagged or blurry edges on mobile screens?

Diagonal miter joints in the CSS border box model do not always benefit from hardware-accelerated multisample antialiasing. Adding transform: rotate(0.1deg) or switching to clip-path forces GPU rasterization and eliminates stepping artifacts.

How do you position a CSS triangle as a tooltip pointer or speech bubble arrow?

Position the triangle absolutely relative to the tooltip container using pseudo-elements ::before or ::after. Anchor it with top: 100% and left: 50% combined with transform: translateX(-50%) for centered alignment.

How can you apply a drop shadow to a border-trick triangle?

Standard CSS box-shadow properties cast rectangular shadows around the zero-dimension bounding box. To cast a silhouette shadow around a border triangle, apply filter: drop-shadow(0 2px 4px rgba(0,0,0,0.15)) to the parent or pseudo-element.

Is the border trick or clip-path better for high-frequency CSS animations?

clip-path polygon is significantly better for animation because coordinate changes run on the GPU compositing layer. Animating border-width or border-color forces continuous layout recalculations and CPU paint cycles.