๐Ÿ”บ

CSS Triangle Generator

Create pure CSS triangles with visual controls. No images needed.

Generated CSS

                    
โšก Quick Presets

๐Ÿ“– Complete Guide to CSS Triangles and the Border Trick

CSS triangles represent one of the most elegant and widely-used techniques in web developmentโ€”creating geometric shapes using only CSS, without any images. This technique exploits the way browsers render borders at corners, transforming what might seem like a limitation into a powerful tool for creating arrows, pointers, decorative elements, and interactive UI components.

Understanding the Border Trick

The CSS triangle technique leverages a behavior that many developers overlook: when multiple borders meet at the corner of an element, they create diagonal lines where they join. If you make an element with zero width and zero height, only the borders remain visible. By making some borders transparent and others colored, you can isolate triangular portions of those borders.

To visualize this, imagine an element with thick borders of different colors on each side. At each corner, the borders meet at 45-degree angles. When you collapse the element's content area to nothing (width: 0; height: 0), only these triangular border sections remain. By hiding three of the four borders with transparent color, you reveal a single triangle.

The Basic Triangle Formula

Creating a basic triangle pointing downward requires setting width and height to zero, making the left and right borders transparent with equal widths (which determines the triangle's base), and setting the top border to your desired color and height. The top border's width becomes the triangle's height, while the combined widths of the left and right transparent borders become its base width.

For a triangle pointing upward, you reverse this: transparent left and right borders, with a colored bottom border. For left-pointing triangles, use transparent top and bottom borders with a colored right border. For right-pointing, use transparent top and bottom with a colored left border.

Controlling Triangle Dimensions

Understanding the relationship between border widths and triangle dimensions is essential for precise control. For up and down pointing triangles, the triangle's width equals the sum of the left and right border widths. The triangle's height equals the opposite border's width (top border for down-pointing, bottom for up-pointing).

For left and right pointing triangles, the triangle's height equals the sum of the top and bottom border widths. The triangle's width equals the opposite border's width (right border for left-pointing, left for right-pointing).

For equilateral triangles where all sides are equal, you need to calculate border widths mathematically. For an equilateral triangle with a given side length, the perpendicular borders should be half the side length, and the colored border should be approximately 0.866 times the side length (half the square root of 3).

Creating Corner Triangles

Corner triangles (top-left, top-right, bottom-left, bottom-right) require a slightly different approach. Instead of using three transparent borders and one colored border, corner triangles use two borders: one colored and one transparent, both adjacent to the desired corner.

For a top-left corner triangle, use a colored top border and a transparent right border. The triangle fills the space where these borders would have met at a diagonal. For a bottom-right corner triangle, use a colored bottom border and a transparent left border. The other combinations follow the same pattern.

Corner triangles are particularly useful for decorative ribbon effects, corner badges, and folded paper simulations.

Common Use Cases for CSS Triangles

Tooltip Arrows

One of the most common applications of CSS triangles is creating arrows for tooltips and popovers. A small triangle positioned at the edge of a tooltip creates a visual connection between the tooltip and its target element, guiding the user's eye to the relationship between the two.

Tooltip arrows are typically created using pseudo-elements (::before or ::after) positioned absolutely relative to the tooltip container. This keeps the HTML clean while adding the visual indicator through CSS alone. The arrow's color should match the tooltip background, and its position can be adjusted to point in any direction depending on where the tooltip appears relative to its trigger.

Dropdown Indicators

Small triangles commonly indicate dropdown menus or collapsible sections. These indicators are typically placed beside text labels, pointing downward to suggest that clicking will reveal content below. When the dropdown opens, the triangle often rotates to point upward, providing immediate visual feedback about the state change.

For accessible dropdown indicators, ensure the triangle serves only as a visual enhancement, not as the primary means of conveying state. Use appropriate ARIA attributes to communicate the expanded or collapsed state to assistive technologies.

Breadcrumb Separators

In breadcrumb navigation, right-pointing triangles serve as separators between items, suggesting progression and hierarchy. Unlike slash or chevron characters, CSS triangles can be precisely sized and colored to match your design system without depending on font rendering.

For breadcrumb triangles, smaller sizes (8-12px width) typically work best. The triangle color might match the text color or use a muted shade to avoid overpowering the navigation labels themselves.

Play Buttons

Video players, audio interfaces, and slide shows commonly use right-pointing triangles to indicate "play" functionality. CSS triangles create crisp, scalable play icons that never pixelate and can be easily colored to match any design theme.

For play buttons within circles, the triangle should be slightly offset from center toward the right. This optical adjustment compensates for the visual weight distribution and makes the button appear balanced.

Corner Ribbons and Badges

Corner triangles excel at creating ribbon effectsโ€”those folded banners that appear to wrap around a container's corner. By combining a colored corner triangle with a positioned pseudo-element containing text, you can create attention-grabbing badges for "Sale," "New," or other promotional indicators.

Styling and Enhancing CSS Triangles

Adding Borders to Triangles

Since CSS triangles are made of borders themselves, adding an outline to a triangle requires a layering technique. Create two triangles: a larger one in the border color, and a smaller one in the fill color positioned on top. The size difference creates the border effect.

This technique uses pseudo-elements to create both triangles, with careful z-index management to ensure proper layering. The larger background triangle might use the ::before element, while the smaller foreground triangle uses ::after.

Adding Shadows

The standard box-shadow property does not work well with CSS triangles because box-shadow follows the rectangular element boundaries, not the visible triangle shape. Instead, use the filter: drop-shadow() function, which respects the actual rendered shape including transparent regions.

A drop-shadow applied to a triangle produces a shadow that follows the triangular outline, creating convincing depth effects without the rectangular artifacts that box-shadow would create.

Gradients in Triangles

Applying gradients to CSS triangles is challenging because border colors do not accept gradients. However, you can achieve gradient effects by using multiple overlapping triangles with different solid colors, creating a stepped gradient approximation. For smooth gradients, consider using clip-path as an alternative to the border technique.

Alternatives and Modern Approaches

While the border trick remains widely useful, modern CSS offers alternatives for creating triangles. The clip-path property can create triangles that accept gradients, images, and more complex styling. SVG elements provide even more control and can be animated smoothly.

However, the border technique remains valuable for its simplicity, excellent browser support, and minimal markup requirements. For basic arrows and pointers, CSS triangles often provide the most efficient solution.

Browser Support and Reliability

The CSS border trick for triangles works in virtually all browsers, including older versions of Internet Explorer. It is one of the most reliably supported CSS techniques available, making it safe to use without fallbacks in almost any project.

The technique requires no vendor prefixes, no feature queries, and no polyfills. It simply works, making it an excellent choice when broad compatibility is important.

โ“ Frequently Asked Questions

CSS triangles offer several advantages. They require no additional HTTP requests, reducing page load time. They scale perfectly to any size without quality loss, unlike raster images. They can be colored dynamically with CSS, making them easy to adapt to themes, hover states, or dark mode. They work on all screen resolutions including high-DPI retina displays. And they add virtually no file size to your project. SVG is also excellent and offers more flexibility for complex shapes, but for simple triangles, the CSS border technique is often the most efficient solution.

Yes, the border technique for creating CSS triangles works in virtually all browsers, including very old versions. It works in Internet Explorer 6 and later, all versions of Chrome, Firefox, Safari, and Edge. No vendor prefixes are required, and no feature detection is necessary. This universal support makes CSS triangles one of the safest CSS techniques to use when browser compatibility is a concern.

Adding borders requires creating two overlapping triangles: a larger one in the border color and a smaller one in the fill color positioned on top. The size difference creates the appearance of a border. For shadows, use filter: drop-shadow() instead of box-shadow. The drop-shadow filter respects the actual visible shape rather than the element's rectangular bounding box, creating a shadow that properly follows the triangular outline. Box-shadow would create a rectangular shadow around the zero-sized element, which would not look correct.

For a mathematically equilateral triangle (all sides equal), the border widths need specific proportions. For a triangle with sides of length s, the perpendicular borders (left and right for up/down triangles) should each be s/2, and the colored border (which forms the base and determines height) should be approximately 0.866 ร— s (half the square root of 3 times s). For a 100px equilateral triangle pointing up: border-left: 50px solid transparent; border-right: 50px solid transparent; border-bottom: 86.6px solid #color. In practice, you can round these values for convenience when exact mathematical precision is not critical.

Border colors do not accept gradient values, so you cannot directly apply gradients to triangles created with the border technique. However, there are workarounds. You can stack multiple triangles of slightly different sizes with different solid colors to approximate a stepped gradient. For true smooth gradients, consider using alternative techniques: clip-path can create triangular shapes that accept background gradients, and SVG provides full gradient support with precise control. If you must use the border technique for compatibility reasons, the stepped color approach often produces acceptable results for subtle gradients.

Consider clip-path when you need gradients inside the triangle, when you need to clip content (like images) into a triangle shape, when you want to animate the shape itself, or when creating more complex polygons beyond simple triangles. The border technique remains preferable for simple arrow indicators, tooltips, dropdown markers, and situations requiring the broadest browser support. Clip-path has good modern browser support but does not work in older browsers, whereas the border technique works everywhere. For new projects targeting modern browsers, either approach is validโ€”choose based on your specific needs.

๐Ÿ“ข

Why Ads?

Ads help keep DC Tools free and running. Thank you for your support!

๐Ÿ›

Found a Bug?

Help us improve! Report issues and suggestions.

Report Bug