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

推荐订阅源

Martin Fowler
Martin Fowler
L
LINUX DO - 最新话题
P
Proofpoint News Feed
Cyberwarzone
Cyberwarzone
Know Your Adversary
Know Your Adversary
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
L
Lohrmann on Cybersecurity
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Security Latest
Security Latest
T
The Exploit Database - CXSecurity.com
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Privacy & Cybersecurity Law Blog
K
Kaspersky official blog
The Last Watchdog
The Last Watchdog
Webroot Blog
Webroot Blog
Scott Helme
Scott Helme
T
Threat Research - Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
WordPress大学
WordPress大学
L
LINUX DO - 热门话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - Franky
V
Visual Studio Blog
O
OpenAI News
AI
AI
Hacker News: Ask HN
Hacker News: Ask HN
V2EX - 技术
V2EX - 技术
GbyAI
GbyAI
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Simon Willison's Weblog
Simon Willison's Weblog
S
Schneier on Security
Spread Privacy
Spread Privacy
Y
Y Combinator Blog
I
InfoQ
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
F
Fortinet All Blogs
C
CERT Recently Published Vulnerability Notes
T
The Blog of Author Tim Ferriss
C
Check Point Blog
Apple Machine Learning Research
Apple Machine Learning Research
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
N
News and Events Feed by Topic
Project Zero
Project Zero
小众软件
小众软件
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
B
Blog
G
Google Developers 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 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 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
How to create Shapes with Inner Curves using CSS Mask
2024-09-25 · via CSS Articles by Temani Afif

How to create Shapes with Inner Curves using CSS Mask

Written on by Temani AfifWeb Developer

Estimated read time 5 minutes

Inverted radius, notch, rounded cut, bell curve, etc. A lot of names can be used to describe the shapes we are going to create in this article. I also call them inner curves but I guess a figure worth thousand words.

CSS-only shapes (inverted radius, inner curves)

Naming those shapes is not that easy, same as creating them using CSS. Most of the time we reach for many elements/pseudo-elements and we try to stack them in a way to simulate the curvature. It’s a bit hacky, not flexible and it’s a lot of magic numbers everywhere! I will show you how to create those CSS shapes using one element and a flexible code that you can easily reuse.

Before we start, you can already find the code of the shapes within my online collection but stay with me to know the secret behind creating them.

Inverted Radius shape

Let’s start with the first shape. The idea is to cut the corner of an element while having smooth corners.

CSS only inverted radius shape

Cutting the corner of an element is an easy task. All that you need is one gradient:

