Intro to CSS Houdini

Roberto Moreno Celta
The Startup
Published in
3 min readApr 21, 2023
Css
Photo by Pankaj Patel on Unsplash

CSS Houdini is a set of low-level APIs that allow developers to create custom CSS properties, animations, and effects. These APIs enable developers to create and customize their own CSS rendering engines and extend the capabilities of the browser beyond what is currently possible with standard CSS.

https://houdini.how/ is a website dedicated to showcasing the power of CSS Houdini. The website offers a variety of demos that demonstrate the capabilities of CSS Houdini, including custom CSS properties, animations, and effects. Some of the demos on the website include “CSS Grid Generator,” “CSS Grid Animation,” “CSS Shapes,” and “CSS Paint API.”

One of the most powerful features of CSS Houdini is the CSS Paint API, which allows developers to create custom graphics and effects directly in CSS. The CSS Paint API is a part of the CSS Houdini Paint Worklet API, which allows developers to create custom paint worklets that can be used in CSS. Paint worklets are similar to other types of worklets, such as animation worklets and layout worklets, and can be used to create custom rendering engines and effects.

To demonstrate the power of the CSS Paint API, we can create a custom paint worklet that draws a simple gradient. To do this, we can create a JavaScript file that defines the paint worklet and registers it with the browser. Here is an example:

class GradientPainter {
static get inputProperties() {
return ['--gradient-start', '--gradient-end'];
}

paint(ctx, geom, properties) {
const gradient = ctx.createLinearGradient(0, 0, geom.width, geom.height);
gradient.addColorStop(0, properties.get('--gradient-start').toString());
gradient.addColorStop(1, properties.get('--gradient-end').toString());
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, geom.width, geom.height);
}
}

registerPaint('gradient', GradientPainter);

This JavaScript file defines a custom paint worklet called “gradient” that takes two input properties, “ — gradient-start” and “ — gradient-end,” which define the start and end colors of the gradient. The paint method of the GradientPainter class creates a linear gradient using the start and end colors and fills a rectangle with the gradient.

To use this custom paint worklet in CSS, we can define a new CSS property called “ — gradient” that uses the “gradient” paint worklet to draw the gradient. Here is an example:

.gradient {
--gradient: paint(grad);
--gradient-start: red;
--gradient-end: blue;
width: 100px;
height: 100px;
}

In this CSS code, we define a new CSS class called “gradient” that sets the “ — gradient” property to use the “gradient” paint worklet. We also set the “ — gradient-start” and “ — gradient-end” properties to define the start and end colors of the gradient. Finally, we set the width and height of the element to 100 pixels.

With these CSS and JavaScript files in place, we can use the “gradient” class to create a div element with a custom gradient:

<div class="gradient"></div>

This code creates a div element with the “gradient” class, which applies the custom gradient defined in the CSS file.

In conclusion, CSS Houdini is a powerful set of APIs that allows developers to create custom CSS rendering engines and effects. The CSS Paint API, in particular, allows developers to create custom graphics and effects directly in CSS.

--

--

Roberto Moreno Celta
The Startup

UX Designer & Front End Dev. Love for food, movies and video games.