CSS Triangle Generator
Create pure CSS triangles with visual controls. No images needed.
๐ 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.