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

推荐订阅源

AI
AI
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
N
News and Events Feed by Topic
O
OpenAI News
W
WeLiveSecurity
S
Secure Thoughts
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Heimdal Security Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Schneier on Security
Schneier on Security
Cloudbric
Cloudbric
T
Threatpost
T
Troy Hunt's Blog
T
Tenable Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
The Hacker News
The Hacker News
Know Your Adversary
Know Your Adversary
Forbes - Security
Forbes - Security
Cyberwarzone
Cyberwarzone
C
CXSECURITY Database RSS Feed - CXSecurity.com
P
Privacy & Cybersecurity Law Blog
L
Lohrmann on Cybersecurity
博客园 - 司徒正美
腾讯CDC
博客园 - 三生石上(FineUI控件)
爱范儿
爱范儿
J
Java Code Geeks
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
M
MIT News - Artificial intelligence
T
The Blog of Author Tim Ferriss
MongoDB | Blog
MongoDB | Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
GbyAI
GbyAI
D
Docker
V
Visual Studio Blog
云风的 BLOG
云风的 BLOG
I
InfoQ
WordPress大学
WordPress大学
人人都是产品经理
人人都是产品经理
A
Arctic Wolf
L
LINUX DO - 热门话题
The Cloudflare Blog
雷峰网
雷峰网
Microsoft Azure Blog
Microsoft Azure 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 CSS Shapes: The Triangle 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 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 Ribbon
2023-09-18 · via CSS Articles by Temani Afif

CSS Shapes: The Ribbon

Written on by Temani AfifWeb Developer

Estimated read time 5 minutes

After the Heart Shape, let’s tackle another famous CSS shape; The Ribbon. I can hear you saying: “Why another article about CSS ribbon!?”. In this one, I will consider modern CSS tricks to create not one but a lot of ribbon shapes with the smallest code possible. In fact, the HTML code will be as simple as:

<div class="ribbon">I am a ribbon</div>

A single element is all that is needed to create different ribbon shapes. You can find all of them in one place and easily copy/paste the code: css-generators.com/ribbon-shapes

In this article, we will explore some of them to understand the CSS tricks used to create such shapes.

Overview of the CSS Ribbon shapes

I will consider three categories for the ribbon: The full ribbon (the top one on the figure above), the ribbons on the corners, and the ones on the sides. The goal is to provide a CSS code that we can easily adjust to control the ribbon shape and also make sure the shape fits its content. We are not going to use any magic number of fixed dimensions. The size will be based on the text inside the ribbon.

40%

💰 40% OFF YOUR FIRST MONTH WITH VERPEX MANAGED CLOUD SERVERS

with the discount code

SERVERS-SALE

The Full Ribbon

Let’s start with our first ribbon. Below is a figure to illustrate the different variables that will control the shape:

Overview of the CSS variables

We define them using CSS variables

.ribbon {
  --s: 50px; /* the ribbon size */
  --d: 20px; /* the depth */
  --c: 25px; /* the cutout part */
}

Now the trick is to rely on clip-path and CSS gradients to build the shape. You are probably thinking we are going to use pseudo-elements and a lot of CSS code but no. Two properties will do the job.

We first apply the gradients:

