Free tool · Developer tools
CSS triangle generator
A triangle 100 px wide and 60 px high, pointing up, needs three border lines and a box of zero size. The page writes that CSS for you, shows it live, and gives the area (3000 px²) and the angles.
- Free
- No sign-up
- Runs in your browser
.triangle {
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 60px solid #1f6feb;
}- Area
- 3000 px²
- Box size
- 100 × 60 px
- Slanted sides
- 78.1 px and 78.1 px
- Angles
- 79.61°, 50.19°, 50.19°
How to use the CSS Triangle Generator
- Pick a Direction: four pointing arrows or four right angle corners.
- Type the Width and Height in pixels and a Color as a hex code.
- Choose Borders or clip-path as the method.
- Check the preview, press Copy the CSS and paste it into your stylesheet.
How do you make a triangle in CSS?
The border trick starts from a box with width 0 and height 0. Its thick borders then meet along diagonal lines, so each border is a triangle by itself. Give one border a color and make the neighbors transparent, and a single triangle is left. For an upward triangle of 100 by 60 px, the colored border is the bottom one with the height of the triangle, 60 px. The left and right borders are transparent and each is half the width, 50 px, because together they make up the base. The result is:
.triangle { width: 0; height: 0; border-left: 50px solid transparent; border-right: 50px solid transparent; border-bottom: 60px solid #1f6feb; }
A width that is odd gives a half pixel: at 25 px wide the side borders become 12.5px, which browsers draw with anti-aliasing. If the edge looks soft, use an even width.
| Direction | Border lines |
|---|---|
| Pointing up | border-left: 50px solid transparent; border-right: 50px solid transparent; border-bottom: 60px solid #1f6feb |
| Pointing down | border-left: 50px solid transparent; border-right: 50px solid transparent; border-top: 60px solid #1f6feb |
| Pointing left | border-top: 30px solid transparent; border-bottom: 30px solid transparent; border-right: 100px solid #1f6feb |
| Pointing right | border-top: 30px solid transparent; border-bottom: 30px solid transparent; border-left: 100px solid #1f6feb |
| Corner at top left | border-top: 60px solid #1f6feb; border-right: 100px solid transparent |
| Corner at top right | border-top: 60px solid #1f6feb; border-left: 100px solid transparent |
| Corner at bottom left | border-bottom: 60px solid #1f6feb; border-right: 100px solid transparent |
| Corner at bottom right | border-bottom: 60px solid #1f6feb; border-left: 100px solid transparent |
Should I use borders or clip-path?
The border method needs no extra property and works on an empty element, which is why it has long been used for tooltip arrows and dropdown pointers. A box with only borders cannot hold text, and its color and size are tied to the border values.
The clip-path method keeps a normal box of 100 by 60 px and cuts it to a triangle with polygon(). You can give it a gradient background, put an image in it or animate it. For the same triangle the page writes: .triangle { width: 100px; height: 60px; background: #1f6feb; clip-path: polygon(50% 0, 0 100%, 100% 100%); } Both methods make the same shape, so choose by what else the element has to do. Use the switch to see both outputs for one size.
How are the area and the angles of the triangle worked out?
Width and height describe the box around the triangle. The area is half of the box, 100 times 60 divided by 2, which is 3000 px². For a pointing triangle the two slanted sides are equal. Each has a length of the square root of (50 squared plus 60 squared), which is 78.1 px, and the tip angle is 79.61°. The two base angles are 50.19° each.
A corner triangle has a right angle. At 100 by 60 px the long side is 116.62 px and the other two angles are 30.96° and 59.04°. The table shows pointing triangles at a few sizes.
| Width × height (px) | Area (px²) | Slanted side (px) | Tip angle |
|---|---|---|---|
| 20 × 10 | 100 | 14.14 | 90° |
| 40 × 20 | 400 | 28.28 | 90° |
| 60 × 30 | 900 | 42.43 | 90° |
| 100 × 60 | 3000 | 78.1 | 79.61° |
| 200 × 100 | 10000 | 141.42 | 90° |
For other shapes use the CSS clip-path generator or the box shadow generator, and for pixel to rem sizes the px to rem converter.
How do I attach a triangle to a box, like a speech bubble tip?
Put the triangle on a pseudo-element of the box. Give the box position: relative, then give ::after an empty content string, position: absolute and the triangle borders. A tip of 16 by 8 px pointing up uses border-left: 8px solid transparent, border-right: 8px solid transparent, border-bottom: 8px solid #1f6feb.
To sit on the top edge of the box, set bottom: 100% and a left offset. To center it, set left: 50% and pull it back by half its own width, which is 8 px here, with a negative left margin. Use the preview on this page to check the size before you place it.
Use it with
- Box Shadow Generator: visual CSS box-shadow builder.
- HEX to RGB Converter: HEX, RGB, HSL, CMYK conversions.
- CSS Grid Generator: build a CSS grid layout and copy the code.
Questions
How do I make a CSS triangle pointing up?
Use a box of width 0 and height 0, transparent left and right borders of half the width, and a colored bottom border of the full height. For 100 by 60 px that is 50 px sides and a 60 px bottom.
How do I make a right angle triangle in CSS?
Color one border and make the border on the side next to it transparent. For a corner at the top left, the top border is colored and the right border is transparent.
Can I put text inside a CSS triangle?
Not with the border method, because the box has no size. With clip-path the box keeps its size, so text and backgrounds work.
Why does my triangle look blurry?
An odd width gives a half pixel in the side borders, which anti-aliasing smooths. An even width draws crisp edges.
How do I change the triangle color?
Type another hex code in Color. The page accepts three digit codes such as #f80 and writes them in full.
Does the page upload anything?
No. The CSS is built in your browser from the numbers you type.
The tool's results are computed in your browser by the code on this page; the explanations are checked against the tool's own output.