Skip to content

Round the corner of complex polygon shapes using the CSS Paint API (Houdini project)

License

Notifications You must be signed in to change notification settings

Afif13/CSS-rounding-shapes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CSS Paint API: Rounding Shapes

From my article: https://css-tricks.com/exploring-the-css-paint-api-rounding-shapes/

CSS rounding shapes

Round the corners of any kind of complex shape. You can also adjust the radius of each corner individually.

How to use

First, you include the Paint Worklet:

<script>
if(CSS.paintWorklet) {              
  CSS.paintWorklet.addModule('src/rounding-shapes.js');
} else {
  console.log("Your browser doesn't support the Paint API :(");
}
</script>

Then the CSS will look like below:

@property --radius{
  syntax: '<length>';
  inherits: true;
  initial-value: 0;
}
@property --border{
  syntax: '<length>';
  inherits: true;
  initial-value: 0;
}

div {
  --radius: 5px; /* Defines the global radius */
  --border: 6px; /* Defines the border thickness */
  --path: ... ;  /* Define your shape here */

  --t: 0; /* The first type of mask on the main element */
  -webkit-mask: paint(rounding-shapes);
          mask: paint(rounding-shapes);
}

div::before {
  content: "";
  background: ...; 
  /* Add the below if you want the border-only version*/
  --t: 1; 
  -webkit-mask: paint(rounding-shapes);
          mask: paint(rounding-shapes);
  /**/
}

the --path variable behaves the same way as the path we define inside clip-path: polygon(). Use Clippy to generate one for you. It also accepts a third (optional) value for the radius.

Use Cases

A few use cases where this worklet can be useful

CSS Shapes

CSS Shapes

Speech Bubble

CSS Speech Bubble

Content Frames

CSS frames

Navigation Menu

CSS navigation menu

Section Divider

CSS section divider


Find all the details in my CSS-tricks article

About

Round the corner of complex polygon shapes using the CSS Paint API (Houdini project)

Topics

Resources

License

Stars

Watchers

Forks