CSS Triangle Generator

Generate a pure CSS triangle in any of eight directions from a width, a height and a colour, using the border technique, with the right-angled variants too.

Loading tool…

The CSS is generated by JavaScript in your browser and never leaves the page. There is nothing to upload and nothing is stored.

What the CSS Triangle Generator does

This CSS triangle generator will generate a pure CSS triangle in any of eight directions from a width, a height and a colour, using the border technique, with the right-angled variants too. Everything runs inside this page, so the work happens on your own device rather than on a server. It is most often used for the pointer on a tooltip, popover or dropdown menu. Next to this one sits our CSS gradient generator.

How to use it

  1. Choose which way the triangle should point.
  2. Set the width and height — they are independent, so the triangle need not be equilateral.
  3. Pick the colour.
  4. Copy the rule. The element has no width or height: it is made entirely of borders.

How it works

The technique relies on how browsers join borders. Where two borders of different colours meet at a corner, the join is mitred — cut diagonally — rather than squared off. Give an element zero width and zero height and three thick borders, and you get four triangles meeting at a point. Make two of them transparent and only one triangle is left. That is the whole trick, and it has worked in every browser since the technique was first written up.

The geometry follows from that. For a triangle pointing up, the coloured border is the bottom one and its width is the triangle’s height; the left and right borders are transparent and each is half the triangle’s base, because together they span the full width. So a 120px by 70px triangle needs 60px transparent side borders and a 70px coloured bottom border. Width and height are independent, which is why you can make a shallow caret or a tall spike from the same rule.

A right-angled triangle is simpler still and needs only two borders. One coloured, one transparent, meeting at the corner you want the right angle on: the diagonal is simply the join between them. That makes it the cheaper shape when you want a corner flag or a folded-corner effect, and it is why the eight-direction menu here includes the four diagonal variants.

There are two things the border triangle cannot do, and knowing them saves an hour. It cannot have a border of its own, because the triangle is already made of borders — an outlined triangle needs a second, slightly larger triangle behind it, offset by a pixel or two. And it cannot have a border radius on the point, because there is no corner there to round; the apex is a mitre join, not a corner. For either of those, or for a shape that needs to be filled with a gradient, clip-path: polygon(50% 0, 100% 100%, 0 100%) is the better tool, and an inline SVG better still if the shape has to scale.

Where a triangle is almost always being used is as a pointer on a tooltip or a dropdown, and there the right place for it is a ::before or ::after pseudo-element on the bubble rather than an extra empty div in the markup. The generated rule drops straight into one — content: ""; position: absolute; and then these border declarations.

When to use it

  • The pointer on a tooltip, popover or dropdown menu.
  • A caret or chevron for a disclosure control, with no icon font or SVG.
  • A folded-corner or corner-flag effect, using the right-angled variants.
  • A speech-bubble tail.
  • Any small arrow where loading an image would be more weight than the shape deserves.

Common mistakes to avoid

  • Giving the element a width or height. It must be zero for the borders to meet at a point.
  • Making the side borders the full width instead of half of it, which doubles the base.
  • Trying to add a border to the triangle. It is already made of borders; use a second triangle behind it.
  • Expecting border-radius to round the apex. There is no corner there to round.
  • Adding an empty div for a tooltip arrow instead of using a pseudo-element.

Frequently asked questions

How does the CSS triangle trick work?

Borders are mitred where they meet, so an element with zero size and three thick borders shows four triangles meeting at a point. Making two of them transparent leaves one visible triangle. The element itself has no width or height at all.

Why is each side border half the width?

Because the two side borders together form the base. For a triangle pointing up with a 120px base, the left and right transparent borders are 60px each, and the coloured bottom border is the triangle’s height.

Can I put a border or a shadow on a CSS triangle?

Not directly — the shape is made of borders, so there is nothing left to add one to. For an outline, stack a slightly larger triangle in the outline colour behind it. filter: drop-shadow does work on a border triangle, because it follows the painted shape rather than the box.

Can I round the point of the triangle?

No. The apex is a mitre join between two borders, not a corner, so border-radius has nothing to act on. If you need a rounded point, use clip-path or an inline SVG instead.

Should I use clip-path instead?

For anything beyond a flat-coloured triangle, yes. clip-path: polygon(50% 0, 100% 100%, 0 100%) gives a real triangular element that can hold a gradient, an image or text, and it can be animated. The border trick remains the lightest option for a small solid arrow, and it works everywhere.

How do I add a triangle to a tooltip?

Put it on a pseudo-element of the tooltip: content: ""; position: absolute; then the border declarations, then top or bottom and left to place it. That keeps the arrow out of your markup entirely.

Related tools

See all developer tools →