惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cloudbric
Cloudbric
T
Threat Research - Cisco Blogs
Simon Willison's Weblog
Simon Willison's Weblog
AWS News Blog
AWS News Blog
P
Privacy & Cybersecurity Law Blog
H
Help Net Security
云风的 BLOG
云风的 BLOG
G
GRAHAM CLULEY
Spread Privacy
Spread Privacy
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
Arctic Wolf
Project Zero
Project Zero
Engineering at Meta
Engineering at Meta
P
Privacy International News Feed
Blog — PlanetScale
Blog — PlanetScale
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence
The Register - Security
The Register - Security
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
Cisco Blogs
PCI Perspectives
PCI Perspectives
Recent Announcements
Recent Announcements
Martin Fowler
Martin Fowler
A
About on SuperTechFans
W
WeLiveSecurity
GbyAI
GbyAI
V
Vulnerabilities – Threatpost
The GitHub Blog
The GitHub Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Check Point Blog
Y
Y Combinator Blog
月光博客
月光博客
Scott Helme
Scott Helme
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
F
Fortinet All Blogs
U
Unit 42
G
Google Developers Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Threatpost
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google Online Security Blog
Google Online Security Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Cisco Talos Blog
Cisco Talos Blog
博客园 - 三生石上(FineUI控件)
Hugging Face - Blog
Hugging Face - Blog
MongoDB | Blog
MongoDB | Blog
博客园 - 司徒正美

CSS Articles by Temani Afif

Get Ready For the Powerful CSS border-shape Property! | CSS-Tricks Let’s Play With Gap Decorations! How to Control Infinite CSS Animations (Part 2 of 2) How to Control Infinite CSS Animations (Part 1 of 2) Two Circles, One Arrow, and Anchor Positioning Making a Responsive Pyramidal Grid With Modern CSS | CSS-Tricks How to Create a CSS-only Elastic Text Effect Making Complex CSS Shapes Using shape() | CSS-Tricks Responsive Hexagon Grid Using Modern CSS | CSS-Tricks Responsive List of Avatars Using Modern CSS (Part 2) | CSS-Tricks Responsive List of Avatars Using Modern CSS (Part 1) | CSS-Tricks Perfectly Pointed Tooltips: To The Corners Perfectly Pointed Tooltips: All Four Sides Perfectly Pointed Tooltips: A Foundation Sequential linear() Animation With N Elements | CSS-Tricks Infinite Marquee Animation using Modern CSS Better CSS Shapes Using shape() — Part 4: Close and Move | CSS-Tricks Drawing CSS Shapes using corner-shape Better CSS Shapes Using shape() — Part 3: Curves | CSS-Tricks Better CSS Shapes Using shape() — Part 2: More on Arcs | CSS-Tricks Better CSS Shapes Using shape() — Part 1: Lines and Arcs | CSS-Tricks Creating Blob Shapes using clip-path: shape() Creating Flower Shapes using clip-path: shape() Custom progress element using the attr() function A CSS-Only Star Rating Component and More! (Part 2) | CSS-Tricks A CSS-Only Star Rating Component and More! (Part 1) | CSS-Tricks How to Create Wavy Boxes Using CSS Full-Bleed Layout with Modern CSS Fancy Menu Navigation Using Anchor Positioning | CSS-Tricks How to Create a Zig-Zag Box Using CSS How to Create Zig-Zag CSS Loaders Using One Element Custom Progress Element Using Anchor Positioning & Scroll-Driven Animations How to Create Filling CSS Loaders Using One Element How to Create Curved-Edge and Rounded-Edge Shapes Using CSS CSS Tricks That Use Only One Gradient | CSS-Tricks How to create Shapes with Inner Curves using CSS Mask Custom Range Slider Using Anchor Positioning & Scroll-Driven Animations How to Get the Width/Height of Any Element in Only CSS How Keyboard Navigation Works in a CSS Game How To Create Cut-Out Shapes using The clip-path property The Modern Guide For Making CSS Shapes — Smashing Magazine css-shape.com: The Ultimate Collection of CSS-only Shapes ⚡️ Sliding 3D Image Frames In CSS — Smashing Magazine CSS Tricks To Master The clip-path Property Creating Wavy Circles with Fancy Animations in CSS Modern CSS Tooltips And Speech Bubbles (Part 2) — Smashing Magazine Do you need a Tooltip or a Speech Bubble? I have created 100 using CSS 😲 Modern CSS Tooltips And Speech Bubbles (Part 1) — Smashing Magazine Creating Flower Shapes using CSS Mask & Trigonometric Functions The Complex But Awesome CSS border-image Property — Smashing Magazine CSS Tricks to add 3D Effects to your Text CSS Responsive Multi-Line Ribbon Shapes (Part 2) — Smashing Magazine CSS Responsive Multi-Line Ribbon Shapes (Part 1) — Smashing Magazine I have made 100+ CSS-only Ribbon Shapes | The Perfect Collection 🎀 How to create a CSS-only infinite scroll animation Re-Creating The Pop-Out Hover Effect With Modern CSS (Part 2) — Smashing Magazine Re-Creating The Pop-Out Hover Effect With Modern CSS (Part 1) — Smashing Magazine How to Create CSS Ribbon Shapes with a Single Element CSS Shapes: The Ribbon Revealing Images With CSS Mask Animations — Smashing Magazine css-loaders.com: The Biggest Collection of Loading Animations (more than 500 🤯) How To Define An Array Of Colors With CSS — Smashing Magazine CSS effects on images II Shines, Perspective, And Rotations: Fancy CSS 3D Effects For Images — Smashing Magazine What's your Lucky CSS Pattern? Check It Now! How to Add a CSS Reveal Animation to Your Images Modern Layouts using CSS Grid I created 100+ unique CSS patterns | The best collection 🤩 How to Create a Custom Range Slider Using CSS CSS Shapes: The Heart CSS Shapes: Polygon & Starburst A Text Reveal Animation using CSS CSS Tip: learn CSS the easy way! How to build a CSS-only accordion How to create Breadcrumb Navigation with CSS Different Ways to Get CSS Gradient Shadows | CSS-Tricks A CSS-only responsive Stepper component A Fancy Hover Effect For Your Avatar | CSS-Tricks How to make a zoom effect using CSS Creating a Custom Cursor using CSS CSS Infinite 3D Sliders | CSS-Tricks CSS Infinite Slider Flipping Through Polaroid Images | CSS-Tricks How to create a responsive sidebar menu using CSS CSS Infinite and Circular Rotating Image Slider | CSS-Tricks Making Static Noise From a Weird CSS Gradient Bug | CSS-Tricks CSS Grid and Custom Shapes, Part 3 | CSS-Tricks Fancy Image Decorations: Outlines and Complex Animations | CSS-Tricks Fancy Image Decorations: Masks and Advanced Hover Effects | CSS-Tricks Fancy Image Decorations: Single Element Magic | CSS-Tricks How to create an infinite image slider using CSS How to Create Wavy Shapes & Patterns in CSS | CSS-Tricks How I Made a Pure CSS Puzzle Game | CSS-Tricks How to create a Tooltip/Speech Bubble using CSS CSS Grid and Custom Shapes, Part 2 | CSS-Tricks CSS Grid and Custom Shapes, Part 1 | CSS-Tricks Zooming Images in a Grid Layout | CSS-Tricks How to create a CSS-only loader with one element Exploring CSS Grid’s Implicit Grid and Auto-Placement Powers | CSS-Tricks How to create background pattern using CSS & conic-gradient Single Element Loaders: The Bars | CSS-Tricks
CSS Shapes: The Triangle
2024-02-09 · via CSS Articles by Temani Afif

