Xerobit

CSS Triangle Generator: Border and Clip-Path Triangles

This triangle css generator produces ready-to-use CSS for directional triangles using either the classic border hack or the modern clip-path property. Choose a direction, set the width, height and color, then copy the generated code. Everything runs in your browser without uploading any data.

Result

.triangle {
  width: 0;
  height: 0;
  border-left: 20px solid transparent;
  border-right: 20px solid transparent;
  border-bottom: 40px solid #333333;
}
  • Method
    Border Hack
  • Direction
    Up
  • Dimensions
    40px × 40px
  • Color
    #333333

How to use the css triangle generator

  1. Select the direction your triangle should point from the dropdown menu.
  2. Enter the width and height in pixels to control the proportions.
  3. Type a valid CSS color value such as a hex code or named color.
  4. Choose between the border hack or clip-path method depending on your browser support needs.
  5. Copy the generated CSS from the output area and paste it into your stylesheet.

How the CSS border triangle works

The border technique relies on how browsers render the corners of an element's borders. When an element has zero width and zero height but thick borders, each border forms a trapezoid that meets at the center. By making three borders transparent and one colored, you isolate a single triangle.

For example, to create an upward-pointing triangle 40 pixels wide and 40 pixels tall, the left and right borders are each half the total width (20px), and the bottom border equals the full height (40px). The top border is set to zero. The resulting CSS looks like this:

width: 0; height: 0; border-left: 20px solid transparent; border-right: 20px solid transparent; border-bottom: 40px solid #333333;

This triangle css generator calculates those values automatically so you do not have to work out the fractions yourself.

The clip-path alternative

Modern browsers support the clip-path property, which clips an element to a polygon shape. Unlike the border trick, the element retains its actual width and height, making it easier to position within flexbox or grid layouts.

A downward-pointing triangle uses three coordinate pairs: polygon(0 0, 100% 0, 50% 100%). The first two points form the top edge, and the third point sits at the bottom center. Because percentages are relative to the element's own box, you can resize the triangle simply by changing the element's width and height properties.

If you need to animate the shape or apply a background gradient, clip-path is usually the better choice. For maximum compatibility with older browsers, stick with the border method. You can also explore the dedicated CSS clip-path generator for more complex polygon shapes.

Common reference table

The following table shows the border values produced by this triangle generator css tool for a 40 by 40 pixel triangle in each cardinal direction.

DirectionColored borderTransparent bordersZero border
Upbottom: 40pxleft: 20px, right: 20pxtop: 0
Downtop: 40pxleft: 20px, right: 20pxbottom: 0
Leftright: 40pxtop: 20px, bottom: 20pxleft: 0
Rightleft: 40pxtop: 20px, bottom: 20pxright: 0

Corner directions use only two borders. An up-right triangle, for instance, sets the top border to the full height and the left border to the full width, leaves the other two at zero, and puts the right angle in the top-right corner.

Practical use cases

CSS triangles appear frequently in user interface design. Dropdown menus use a small downward caret to indicate expandable content. Tooltips rely on a triangular pointer to connect the popup to its trigger element. Breadcrumb separators sometimes use right-pointing triangles instead of slashes.

Because these shapes are pure CSS, they scale perfectly on high-resolution screens and require no extra HTTP requests. When combined with pseudo-elements like ::before or ::after, you can attach a triangle to any element without adding extra markup to your HTML.

After generating your triangle code, you may want to run it through a css formatter to keep your stylesheets consistent, especially when integrating multiple generated snippets.

Mistakes to avoid

One frequent error is forgetting to set the element's width and height to zero when using the border method. If the element has any intrinsic size, the triangle will distort because the borders meet at the edges of the content box rather than at a single point.

Another issue arises with odd-numbered widths. A 41-pixel-wide triangle requires left and right borders of 20.5 pixels. While browsers handle sub-pixel values, rounding differences can cause a faint gap or overlap on certain screens. Sticking to even numbers avoids this problem entirely.

Finally, ensure the transparent borders actually use the keyword transparent rather than matching the background color. Hard-coding a background color breaks the illusion if the triangle moves over a gradient or image.

Worked examples

ExampleInputResult
Small up arrowDirection: up; Width (px): 20; Height (px): 20; Color: #000000; Method: borderMethod: Border Hack; Direction: Up; Dimensions: 20px × 20px; Color: #000000
Wide down caretDirection: down; Width (px): 60; Height (px): 30; Color: #ff0000; Method: borderMethod: Border Hack; Direction: Down; Dimensions: 60px × 30px; Color: #ff0000
Right pointer via clip-pathDirection: right; Width (px): 30; Height (px): 50; Color: #0066cc; Method: clip-pathMethod: Clip-Path; Direction: Right; Dimensions: 30px × 50px; Color: #0066cc
Corner triangleDirection: up-right; Width (px): 80; Height (px): 80; Color: #22c55e; Method: clip-pathMethod: Clip-Path; Direction: Up-right; Dimensions: 80px × 80px; Color: #22c55e

FAQ

What is a triangle css generator?

A triangle css generator is a tool that writes the CSS needed to display a triangle shape using pure styling. It typically outputs either the border hack or a clip-path polygon so developers can copy the code directly into their projects.

Which method should I choose: border or clip-path?

Use the border method if you need to support very old browsers. Use clip-path if you want the element to maintain real dimensions, which makes layout with flexbox or grid simpler and allows background images or gradients inside the triangle.

Can I change the triangle color dynamically?

Yes. Replace the hard-coded color value with a CSS custom property such as var(--arrow-color). This lets you update the color from JavaScript or a parent class without regenerating the CSS.

Why does my border triangle look blurry?

Blurry edges often occur when the width or height results in fractional pixel values for the borders. Use even numbers for the base dimensions so the half-width calculations produce whole pixels.

Do I need to upload anything to use this css triangle generator?

No. The tool runs entirely in your browser. Your dimensions and colors never leave your device, and no files are uploaded to any server.

How do I add a triangle to a tooltip?

Apply the generated CSS to a ::before or ::after pseudo-element on the tooltip container. Position it absolutely at the edge where you want the pointer to appear.