← Back to HTML & CSS, page 8

html-css-0152 · Easy fresher

How do you draw a triangle or a tooltip arrow with CSS only?

For the border method, use a zero-size element with transparent side borders and one coloured border. With clip-path, cut a box with polygon(). A tooltip arrow is a positioned pseudo-element with the same colour as the tooltip.

Full answer

There are two simple ways.

The border method uses the way borders meet at a corner. Borders are cut diagonally where they join. If an element has width: 0 and height: 0, only the borders are left, and each border is a triangle. Make the sides next to the point transparent and give one side a colour:

css
.arrow-up { width: 0; height: 0; border-left: 8px solid transparent; border-right: 8px solid transparent; border-bottom: 8px solid #222; }

The coloured border is the side the point faces away from: a coloured border-bottom makes a triangle that points up.

The clip-path method cuts a normal box to a triangle: clip-path: polygon(50% 0, 100% 100%, 0 100%). It is easier to read, easy to size with width and height, and the shape can have a background, a gradient or a size in percentages. The cut-away parts are not clickable and a shadow is clipped.

A tooltip arrow is usually a pseudo-element on the tooltip. Make it position: absolute, place it on an edge of the tooltip, for example bottom: 100%, and left: 12px. Use the same colour as the tooltip background.

A rotated square is a third way: a small box with transform: rotate(45deg) and a background, half hidden behind the tooltip. It also allows a border on the arrow.

Use CSS shapes for simple pointers. For anything complex, use an SVG, which is easier to adjust and to keep sharp.

The tooltip has a small upward arrow at its top edge, 12px from the left. The red box is cut to a triangle with its point at the top centre.

Check the arrow position when the tooltip text gets longer, and place it at the right edge of the trigger.

html
<style>
  .tip { position: relative; display: inline-block; margin: 20px; padding: 6px 10px; background: #222; color: #fff; font: 14px sans-serif; }
  .tip::after { content: ""; position: absolute; bottom: 100%; left: 12px; width: 0; height: 0;
    border-left: 6px solid transparent; border-right: 6px solid transparent; border-bottom: 6px solid #222; }
  .tri { width: 20px; height: 20px; background: #c00; clip-path: polygon(50% 0, 100% 100%, 0 100%); }
</style>
<div class="tip" id="t">Hint text</div>
<div class="tri" id="tri"></div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Borders meet diagonally, so a zero-size box shows triangles.
  • The coloured border is on the side opposite the point.
  • clip-path: polygon() makes a triangle from a box.
  • Tooltip arrows are positioned pseudo-elements.
  • Use SVG for complex shapes.

Common mistakes

  • Giving the arrow element a width or height, so the border triangle becomes a trapezoid.
  • Using a different colour from the tooltip background, so the arrow looks detached.
  • Using clip-path on the same element as a shadow and losing the shadow.

Follow-ups

  • Why does a zero-size element with borders show triangles?
  • What are the downsides of clip-path for a tooltip arrow?

Tips

  • Describe the border method and the clip-path method.
  • Mention that coloured border faces away from the point.