CSS-Dreieck-Generator

So wird es benutzt 3 Schritte
  1. Wählen Sie, wohin es Zeigt nach.
  2. Legen Sie Basisbreite, Höhe und Farbe fest.
  3. Drücken Sie Dreieck-CSS kopieren und fügen Sie es in Ihr Stylesheet ein.
Generierter CSS-Code

So funktioniert dieses Werkzeug

Der Kniff ist älter als die meisten Formwerkzeuge in CSS und immer noch der leichteste. Ein Element ohne Breite und Höhe zeichnet trotzdem seine vier Rahmen, und weil diese sich in Diagonalen treffen, ist jeder von ihnen ein Dreieck mit der Spitze im Mittelpunkt. Macht man drei transparent, bleibt der vierte allein stehen. Kein Bild, kein SVG, kein zusätzliches Element.

Welcher Rahmen die Farbe trägt, entscheidet die Richtung, und es ist das Gegenteil dessen, was die meisten zuerst vermuten: Ein nach oben zeigendes Dreieck entsteht, indem man den unteren Rahmen einfärbt. Basis und Höhe werden hier getrennt gesetzt, sodass auch ungleichseitige und hohe schmale Dreiecke erreichbar sind, während ein einzelner Größenwert immer nur ein rechtwinkliges oder gleichschenkliges ergibt.

Mit einer Form aus Rahmen arbeiten

  • Die Farbe liegt der Spitze gegenüber: Ein nach oben zeigendes Dreieck färbt den unteren Rahmen. Ist das einmal klar, ergibt sich der Rest von selbst.
  • Die Basis ist doppelt so breit wie der eingestellte Wert: Jeder Seitenrahmen steuert die Hälfte bei, eine Basisbreite von 20 ergibt also ein 40 Pixel breites Dreieck. Die Anzeige nennt die fertige Breite und erspart das Rechnen.
  • Es lässt sich weder umranden noch mit einem Verlauf füllen: Die Form besteht aus Rahmen, es bleibt also keiner übrig, um eine Kontur zu ziehen. Wo eines von beidem gebraucht wird, führen clip-path oder ein eingebettetes SVG hin.
  • Zwei überlagerte Dreiecke ergeben ein umrandetes: Ein etwas größeres in der Konturfarbe hinter einem kleineren in der Füllfarbe ist der übliche Weg, dieser Form einen Rahmen vorzutäuschen.

Why an element with no size still draws something

The trick is older than most of CSS's shape tools and still the lightest way to draw an arrow.

An element with no width and no height still draws its four borders. Because those borders meet at diagonals, each one is not a rectangle but a wedge, with its point at the element's centre. Make three of them transparent and the fourth is left standing alone: a triangle.

No image, no SVG, no extra markup. The [msm_tool slug="css-triangle"] writes the border widths and colours for each direction, but the mechanism is worth understanding because it explains every quirk that follows.

The colour goes on the opposite side to the point

This is the part that catches everyone, and it follows directly from the wedges.

A triangle pointing up is drawn by colouring the bottom border. The bottom border's wedge has its point at the top and its base along the bottom edge, so that is the shape left behind when the others go transparent.

Once that clicks, the rest of the property follows without needing to be memorised.

The base is twice the value you set

The left and right borders each contribute half of an upward triangle's base. Set both to 20 pixels and the finished triangle is 40 pixels wide.

That doubling is the second most common source of confusion, and it is why the generator reports the finished width alongside the value rather than leaving you to do the arithmetic.

Setting the base and the height independently is what allows scalene and tall narrow triangles. A single size value only ever gives a right-angled or isosceles one.

It cannot take a border or a gradient

The shape is made of borders, so there is nothing left over to draw an outline with. There is no fill to apply a gradient to either — the triangle is a solid colour by construction.

Two workarounds:

  • For an outline, stack two triangles: a slightly larger one in the outline colour behind a smaller one in the fill colour, offset by a pixel or two. Fiddly but reliable.
  • For a shadow, use filter: drop-shadow(), which follows the visible shape rather than the element's rectangle. box-shadow would draw a shadow around the invisible box.

If you need either of those regularly, a clip-path or an inline SVG is the less painful route.

Where it is still the right choice

Tooltip and popover arrows, above all. Place the triangle on a pseudo-element positioned against the container's edge, with its colour matching the container's background, and it reads as part of the shape.

Also: dropdown carets, breadcrumb separators, the notch on a speech bubble, and the small indicator on an accordion. Anything where you want a simple arrow with no extra element and no request.

Triangle or clip-path

The border trick for simple arrows — it is smaller, supported everywhere without exception, and needs no vendor prefix. A clip-path when the shape is more complex, needs its own outline, or has to animate.

The border version cannot animate its shape smoothly, because transitioning border widths produces a stepped result rather than a morph.

A note on the markup

Triangles built this way are decoration, and they belong on a pseudo-element rather than a real one. An empty <div> whose only purpose is to be a shape is an element a screen reader has to step over for no reason.

::before or ::after on the element that owns the arrow keeps the markup clean and keeps the decoration out of the accessibility tree entirely.

Häufig gestellte Fragen

Speziell zum CSS-Dreieck-Generator. Allgemeinere Fragen zu dieser Kategorie werden auf der Kategorieseite beantwortet.

Weil jeder Rahmen ein Keil ist, der sich im Mittelpunkt des Elements trifft. Der Keil des unteren Rahmens hat seine Spitze oben, und genau die bleibt übrig, wenn die anderen transparent werden.
Doppelt so breit wie der Basiswert, da linker und rechter Rahmen je die Hälfte beisteuern. Die Anzeige nennt die fertige Breite.
Nicht direkt — die Form besteht aus Rahmen, es bleibt also keiner übrig. Ein Filter mit drop-shadow folgt der sichtbaren Form und ist der übliche Behelf.
Dies für einen einfachen Pfeil oder eine Tooltip-Spitze, wo es kleiner ist und überall funktioniert. Einen clip-path, wenn die Form komplexer ist oder eine eigene Kontur braucht.
Setzen Sie das Dreieck auf ein Pseudoelement, das an der Kante des Tooltips positioniert ist, und gleichen Sie die Farbe an dessen Hintergrund an.

Abonniere Benachrichtigungen zu neuen Tools, Funktionen und Produktivitätstipps.