The triangle is probably the most popular shape in CSS and also the easiest one to create. A lot of techniques can be used to create such a shape but most of them are too old and not efficient (like the border method).

“Why another article about triangles?” you may ask. In this article, I am going to focus on modern ways to create triangle shapes but I will do even more than simple triangles. I am also going to create border-only variation and rounded corners variation as well.

In addition to the different variations, the main challenge is to consider only one element and an optimized CSS code that we can easily adjust to control the shape.

I have an online generator for triangle shapes from where you can copy/paste the code of any triangle shape you want but stay with me if you want to know the secret behind creating them.

Creating a Triangle Shape

To create a triangle all you need is the clip-path property and a 3-point polygon. That’s all!

clip-path: polygon(x1 y1,x2 y2, x3 y3);

I think it’s not really a surprise since 3 points is all we need to draw a triangle.

In reality, we are not going to use random points but rather consider the easy ones like the corners or the middle of sides.

For example, the below will create an isosceles right triangle.

.triangle {
  width: 200px;
  aspect-ratio: 1;
  polygon(0 0,100% 100%,0 100%);
}

<iframe height="300" style="width: 100%;height:350px" scrolling="no" title="Untitled" src="https://codepen.io/t_afif/embed/preview/QWoOLzR/d0f0b51ba246203be0f8c0e62cbe0800?default-tab=result" frameborder="no" loading="lazy" allowtransparency="true" allowfullscreen="true">

See the Pen <a href="https://codepen.io/t_afif/pen/QWoOLzR/d0f0b51ba246203be0f8c0e62cbe0800">

