Full answer
Houdini is a group of browser APIs that open up parts of the CSS engine to developers. Before it, the only way to extend CSS was a JavaScript library that worked around the browser. With Houdini, a script can hook into steps of styling and drawing.
The parts, by how much you can rely on them:
- Custom property registration:
@propertyin CSS orCSS.registerProperty()in script defines a type, a default and inheritance for a custom property, so it can be validated and animated. This is the most used part, and it is well supported in current browsers. Check the support table for your audience. - Typed OM: a way to read and write styles as typed values and not as strings.
el.attributeStyleMap.set("width", CSS.px(120))sets a width, andel.computedStyleMap().get("width").valuereads a number. There are no string splits or unit parsing, and it can be faster. It is supported in Chromium based browsers, and support elsewhere varies. - Paint API:
CSS.paintWorklet.addModule()loads a script that draws into a background or border image with a canvas-like context, which you then use asbackground: paint(name). It is mainly available in Chromium browsers, so a fallback background is needed. - Layout, Animation and Properties and Values worklets: more experimental, with limited or no support. Do not build features that depend on them.
Rules for use: treat Houdini features as progressive enhancement, test with CSS.supports() or by checking that the API exists, and keep a normal CSS fallback. A registered property with a fallback value is safe to use, while a paint worklet needs a plain background to show where it is not supported.
The Typed OM call sets a width of 120 pixels, and reading it back gives the number 120 with the unit px. The registered custom property shows its default of 8px. Whether the paint worklet exists depends on the browser.
Use Houdini where a fallback exists, and treat the rest as experiments.
<div id="el" style="width:50px;height:20px"></div>
<script>
CSS.registerProperty({ name: "--gap", syntax: "<length>", inherits: false, initialValue: "8px" });
el.attributeStyleMap.set("width", CSS.px(120));
window.typed = el.computedStyleMap().get("width");
window.gap = getComputedStyle(el).getPropertyValue("--gap");
window.hasPaint = "paintWorklet" in CSS;
</script>Checked in Chrome 156 only. Houdini features have uneven support, so check the support table for each one. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Houdini is a family of APIs, not one feature.
- @property and registerProperty define typed custom properties.
- Typed OM uses values such as CSS.px(120).
- The Paint API has limited support and needs a fallback.
- Feature-test before use.
Common mistakes
- Building a feature on a worklet that only some browsers support, with no fallback.
- Treating Houdini as one feature that is either supported or not.
- Parsing style strings by hand when Typed OM could give numbers.
Follow-ups
- What does registering a custom property add?
- Why does the Paint API need a fallback?
Tips
- Say that Houdini is a family of APIs with different levels of support.
- Name @property, Typed OM and the Paint API.