Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

CSS Houdini – The Paint API | CSS Basics to Advanced lesson | BraveProgrammer
  1. Courses
  2. /
  3. CSS Basics to Advanced

Lesson 44 of 50

·css

CSS Houdini – The Paint API

10 mins

Course progress88%

What you'll learn

Keep these ideas in mind as you work through the lesson.

3 points
  • Houdini is still experimental; works in Chrome‑based browsers.
  • Allows **custom drawing**, **layout**, and **animation** that were impossible before.
  • Pair with `@property` to animate custom properties.

Houdini opens the browser’s rendering engine to developers, allowing you to write custom CSS renderers in JavaScript.

The Paint API – Drawing Backgrounds with JS

  1. Register a paint worklet.
  2. Define which CSS properties the worklet reads.
  3. Paint on a canvas‑like context.
/* register the worklet */
if ('paintWorklet' in CSS) {
  CSS.paintWorklet.addModule('bg-paint.js');
}

bg-paint.js

registerPaint('bg', class {
  static get inputProperties() { return ['--color']; }
  paint(ctx, size, props) {
    ctx.fillStyle = props.get('--color').toString();
    ctx.fillRect(0, 0, size.width, size.height);
  }
});

Using the Paint Worklet

.box {
  --color:#ff6600;
  background-image:paint(bg);
  width:200px; height:200px;
}

The box now has a programmatically generated background that can react to CSS variables.


Real‑World Use‑Cases

  • Pattern generation – bricks, stripes, or noise without loading images.
  • Dynamic theming – a colour picker that instantly updates a complex gradient.
  • Performance – a single JS worklet can replace many heavy DOM elements.

Browser Support

  • Currently Chrome‑based browsers (Chrome 89+, Edge 89+).
  • Safari and Firefox have experimental flags; best‑practice is to provide a fallback CSS background.

Fallback Example

.box {
  background:#ff6600;               /* fallback for unsupported browsers */
  background-image:paint(bg);      /* Houdini paint */
}

Limitations & Considerations

  • The worklet runs off‑main‑thread (via a separate worker), so it doesn’t block UI.
  • Security – only a subset of the Canvas API is exposed; no DOM access.
  • Cacheability – the painted result is re‑computed each frame if any dependent property changes.

Pro Tip – Combine with @property to animate a custom property.

@property --angle {
  syntax:'<angle>'; inherits:true; initial-value:0deg;
}
.box {
  --angle:45deg; 
  background-image:paint(gradient);
  transition:--angle 2s linear;
}

Now you can animate --angle and let the paint worklet draw a rotating gradient.

Accessibility Note

Since Houdini worklets only affect visuals, they do not impact screen readers. Ensure you still provide a fallback colour for high‑contrast mode.

Make it stickPractice what you've learned1 quiz · 6 practical tests · Optional activities from CSS Basics to AdvancedBrowse activities
Previous lessonCSS‑in‑JS (Styled‑Components, Emotion)Continue to lesson 45Logical CSS for InternationalizationBack to CSS Basics to Advanced curriculum