background:
  conic-gradient(at left  var(--s) bottom var(--d),
   #0000 25%,#0008 0 37.5%,#0004 0) 0   /50% no-repeat,
  conic-gradient(at right var(--s) bottom var(--d),
   #0004 62.5%,#0008 0 75%,#0000 0) 100%/50% no-repeat;
background-color: #CC333F

Overview of the gradients configuration

Each gradient covers half the area and is made with three different colors. A transparent one (#0000) and two semi-transparent ones. By defining a background-color behind the gradients we create the different shades of colors for our ribbon. Updating the color is as simple as changing the background-color.

The last step is to apply the clip-path. This will probably look a bit complex at first glance because the value is a bit verbose but if you follow the polygon point by point, it becomes easy.

.ribbon {
  clip-path: polygon(0 var(--d), var(--s) var(--d),var(--s) 0,calc(100% - var(--s)) 0,calc(100% - var(--s)) var(--d),100% var(--d),calc(100% - var(--c)) calc(50% + var(--d)/2),100% 100%,calc(100% - var(--s) - var(--d)) 100%,calc(100% - var(--s) - var(--d)) calc(100% - var(--d)),calc(var(--s) + var(--d)) calc(100% - var(--d)),calc(var(--s) + var(--d)) 100%,0 100%,var(--c) calc(50% + var(--d)/2))
}

Here is a figure to illustrate some of the points.

The clip-path polygon

You can refer to my article "CSS Tricks To Master The clip-path Property" to better understand the coordinates and how the shape's symmetry can help you easily find most of the values.

It’s done! We have a nice ribbon and all you have to do is update a few variables to easily control the shape:

.ribbon {
  --s: 50px; /* the ribbon size */
  --d: 20px; /* the depth */
  --c: 20px; /* the cutout part */
  
  padding: 0 calc(var(--s) + var(--d)) var(--d); /* we need some padding so the text doesn't overlap the ribbon part */
  background:
    conic-gradient(at left  var(--s) bottom var(--d),
     #0000 25%,#0008 0 37.5%,#0004 0) 0   /50% no-repeat,
    conic-gradient(at right var(--s) bottom var(--d),
     #0004 62.5%,#0008 0 75%,#0000 0) 100%/50% no-repeat;
  clip-path: polygon(0 var(--d), var(--s) var(--d),var(--s) 0,calc(100% - var(--s)) 0,calc(100% - var(--s)) var(--d),100% var(--d),calc(100% - var(--c)) calc(50% + var(--d)/2),100% 100%,calc(100% - var(--s) - var(--d)) 100%,calc(100% - var(--s) - var(--d)) calc(100% - var(--d)),calc(var(--s) + var(--d)) calc(100% - var(--d)),calc(var(--s) + var(--d)) 100%,0 100%,var(--c) calc(50% + var(--d)/2));
  background-color: #CC333F; /* the main color */
  width: fit-content; 
}

See the Pen by Temani Afif () on .

90%

💰 90% OFF YOUR FIRST MONTH WITH ALL VERPEX SHARED HOSTING PLANS

with the discount code

AWESOME

The Corner Ribbon

This ribbon shape is easier than the previous one because we have only one variable to control the shape and no complex gradient configuration.

Corner Ribbon Shape

We first define the background color and we apply a border at the bottom with a semi-transparent color to get a different shade from the main color and simulate the folded part.

.ribbon {
  --f: 15px; /* control the folded part */
  
  background: var(--c,#45ADA8);
  border-bottom: var(--f) solid #0007;
}

Then we apply the clip-path

.ribbon {
  clip-path: polygon(100% calc(100% - var(--f)),100% 100%,calc(100% - var(--f)) calc(100% - var(--f)),var(--f) calc(100% - var(--f)), 0 100%,0 calc(100% - var(--f)),999px calc(100% - var(--f) - 999px),calc(100% - 999px) calc(100% - var(--f) - 999px))
}

Most of the points should be easy to understand but two of them are a bit trick. the 999px calc(100% - var(--f) - 999px) and calc(100% - 999px) calc(100% - var(--f) - 999px). I have to cut the shape of an isosceles triangle on each side but to do this I need to know the height of the element. I don't have this information since the height depends on the content and we don't want to set a fixed dimension.

The trick is to rely on a big value that should be bigger than any height we may encounter. I am using 999px but it can be smaller since a ribbon won’t be that tall.

Cutting the corner of the ribbon shape

Finally, we rotate the ribbon either by 45deg or -45deg depending on the position we want. Such ribbon is generally placed on the corner of a container so in addition to the rotation we need to also consider some positioning to correctly place the ribbon:

.ribbon {
  position: absolute;
  top: 0;
}
.right {
  right: 0;
  transform: translate(calc((1 - cos(45deg))*100%), -100%) rotate(45deg);
  transform-origin: 0% 100%; /* bottom left */
}
.left {
  left: 0;
  transform: translate(calc((cos(45deg) - 1)*100%), -100%) rotate(-45deg);
  transform-origin: 100% 100%; /* bottom right */
}

Two more ribbon shapes straight into the collection!

The Side Ribbon

I suppose you get the main concept by now. All the ribbons use the same code structure and this one is not an exception.

The side ribbon shapes

We first apply a bottom border for the folded part and a left border to have enough space for the cutout. Then we use clip-path to create the final shape.

.ribbon {
  --f: 15px; /* control the folded part*/
  --r: 20px; /* control the ribbon shape */
  
  position: absolute;
  background: #FA6900;
  border-bottom: var(--f) solid #0005;
  border-left: var(--r) solid #0000;
  clip-path: 
    polygon(0 0,100% 0,100% calc(100% - var(--f)),calc(100% - var(--f)) 100%,
      calc(100% - var(--f)) calc(100% - var(--f)),0 calc(100% - var(--f)),
      var(--r) calc(50% - var(--f)/2));
  right: calc(-1*var(--f));
}

What about the other sides?

They follow the same idea with small updates to the code. In the above demo, I am showing the left side and the right side version but I let you try to figure out how to create the top side version. You can still find all of them here: css-generators.com/ribbon-shapes

40%

💰 40% OFF YOUR FIRST MONTH WITH VERPEX MANAGED HOSTING PLANS FOR WORDPRESS

with the discount code

SERVERS-SALE

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