mask: radial-gradient(40px at top right,#0000 98%,#000)

That’s all! I also have an online generator where you can find the different combination. And if you want to dig into the detail, check my article “Tricks to Cut Corners Using CSS Mask and Clip-Path Properties”.

If you are not familiar with mask and gradients, I highly advice you to take the time to read the linked article and study the simple case of cutting a corner. The shape we are going to create will use the same technique but with a more complex gradient configuration.

Here is a step-by-step illustration of the gradient configuration

Illustration of the gradient configuration for the inverted radius shape

First, we use the same radial-gradient we used to cut the top-right corner. The only difference is an offset from the top and right edges. Then, we consider a second gradient (a conic-gradient this time) to fill the space created by the shift of previous gradient while keeping some space for the curvature. Finally, we add two radial-gradient to fill the remaining space and simulate the curvature.

I am using three different colors to illustrate the puzzle. The colored part is what will be visible from the element while the remaining will be the transparent part.

Inverted radius variables

Two variables will control our shape. --r will be the radius of the small circle (the curvature) and --s will be the size of the cut.

Let’s translate this into some code:

.shape {
  --r: 25px; /* the radius */
  --s: 40px; /* the size of the cut */
  
  --_m:/calc(2*var(--r)) calc(2*var(--r))
    radial-gradient(#000 70%,#0000 72%) no-repeat;
  mask:
    right calc(var(--s) + var(--r)) top 0 var(--_m),
    right calc(var(--s) + var(--r)) var(--_m),
    radial-gradient(var(--s) at 100% 0,#0000 99%,#000 101%) 
     calc(-1*var(--r)) var(--r) no-repeat,
    conic-gradient(at calc(100% - var(--s) - 2*var(--r)) calc(var(--s) + 2*var(--r)), #0000 25%,#000 0);
}

The mask property contains four gradients like described previously. Two gradients are identical (the small circles) that’s why I am using the variable --_m to avoid the repetition.

But the code is a bit complex and not easy to memorize…

You don’t need to memorize anything! All you have to do is copy the code and use it. The idea is to understand the logic behind creating the shape not to memorize the code. After all, you don’t have to touch the code since all you have to do is to update a few variables to control the shape. I told you we are going to create a flexible code easy to reuse!

This said, it’s also good to be able to write some CSS alone so here is a homework for you. Can you adjust the previous code to create the other variations? We did an inverted radius at the top-right corner and we can use the same code structure for the other corners.

Different variations for the inverted radius shape

You can always find the code within my online collection and here is a demo where I am using the different variations with image elements:

Try to use the same process I described: one gradient at a time until you get the final shape. It’s a bit tricky at first but we get used to it and maybe you will find a different implementation than mine.

Inverted radius shape with offsets

We can upgrade the previous shape by considering more variables to control the offsets. Actually we can only control the curvature but it's better if we can move the curvature like below for example:

Overview of the offset

The above figure illustrates a horizontal offset but we can also have a vertical one as well. For this, we will introduce a fourth gradient and adjust the configuration slightly.

Overview of the gradients

Previously we used one conic-gradient but for this shape, we use two conic-gradient as illustrated in (1). Both gradients have the same configuration, they simply differ by their position which is controlled by the new offset variables:

.inverted-radius {
  --r: 20px; /* the radius */
  --s: 30px; /* size of inner curve */
  --x: 50px; /* horizontal offset (no percentage) */
  --y: 10px; /* vertical offset (no percentage) */
  
  --_g:conic-gradient(at calc(100% - var(--r)) var(--r),#0000 25%,#000 0);
  --_d:(var(--s) + var(--r));
  mask:
    var(--_g) calc(-1*var(--_d) - var(--x)) 0,
    var(--_g) 0 calc(var(--_d) + var(--y));
  mask-repeat: no-repeat;
}

Then we use the same radial-gradient for the curvatures. One gradient for the inner curve and two others to create the small circles on the corners.

.inverted-radius {
  --r: 20px; /* the radius */
  --s: 30px; /* size of inner curve */
  --x: 50px; /* horizontal offset (no percentage) */
  --y: 10px; /* vertical offset (no percentage) */
  
  --_m:/calc(2*var(--r)) calc(2*var(--r)) radial-gradient(#000 70%,#0000 72%);
  --_g:conic-gradient(at calc(100% - var(--r)) var(--r),#0000 25%,#000 0);
  --_d:(var(--s) + var(--r));
  mask:
    calc(100% - var(--_d) - var(--x)) 0 var(--_m),
    100% calc(var(--_d) + var(--y)) var(--_m),
    radial-gradient(var(--s) at 100% 0,#0000 99%,#000 calc(100% + 1px)) 
     calc(-1*var(--r) - var(--x)) calc(var(--r) + var(--y)),
    var(--_g) calc(-1*var(--_d) - var(--x)) 0,
    var(--_g) 0 calc(var(--_d) + var(--y));
  mask-repeat: no-repeat;
}

The code looks more complex but all you have to do is to adjust the variables to control shape. There is no need to learn everything by heart! Here is a demo where I am using the previous code to create a cool hover effect. All that I am doing is animating the horizontal offset:

Notch Shape

Let’s move to the next shape. This time, the cut will be on the sides instead of the corners.

Like the previous shape, creating a circular cut is as as simple as using one gradient (code also available at: https://css-shape.com/circle-cut/)

mask: radial-gradient(40px at top,#0000 98%,#000)

But to have the curvature part we need more gradients. The same number of gradients as the previous shape since the logic is the same.

The variables of the notch shape

I think we can skip the explanation for this one. Like the inverted radius, we have one gradient for the cut, one gradient to fill the extra space and two gradients for the curvature.

Even the variables are the same but this time I will introduce a third variable that will control the depth of the curve.

The shape is built using three circles that are aligned (one circle for the cut and two circles for the curvature). We can adjust the alignment to add more control to the shape like illustrated below:

Illustration of the depth variable

On the left, all the circles are aligned horizontally but on the right the big circle is shifted to the top. The circles are still adjacent but no more aligned. I drew a line to illustrate the alignment and the angle of rotation of that line will be our new variable.

.shape {
  --r: 20px; /* control the rounded part */
  --s: 40px; /* control the size of the cut */
  --a: 40deg; /* control the depth of the curvature */
  
  --_m:0/calc(2*var(--r)) calc(2*var(--r)) no-repeat
    radial-gradient(50% 50%,#000 calc(100% - 1px),#0000);
  --_d:(var(--s) + var(--r))*cos(var(--a));
  mask:
    calc(50% + var(--_d)) var(--_m),calc(50% - var(--_d)) var(--_m),
    radial-gradient(var(--s) at 50% calc(-1*sin(var(--a))*var(--s)),
      #0000 100%,#000 calc(100% + 1px)) 0 calc(var(--r)*(1 - sin(var(--a)))) no-repeat,
    linear-gradient(90deg,#000 calc(50% - var(--_d)),#0000 0 calc(50% + var(--_d)),#000 0);
}

I know, the code is more complex for this shape but we have our variables so we don’t really care about the complexity. Do you think I will remember that code? Not at all. I will go to my online collection to copy it and adjust the variables to fit my use case.

What is good with modern CSS is that we can easily create reusable code. You make the effort once and you have a code that you can use as many time as you want. No more magic numbers and hacky tricks that work only for particular cases!

Here is the full code showing the top and bottom variations. Can you figure out how to create the bottom version before checking my code? Another exercise to practice with gradients.

And what about a fancy animation? Hover the below and see the nice effect you get!

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