Untitled</a> by Temani Afif (<a href="https://codepen.io/t_afif">@t_afif</a>)

on <a href="https://codepen.io">CodePen</a>.

</iframe>

It’s pretty simple and if you have trouble with the coordinates of the points here is a figure to illustrate the common ones you will use the most. You can also refer to my article "CSS Tricks To Master The clip-path Property" for more details

And here are a few examples of triangles

Note the use of aspect-ratio which is useful when dealing with responsiveness. It allows you to define the ratio and then only update one value (either the width or the height) to control the size.

Now you have all that you need to create any kind of triangle shape. You can even apply it to image elements and consider gradient coloration!

You can find the common shapes within my online collection but I highly recommend you to try creating some of them as a small exercise. A good opportunity to practice with clip-path and build some triangles!

Border-only Triangle Shapes

Creating a simple triangle is easy but it gets more tricky when we want to consider the border-only version. We are still relying on clip-path but we need more points this time.

The above figure illustrates the number of points. I am intentionally leaving a gap at the top to understand that we need an 8-point polygon. You may intuitively think that we need a 6-point polygon since we started with a 3-point polygon for the simple triangle but no.

This said, we only have 6 different coordinates because if we close the gap at that top, we will have an overlap between 4 points so two points will get repeated.

Let’s take the previous triangle and create a border-only version of it:

<iframe height="300" style="width: 100%;height: 300px" scrolling="no" title="Untitled" src="https://codepen.io/t_afif/embed/preview/dyrZPoL/3c854c7556ef0e4244084f992a592c94?default-tab=result" frameborder="no" loading="lazy" allowtransparency="true" allowfullscreen="true">

See the Pen <a href="https://codepen.io/t_afif/pen/dyrZPoL/3c854c7556ef0e4244084f992a592c94">Untitled</a> by Temani Afif (<a href="https://codepen.io/t_afif">@t_afif</a>)

on <a href="https://codepen.io">CodePen</a>. </iframe>

.triangle {
  width: 200px;
  aspect-ratio: 1;
  clip-path: 
    polygon(
      /* the outer points */
      0 0,100% 100%,0 100%,0 0,
      /* the inner points */
      5% 10%,5% 95%,90% 95%,5% 10%
    );
}

The “outer points” are the ones we used to create the basic triangle where the first point (0,0) is repeated twice and the “inner points are the new ones that will define the border of our triangle. The first point (5%,10%) is also repeated twice.

The difficult part here is to find the correct coordinates for the inner points to get a uniform thickness on all the sides. You will need some geometry/math knowledge to identify the formulas and I know that it can be tricky that’s why having an online generator can be a lifesaver.

The code of the previous triangle can be written like below:

.triangle {
  --b: 15px; /* control the border thickness */

  width: 150px;
  aspect-ratio: 1;
  clip-path: 
    polygon(
      /* outer points */
      0 100%,0 0,100% 100%,0 100%,
      /* inner points */
      var(--b) calc(100% - var(--b)),
      calc(100% - var(--b)/tan(22.5deg)) calc(100% - var(--b)),
      var(--b) calc(var(--b)/tan(22.5deg)),
      var(--b) calc(100% - var(--b))
    );
}

Even if you don’t understand all the values/formulas of the clip-path, all you have to do is update one variable to control the border thickness.

<iframe height="300" style="width: 100%;height:300px" scrolling="no" title="Untitled" src="https://codepen.io/t_afif/embed/preview/XWGzJpP/a330133c89e30d9a81e78e51ac2263b4?default-tab=result" frameborder="no" loading="lazy" allowtransparency="true" allowfullscreen="true">

See the Pen <a href="https://codepen.io/t_afif/pen/XWGzJpP/a330133c89e30d9a81e78e51ac2263b4">Untitled</a> by Temani Afif (<a href="https://codepen.io/t_afif">@t_afif</a>)

on <a href="https://codepen.io">CodePen</a>.

</iframe>

You will find 12 variations within my collection. I tried to create the most common ones.

Rounded corners triangle shapes

For this variation, we are going to introduce mask. clip-path alone cannot give us rounded shapes but mask combined with radial-gradient() can do it.

Here is a figure to illustrate the process

The first step is to use a 3-point polygon to create a simple triangle as we did in the first section. The second step is to use radial-gradient() to create 3 circles each one placed at a corner. We will need 3 gradients and a variable to control their radius. The third step is to fill the middle area using another gradient illustrated in green.

If we consider the red and green areas as the visible area of our element then we get the rounded triangle shown in the last step.

Explained like that, the process may sound easy but finding the gradient configuration is not that easy. It’s more complex than the border-only version because it depends on the nature of the triangle and each type of triangle will have its implementation.

