Finally! CSS Triangles Without Ugly Hacks

Anyone who has tried to make HTML upvote arrows, speech bubbles or other pointy elements, knows that in order to create a CSS-only triangle you have to use some sort of hack. The two most popular solutions are to create your triangles out of borders, or to use unicode characters.

We have to admit that both these CSS hacks are pretty clever but they still are terrible solutions, and as such make our code much uglier and less flexible. For instance, we can't have a triangle with a background image, since borders and characters can only be one color.

Introducing Clip-path

Clip-path is a fairly new addition to the CSS spec that allows us to show only part of an element and hide the rest. Here is how it works:

Let's say we have an ordinary rectangular div element. You can click Run in the editor below to view the rendered HTML.

div {
    width: 200px;
    height: 200px;
    background: url(https://goo.gl/BeSyyD);
}
<div></div>

To make a triangle we will need the polygon() function. It expects as argument comma separated 2D points which will define the shape of our mask. A triangle = 3 points. Try and change the values to see how the shape transforms.

div {
    width: 200px;
    height: 200px;
    background: url(https://goo.gl/BeSyyD);

    /* The points are: centered top, left bottom, right bottom */
    clip-path: polygon(50% 0, 0 100%, 100% 100%);
}
<div></div>

Everything inside the path we created stays, everything outside it is hidden. This way we can make not only triangles, but all sorts of asymmetrical shapes that will behave like regular CSS blocks.

The only drawback of this technique is that we have to carefully calculate the coordinates of our points in order to get a good looking triangle.

Still, it's way better than using borders or ▲.

Browser Support

If you open the caniuse page for clip-path things don't look very good at first sight, but in reality the property works perfectly fine unprefixed in Chrome and with the -webkit- prefix in Safari. Firefox users have to wait till version 53. IE/Edge is behind the curve as usual but we can expect support sometime in the future.

Further Reading

The clip-path property has many other tricks up its sleeve, including some SVG magic. To find out more about it check out the links below.

  • Clip-path on MDN - here
  • In-depth tutorial for clip-path on Codrops - here
  • Clippy, a clip-path generator - here
Bootstrap Studio

The revolutionary web design tool for creating responsive websites and apps.

Learn more

Related Articles

Comments 3

  • As a follow-up from Tarek's point, since we're now a year on. I'm using this technique for a lead image style. Browser support still isn't full - IE and Edge still lacking - but all the other major players are there as far as our users go. The fallback for IE/Edge isn't ugly in my use-case, and it's really great to be able to do this without images or CSS borders. Thank you for posting.

Thank you! Finally the solution I was looking for to make a right triangle!
clip-path: polygon(0 0, 100% 100%, 0 100%);

Thank you Danny for this tutorial,

but I think it's totally useless at this point because of the lack of browser support. but I'm glad to see css is moving forward and maybe in a year or two we'll be able to use it without fallbacks and dirty hacks.