Here is the code for the previous figure:

.triangle {
  --r: 20px; /* border radius */

  width: 180px;
  aspect-ratio: 1/cos(30deg);
  --_g:calc(tan(60deg)*var(--r)) bottom var(--r),#000 98%,#0000 101%;
  mask:
    /* a conic gradient to fill the middle area */
    conic-gradient(from -30deg at 50% calc(200% - 3*var(--r)/2),#000 60deg,#0000 0)
     0 100%/100% calc(100% - 3*var(--r)/2) no-repeat,
    /* 3 radial gradients for 3 circles */
    radial-gradient(var(--r) at 50% calc(2*var(--r)),#000 98%,#0000 101%),
    radial-gradient(var(--r) at left  var(--_g)),
    radial-gradient(var(--r) at right var(--_g));
  /* the 3-point polygon */
  clip-path: polygon(50% 0,100% 100%,0 100%);
}

Like the border-only version, even if the code may look complex all you have to do is to update one variable to control the radius:

<iframe height="300" style="width: 100%;height:300px" scrolling="no" title="Untitled" src="https://codepen.io/t_afif/embed/preview/JjzOWKw/42883a4d9d7e8bc1307f3a4c82e9da35?default-tab=result" frameborder="no" loading="lazy" allowtransparency="true" allowfullscreen="true">

See the Pen <a href="https://codepen.io/t_afif/pen/JjzOWKw/42883a4d9d7e8bc1307f3a4c82e9da35">Untitled</a> by Temani Afif (<a href="https://codepen.io/t_afif">@t_afif</a>)

on <a href="https://codepen.io">CodePen</a>.

</iframe>

You can find the code of the common shapes within my collection of triangle shapes

Adding Some Animations

Now that we have our shapes, let’s animate them! Both the border-only and rounded variations rely on a CSS variable to control the border thickness or the radius. We can animate that variable to get cool effects.

For this, we need to rely on @property to register the variable and be able to animate it.

@property --r {
  syntax: "<length>";
  initial-value: 0px;
  inherits: false;
}

We can for example create a hover effect to slightly adjust the radius of the rounded triangles:

<iframe height="300" style="width: 100%;height:400px" scrolling="no" title="Rounded triangles with hover effect" src="https://codepen.io/t_afif/embed/preview/poYPLbp?default-tab=result" frameborder="no" loading="lazy" allowtransparency="true" allowfullscreen="true">

See the Pen <a href="https://codepen.io/t_afif/pen/poYPLbp"> Rounded triangles with hover effect</a> by Temani Afif (<a href="https://codepen.io/t_afif">@t_afif</a>)

on <a href="https://codepen.io">CodePen</a>.

</iframe>

If we use a big radius value we can transform a circle into a triangle:

<iframe height="300" style="width: 100%;height:400px" scrolling="no" title="Untitled" src="https://codepen.io/t_afif/embed/preview/qBvVrwB/029aa7cf1263635adbd05f0f9431f01f?default-tab=result" frameborder="no" loading="lazy" allowtransparency="true" allowfullscreen="true">

See the Pen <a href="https://codepen.io/t_afif/pen/qBvVrwB/029aa7cf1263635adbd05f0f9431f01f">Untitled</a> by Temani Afif (<a href="https://codepen.io/t_afif">@t_afif</a>)

on <a href="https://codepen.io">CodePen</a>.

</iframe>

We can also do the same with the border-only version by animating the thickness to create some cool reveal effects.

<iframe height="300" style="width: 100%;height:400px" scrolling="no" title="Animating the border-thickness" src="https://codepen.io/t_afif/embed/preview/dyrZdev/1653d96523c21906fae67a4bacc25f15?default-tab=result" frameborder="no" loading="lazy" allowtransparency="true" allowfullscreen="true">

See the Pen <a href="https://codepen.io/t_afif/pen/dyrZdev/1653d96523c21906fae67a4bacc25f15">Animating the border-thickness</a> by Temani Afif (<a href="https://codepen.io/t_afif">@t_afif</a>)

on <a href="https://codepen.io">CodePen</a>.

</iframe>

Conclusion

I hope that, after this article, we will no longer use old methods to create triangles. With modern CSS we were able to create simple triangle shapes and also more complex ones like the border-only and rounded variation. Not to mention the animation effect as a bonus.

Don’t forget to bookmark my online generator for triangles to easily grab the code of any triangle. Also check my CSS Shapes collection for more CSS-only shapes.

Frequently Asked Questions

Temani Afif

Temani Afif is an expert web developer, a content creator, and a CSS addict. He is the mastermind behind CSS Loaders, CSS Generators, CSS Tip and many other CSS-related websites.

View more posts by Temani Afif