Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for A Complete Guide To CSS Houdini
Anuraggharat651
Anuraggharat651

Posted on • Originally published atlambdatest.com

A Complete Guide To CSS Houdini

As a developer, checking thecross browser compatibility of your CSS properties is of utmost importance when building your website. I have often found myself excited to use a CSS feature only to discover that it’s still not supported on all browsers. Even if it is supported, the feature might be experimental and not work consistently across all browsers. Ask any front-end developer about using a CSS feature whose support is still in the experimental phase in most prominent web browsers. 🙁

However, some CSS features outshine others, even though they are in the experimental phase. In the end, developers would want to be early adopters of technology. Talking about early adoption, you should check out CSS Houdini. Like me, many front-end developers are super excited about it since it is considered the future of writing code with CSS. It is considered the most exciting development in CSS sinceCSS Flexbox and grid.

The first question that comes to mind is, “What is so special about CSS Houdini”? Well, it literally gives you the super-power through which you can have tighter control over the styling of web pages. So, let’s look at how you can do much more as a front-end developer with CSS Houdini!

There have been many advancements in HTML and JavaScript in the last few years. You can now extend HTML and create your components and custom properties. But CSS, there’s nothing much yet! As a developer, you have a lot of constraints while working with CSS. You can only use the properties supported by the browser and have limited control over the styling and layout of the webpage.

Imagine the possibilities if we are allowed to extend CSS using JavaScript. No restrictions and no rules! Extend CSS as you wish! Adding new features which will be performant and consistent across all browsers will be easy for developers.

Mobileemulators onlinefrom LambdaTest allows you to seamlessly test your mobile applications, websites,and web apps on mobile browsers and mobile devices.

But what will CSS Houdini do?

Let’s consider that you want to implement an angled border button in your website, but the existing browsers don’t support it. So instead of waiting for the browser to provide support, you can import the paint worklet directly into your project and apply the angled border and clipping property to your buttons without worrying about browser compatibility. This is as easy as installing a plugin for a feature. You can create a worklet by yourself or use one already created by other developers.

Like the paint worklet, we have a layout worklet to implement masonry layout, animation API to implement custom animations, and properties API to register custom properties with type checking.

In this blog, learn what CSS Houdini is, how it handles web page rendering issues, the CSS Houdini APIs, and some practical examples.

Introduction to CSS Houdini

CSS Houdini is an umbrella term for browser APIs that expose certain parts of the CSS rendering engine to the developers. This allows developers to extend CSS without compromising on performance. We can add new features using JavaScript and share them as project modules. Hence developers are no longer restricted to using only the properties supported by the browser.

For the first time, the developers can extend the various stages present in the browser’s rendering pipeline.

The main goal of CSS Houdini is to present a set of new APIs to the developers so that they can attach (hook) their code in the CSS engine. This allows the developers to extend the existing CSS with features not yet available on some browsers.

You might wonder what CSS Houdini has to do with your small website.

Think of the bigger picture!

Wouldn’t it be nice if you could get more control over the rendering of your website, use the latest features in CSS without worrying about cross browser compatibility, and import CSS features just like you import modules in JavaScript? This is what Houdini is trying to achieve!

A Houdini task force comprised engineers from top companies like Google, Microsoft, Apple, HP, and IBM. The aim of this task force was simple:

How can we improve the developer experience by giving them better control of the rendering process of the web page?

The ultimate goal was to give developers more control over the browser’s rendering process and improve CSS capabilities without compromising performance.

They achieved this by exposing parts of the CSS engine, allowing developers to build APIs that hook directly into the browser. I have covered the same in the section where the focus is on the problems Houdini is trying to solve. By opening up these parts, developers can extend the stages in the pipeline with JavaScript and add new features.

So, which are these parts? I will dive deep into it in the subsequent sections of the blog.

Before we dive deeply into CSS Houdini, let’s understand why we need CSS Houdini in the first place and what problem it is trying to solve.

NewSelenium IDEsupports cross browser testing and parallel tests for automation along with record and replay function. Here’s why you shouldn’t miss out on it.

Why use CSS Houdini?

You might have noticed that it has taken a lot of time for a CSS feature to be available in all browsers since its announcement. Alas,cross browser compatibility issues arise since not all the web browsers (and browser versions) support all the available CSS properties (including the ones in the experimental phase).

In JavaScript, features are added and adopted very quickly, but in CSS, it’s the exact opposite. Ask any CSS developer 😣 It takes a lot of time to propose a new feature, approve it, and wait for all browsers to adopt it and then use it. It took years for our favoriteCSS Grid to be available in all browsers.

The other problem is that you might want to use a feature, but it might not be supported in all browsers. In such a case, you choose another workaround which is CSS polyfill.

Polyfill is a piece of JavaScript code that provides modern features in the older browser that lack support. It acts as a browser fallback for missing properties in older browsers. For example, a polyfill code can provide the CSS outline property in Internet Explorer 7, where the property is not supported.

As interesting as CSS polyfill sounds, it has a lot of drawbacks.

The main downside of using a CSS polyfill is that it negatively impacts the browser’s performance. The polyfill code is never as efficient as the native CSS code. Hence it can never work as a replacement.

To understand the drawback and why CSS Houdini is important, we must first know how a browser loads a web page.

Role of Browser Rendering Pipeline

The below image depicts various stages present in thebrowser rendering engine pipeline. A web page goes through all these stages before it is displayed on the screen.

Let’s understand each stage in detail

image

DOM and CSSOM

Whenever a request is sent from the browser, the server returns a web page. This web page is an HTML document. While loading this web page, the browser scans the HTML code and generates a JavaScript object called a Node for every HTML element.

For example, every ‘div’ generates a JavaScript object from theHTMLDivElement class function. After the browser has gone through the entire page, it builds a tree-like structure of the nodes from the entire document. This tree-like structure is called aDOM (Document Object Model).

image

JavaScript has access to this DOM with the help of DOM APIs. Using these APIs, a developer can manipulate the DOM.

AutomatedFunctional Testingtests helps to ensure that your web app works as it was intented to. Learn more about functional tests, and how automating them can give you a faster release cycle.

![image](https://cdn-images-1.medium.com/max/800/0*bYRjzkzBOQrcwxZF.png

Here’s a simple example of how we can change the text inside the< p > tag using JavaScript.

HTML:

<div>    <p>Hi, there!</p>    <button>        Change Text    </button></div>
Enter fullscreen modeExit fullscreen mode

#"text").innerHTML = "Hello World!";}

Enter fullscreen modeExit fullscreen mode

Output:

<div>  <p>Hi, there!</p>  <button>    Change Text  </button></div>
Enter fullscreen modeExit fullscreen mode

After reading the HTML file and constructing the DOM, the browser collects all the CSS styles from all the existing sources and builds aCSSOM (Cascading Style Sheet Object Model).

image

CSSOM is a CSS representation of DOM. Each node in CSSOM contains the style that will be applied to that particular node in DOM that theCSS Selector is targeting.

First, a final CSS is generated by considering the specificity (overriding some styles if they are repeated); only then is the CSSOM created. CSSOM does not contain elements that are not printed on the screen.

Render Tree

In the third step, we combine the DOM and CSSOM to form a render tree. While creating the render tree, the browser checks every node from the root and determines the styles attached to it. Only the elements that are supposed to be present on the screen are added in the render tree nodes.

If there’s a node with a property display set as none, that node and its descendants are not added to the render tree. Similarly, the head section of the web page contains no visible information and hence is not included in the render tree.

image

Layout

The layout operation is responsible for calculating the position and dimensions of each node from the render tree. The layout is the process by which the width, height, and location of all nodes in the render tree are determined. In this step, the browser determines the width, height, location, and geometry of each node present in the render tree and creates a layout.

Layout operation is also calledbrowser reflow. This operation runs every single time the browser viewport is changed.

Through thisusability testingtutorial, you will learn how usability testing is a great way to discover unexpected bugs, find what is unnecessary or unused before going any further, and have unbiased opinions from an outsider.

Paint

Up until now, the browser has created layers of each element separately. In paint operation, the browser works on each layer and fills them with visual elements like borders, colors, backgrounds, fonts, shadows, etc.

This process is performed on each layer separately. Paint operation is also called rasterization.

Composition

In this step, we combine all the layers generated till now and draw them on the screen as a single web page. After the composition operation, the web page is displayed on the browser.

This sequence of events is called thecritical rendering path.

So let’s quickly summarise

  • DOM is generated from the HTML code.

  • CSSOM is created based on the stylesheets (Internal and External).

  • DOM and CSSOM are combined to form the render tree.

  • Layers are created based on the render tree in the layout operation.

  • Visual factors are added to each layer in the paint operation.

  • Layers are combined in the composition process and presented on the screen.

Once all the critical rendering path steps are completed, JavaScript attached to the code is loaded. However, there is a slight issue with the web page rendering that we typically face. In the next section of this blog, we will explore the issues with the rendering of a web page.

What’s the issue with the rendering of a web page?

image

As seen above, a developer has limited access to DOM and CSSOM. (The degree of access to CSSOM is unspecified in the official docs)

So if you ever have to write a polyfill for CSS, you cannot plug it into CSSOM directly since you don’t have access to it. You can only plug it inside the DOM.

image

This means you have to update the DOM, resulting in a re-rendering of the complete web page. The browser will reinitiate the rendering process whenever you make any DOM changes.

Re-rendering your website once or twice after you initially load it might sound like a small dent in your website performance. But consider a case where you have written a polyfill that will run every time you scroll. This will make your webpage load multiple times, significantly affecting performance.

For example, let’s say you created a CSS smooth scroll polyfill function that bringsCSS smooth scroll property in old browsers and Internet Explorer. This polyfill function runs every time the browser detects scroll. This makes the web page reload whenever the function is invoked, hence affecting the performance.

So we need to find a way to modify or extend the CSS without triggering the re-render. That means modifying the rendering cycle without touching the DOM.

Black Box testing? Don’t worry; we will be covering what is Black box testing, merits, demerits, types & techniques.

How does CSS Houdini handle web page rendering issues?

Houdini introduces a new set of APIs that lets the developers access the parts of the browser rendering pipeline. For the first time, you will have access to the layout, painting, and other processes of the website. Developers can go above the regular CSS styling and create their properties, layout, and animations.

This extension of the CSS engine is possible due to CSS Houdini APIs. CSS Houdini is a set of APIs that expose parts of the CSS engine.

CSS Houdini APIs

CSS Houdini consists of two sets of APIs.

  1. Low-Level APIs

  2. High-Level APIs

These APIs (low-level and high-level) make it easier for developers to create extensions for CSS. These extensions might be to polyfill features that are not yet available in a browser, experiment with new layout methods, or add creative borders or other effects.

These extensions act as polyfill to provide features that are not yet available in the browser. Using these APIs, developers extend the stages of the CSS rendering pipeline by adding custom JavaScript code and creating custom features like layouts, borders, backgrounds, etc.

image

Low-Level APIs

These APIs form the foundation of the High-level APIs.

  1. Worklets

  2. Typed Object Model API

  3. Custom Properties API

  4. Font Metrics API

High-Level APIs

These High-level APIs represent the four stages of the browser’s rendering pipeline.

  1. Paint API

  2. Layout API

  3. Animation Worklet API

  4. Parser API

As of November 2022, only paint API, custom properties API, and typed object model API is supported in Chromium-based browsers and will be available soon in Firefox and Safari. So only use these supported browsers to check the output of the explained examples.

Following is the browser support for Houdini APIs.

image

Source

A comprehensive UserAcceptance Testing(UAT) tutorial that covers what User Acceptance Testing is, its importance, benefits, and how to perform it with real-time examples.

Worklets

A worklet is a lightweight version of aweb worker and acts as anextension point of the browser rendering engine for the developers. Each worklet runs in a separate context and has no access to the window, document objects, or high-level functions.

Worklets run independently of the main thread and can be invoked at any point of the rendering pipeline. Worklets are the JavaScript modules that attach themselves to the browser and make the high-level APIs work. They are imported and registered using a single line of JavaScript.

Currently, CSS Houdini supports three worklets.

  1. Paint Worklet

  2. Layout Worklet

  3. Animation Worklet

Importing a paint worklet:

CSS.paintWorklet.addModule("url-of-worklet");
Enter fullscreen modeExit fullscreen mode

Using an imported worklet:

    body{     background-color: paint(confetti)     }
Enter fullscreen modeExit fullscreen mode

Typed Object Model API

CSS values come with a variety of units. We havepx,rem,em,ch,cm, and more! Interacting with so many units is difficult for any developer. Currently, the only way for developers to read and modify CSS values using JavaScript is by parsing them into Strings. This makes it difficult to perform arithmetic operations, and code is more prone to errors.

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>Lambda Test</title></head><body>    <section>        <div>            <h1>Lambda Test</h1>        </div>    </section></body></html><script>    var heading = document.getElementById('heading')    console.log(heading.style.height)    console.log(typeof heading.style.height)</script>
Enter fullscreen modeExit fullscreen mode

Output:

image

As you can see in the above example, the 100px value is a string. So to add any value to it, we need first to parse it into an Integer, perform our operation, and convert it back to a String. This gets even tougher when the unit used is relative, like %, vh/vw.

Typed Object Model API adds moresemantics to the CSS by exposing them as JavaScript objects. Hence instead of taking a CSS value and parsing it into a String, Typed OM APIs provide you with aJavaScript Object withvalue andunit property. This approach is faster, and the code is more maintainable and less prone to errors.

Using computedStyleMap() we get an object which consists of the value and the unit. It is easier to perform the calculations and work with a numerical value.

Here is how we use Typed Object Model API.

    //select the container using javascriptvar elem = document.getElementById('container')console.log("Using Typed OM ")console.log("Height : " + elem.computedStyleMap().get("height"));console.log("Type of Height = " + typeof elem.computedStyleMap().get("height").value);
Enter fullscreen modeExit fullscreen mode

Output:

image

computedStyleMap() method is used for parsing computed properties and attributeStyleMap is used for parsing and modifying inline styles.

Typed Object Model API performs type checking. So if you pass a wrong data type, it will show an error in the console.

Here’s an example showing how easy it is to interact with CSS properties using the Typed Object Model API.

HTML:

<body>    <div>        <h1>Typed OM using CSS Houdini</h1>        <p>Open the console log</p>    </div></body>
Enter fullscreen modeExit fullscreen mode

CSS:

body {    background-color: #fefefe;    display: flex;    justify-content: center;    align-items: center;}h1 {    font-family: 'Courier New', Courier, monospace;    font-size: 30px;}div {    display: flex;    flex-direction: column;    justify-content: center;    align-items: center;    height: 400px;    width: 800px;}
Enter fullscreen modeExit fullscreen mode

#"container");elem.style.height = 500;console.log(typeof elem.style.height);//String//getting styles using Typed OMconsole.log(elem.computedStyleMap().get("height"));console.log(elem.computedStyleMap().get("height").value);console.log(typeof elem.computedStyleMap().get("height").value);console.log(elem.computedStyleMap().get("height").unit);//setting styles using Typed OMvar elemTwo = document.getElementById("heading");elemTwo.attributeStyleMap.set("font-size", CSS.px(50));//getting inline-stylesconsole.log(elemTwo.attributeStyleMap.get("font-size"));//an error will be thrown if we pass a non string value//elemTwo.attributeStyleMap.set('font-size', CSS.px(asd))//Clear all the stylesconsole.log(elemTwo.attributeStyleMap.clear);//delete font-size styleconsole.log(elemTwo.attributeStyleMap.delete("font-size"));//check if style is presentconsole.log(elemTwo.attributeStyleMap.has("font-size"));

Enter fullscreen modeExit fullscreen mode

Output:

image

<div>        <h1>Typed OM using CSS Houdini</h1>  <p>Open the console log</p>    </div>
Enter fullscreen modeExit fullscreen mode

Here I am usingLT Browser — a complementary tool by LambdaTest, to test my output.

LT Browser is amobile-friendly tester offered by LambdaTest — a continuous quality cloud testing platform for manual andautomated testing for web and mobile. With LT Browser, you can test your website’s responsiveness on over 50+ device viewports. It supportsresponsive testing on the mobile, tablet, desktop, and even laptops.

Check out the following tutorial for more details.

Subscribe to theLambdaTest YouTube channel for tutorials aroundSelenium testing,Playwright,Cypress, and more.

LT Browser provides tons of amazing features like

  • Check the mobile view of your websites.

  • Compare the side-by-side view of the website on different viewports

  • Generate performance reports of your websites.

  • Network simulation to test websites across varying networks.

  • Perform one-click bug logging and video recording of test sessions.

We recently went live with the beta version of our all-newChromium-based LT Browser 2.0, which has several new features and enhancements.

Run yourSelenium onlineAutomation Testing scripts on the LambdaTest cloud grid. Test on 3000+ desktop & mobile environments. Try it for free.

Along with the Object, we also get useful methods likeclear, delete, andhas.

  //Clear all the styles  console.log(elemTwo.attributeStyleMap.clear)  //delete font-size style      console.log(elemTwo.attributeStyleMap.delete('font-size'))  //check if style is present          console.log(elemTwo.attributeStyleMap.has('font-size'))
Enter fullscreen modeExit fullscreen mode

Custom Properties and Values API

Custom properties and values API allows developers to create CSS properties withdata types,initial values, andinheritance rules. Developers can register these properties using the ‘registerProperty()’ method or the‘@property’ rule.

CSS Custom Properties API extends the CSS variables and adds Type checking. You can add a variable in the ‘:root{}’ block and use it throughout your CSS file.

  :root {   --primary-color: blue;}body {    background-color: var(--primary-color);}
Enter fullscreen modeExit fullscreen mode

One of the downsides of using a regularCSS variable is that there is no type checking. So if the browser encounters a value that does not match the expected property value type. It directly discards the property.

The other drawback of using a regular CSS variable is that we can’t animate the property. The browser does not know what the expected type of the property is hence it is unable to handle the animations. But registered properties allow us to handleCSS animations since they come with a type.

You can create yourcustom CSS property in two ways:

  • Using theregisterProperty() method in your JavaScript file.
  CSS.registerProperty({    name: "name-of-property",    syntax: "<property-type>",    inherits: false / true,    initialValue: "initial-value",  });
Enter fullscreen modeExit fullscreen mode
  • Using@property in your CSS file
  @property --property-name {      syntax: "<property-type>";      inherits: false/true;      initial-value: initial-value;  }
Enter fullscreen modeExit fullscreen mode

Let’s take an example to understand CSS custom property. In this example, we will create two custom properties. One with theregisterProperty() method and another with the@property rule.

HTML:

<body>    <h1>CSS CUSTOM PROPERTY</h1></body>
Enter fullscreen modeExit fullscreen mode

CSS:

@property --primaryColor {    syntax: "<color>";    inherits: false;    initial-value: teal;}body {    height: 100vh;    width: 100%;    display: flex;    justify-content: center;    align-items: center;    background-color: var(--primaryColor);    color: var(--textColor)}
Enter fullscreen modeExit fullscreen mode

#"--textColor", syntax: "<color>", inherits: false, initialValue: "orange",});

Enter fullscreen modeExit fullscreen mode

Output:

image

<body>  <h1>CSS CUSTOM PROPERTY & VALUES API</h1></body>
Enter fullscreen modeExit fullscreen mode

Font Metrics API

According to the documentation, font metrics API will contain methods that will allow the developers to measure thedimensions of the textual elements that are being rendered on the screen. For example, using this API, you can perform text truncation on multiple line text elements.

Currently, this API is still in a very early stage of development, so there is a high chance that the specifications and features may change.

Run yourSelenium Automation Testingscripts on the LambdaTest cloud grid. Test on 3000+ desktop & mobile environments. Try it for free.

Paint API

CSS paint API allows developers to generate graphics using JavaScript functions during the Paint stage of the browser’s rendering pipeline. This is achieved with the help of Paint Worklet. In the Background, Paint API uses theCanvasRenderingContext2D, which is the subset ofHTML5 Canvas API.

Using a paint worklet, you can create custombackgrounds,borders,outlines, and more! You can define your own custom painting functions or use the existing worklets created by other developers.

This API has the best support and is enabled in all the Chromium-based browsers, under consideration for Firefox and in the development stage for Safari. You can use thepaint() function as a value to the CSS properties and pass the registered Paint worklet.

Here the developers define the paint functions and use them as CSS properties in their CSS file.

Steps

  1. Create your own worklet using the registerPaint() method or link an already created worklet using CDN.
registerPaint("name-of-worklet", {  //..code});
Enter fullscreen modeExit fullscreen mode
  1. Register and add it to your project using theaddModule() method.
CSS.paintWorklet.addModule(“path-to-worklet.js-file”);
Enter fullscreen modeExit fullscreen mode
  1. Use thepaint() function as a CSS value to the background property.
.demo {    background: paint(name-of-worklet);}
Enter fullscreen modeExit fullscreen mode
  1. Add a polyfill script to ensure the worklet runs on all old browsers.
<script src="cdn-link-to-polyfill-file.js"></script>
Enter fullscreen modeExit fullscreen mode

So, let’s take an example and show you the possibilities of paint API. For this example, we will use already created worklets. There are lots of cool worklets to choose from. I loved the static gradient one, so I’ll use that.

HTML:

<section>    <div>        <h1>PAINT WORKLET - CSS Houdini</h1>    </div></section><script src="https://unpkg.com/Houdini-static-gradient/worklet.js"></script>
Enter fullscreen modeExit fullscreen mode

CSS:

body {    padding: 0;    margin: 0;    box-sizing: border-box;    background-color: #222;}section {    height: 100vh;    width: 100%;}div {    width: 100%;    height: 100%;    display: flex;    align-items: center;    justify-content: center;}.demo {    background: paint(static-gradient);    --static-gradient-size: 5;    --static-gradient-color: #7743DB;    --static-gradient-direction: to-bottom;}h1 {    font-size: 3rem;    color: #fff;}
Enter fullscreen modeExit fullscreen mode

#"http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24">Enter fullscreen modeExit fullscreen mode

Output:

<section>        <div>            <h1>PAINT WORKLET - CSS Houdini</h1>        </div>    </section><script src="https://unpkg.com/houdini-static-gradient/worklet.js"></script>
Enter fullscreen modeExit fullscreen mode

You can get a lot creative with the paint API. The possibilities are limitless. Check out these two examples that I created using paint API. I went a step ahead and added keyframes animation to it.

Example 1

Here is the code for our first example.

HTML:

<section>    <div>        <h1>CSS Houdini is Awesome!</h1>    </div></section><script src="https://unpkg.com/extra-confetti/worklet.js"></script>
Enter fullscreen modeExit fullscreen mode

CSS:

 body {        padding: 0;        margin: 0;        box-sizing: border-box;        background-color: #222;    }    section {        height: 100vh;        width: 100%;        display: flex;        align-items: center;        justify-content: center;    }    div {        width: 1000px;        height: 500px;        background-color: aquamarine;        display: flex;        align-items: center;        justify-content: center;    }    .demo {        background: paint(extra-confetti);        --extra-confettiNumber: 20;        --extra-confettiLengthVariance: 20;        --extra-confettiWeightVariance: 10;        animation: animateConfetti 1s infinite;    }    h1 {        font-size: 5rem;        color: #fff;    }    @keyframes animateConfetti {        0% {            --extra-confettiNumber: 20;        }        50% {            --extra-confettiNumber: 60;        }        100% {            --extra-confettiNumber: 100;        }    }
Enter fullscreen modeExit fullscreen mode

#"https://unpkg.com/extra-confetti/worklet.js");

Enter fullscreen modeExit fullscreen mode

Output:

image

<section>        <div >            <h1>CSS Houdini Paint API Example!</h1>        </div>    </section>   <script src="https://unpkg.com/extra-confetti/worklet.js"></script>
Enter fullscreen modeExit fullscreen mode

Example 2

Now let’s look at the code for our second example.

HTML:

<section></section><script src="https://unpkg.com/Houdini-alien@0.1.1/AlienPainter.js"></script>
Enter fullscreen modeExit fullscreen mode

CSS:

 body {        padding: 0;        margin: 0;        box-sizing: border-box;    }    section {        height: 100vh;        width: 100%;        display: flex;        align-items: center;        justify-content: center;    }    .demo {        background-image: paint(alien);        --alien-bg-color: #222;        --alien-fg-color: #f321f1;        --alien-grid-size: 2;        animation: animateAlien 2s infinite;    }    @keyframes animateAlien {        0% {            --alien-grid-size: 2;        }        25% {            --alien-grid-size: 3;        }        50% {            --alien-grid-size: 4;        }        75% {            --alien-grid-size: 3;        }        100% {            --alien-grid-size: 2;        }    }
Enter fullscreen modeExit fullscreen mode

#"https://unpkg.com/Houdini-alien@0.1.1/AlienPainter.js");

Enter fullscreen modeExit fullscreen mode

Output:

image

<section>    </section>    <script>        CSS.paintWorklet.addModule('https://unpkg.com/houdini-alien@0.1.1/AlienPainter.js')    </script><script src="https://unpkg.com/houdini-alien@0.1.1/AlienPainter.js"></script>
Enter fullscreen modeExit fullscreen mode

Layout API

CSS layout API allows the developers to create their ownDisplay properties. This API extends the layout stage of the browser’s rendering pipeline. Layout API will make creating new and complex layouts easy for developers.

Similar to the paint API, the layout worklet needs to be registered before it can be used.

registerLayout(  "mylayout",  class {    //code for the layout  });
Enter fullscreen modeExit fullscreen mode

Once registered, you can add the layout API in your HTML file using the add method.

CSS.layoutWorklet.addModule('path-to-layout-worklet');
Enter fullscreen modeExit fullscreen mode

Finally, use the layout function against the display property in your CSS file.

.container {    display: layout(mylayout);}
Enter fullscreen modeExit fullscreen mode

Currently, layout API is not supported in all browsers; hence we won’t be able to see a working demo.

Animation API

Animation API improves the performance of web animations by running them on their own worklet. This API is the extension of theComposite stage of the browser’s rendering pipeline. The API allows the developers to generate KeyframeEffect animations based on user inputs like ascroll,hover, andclick. These animations are non-blocking and more performant than the existing ones since they run off the main thread(on their own worklet).

Similar to the other worklets, the animation worklet needs to be registered first.

registerAnimator(  "myanimation",  class {    //code for custom animation  });
Enter fullscreen modeExit fullscreen mode

Then add it as a module in your main JavaScript file.

CSS.animationWorklet.addModule("path-to-animation-worklet");
Enter fullscreen modeExit fullscreen mode

Animation API has partial support inChromium-based browsers and is not supported by Firefox and Safari, so we won’t be able to see a working demo.

ThisCypress test automationtutorial will help you learn the benefits of Cypress automation, and how to install Cypress and execute Cypress automation testing over scores of browsers and operating systems online.

Parser Worklet

Parser worklet extends the parsing stage of the browser’s rendering pipeline and is built on top of the Typed Object Model. The goal of this API is to allow the Developer access to the Rendering Engine’s Parser. With this API, you can create new rules, performnesting,extend CSS and applyexternal properties.

Currently, this API is not implemented; hence the specifications aren’t clear. But this API has a massive scope since developers can extend the CSS parsing phase and directly tell the browser what CSS property follows.

Conclusion

I am very excited about CSS Houdini APIs. How cool is it that the developers get access to all the rendering pipeline stages? This means more control and power in the hands of the developers. All these new APIs and worklets have opened a new world of possibilities. You are now not restricted to the browser-supported CSS properties. You can create your own CSS property polyfills or use the ones already created without worrying about browser compatibility. This will make the developer experience a lot better.

CSS Houdini is still in the early stages of development but has shown the potential to change how developers write the styles. This is still the surface; there are a lot more APIs that are yet to be announced. As a developer, the CSS Houdini project is one of the most exciting projects to look forward to in the upcoming years.

I hope you learned something new and exciting from this blog. CSS has always been a tricky topic for all developers. So many new features are coming up, and it is vital to stay updated on them. It’s time to use some of the APIs mentioned above in your projects!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Anuraggharat651
Anurag Gharat is a Full Stack Developer, Freelancer, and Technical Writer based in India. He is currently working as a Software Engineer in one leading IT services company.
  • Joined

More fromAnuraggharat651

Mastering CSS Units: A Complete Guide
#css#softwaretesting#automationtesting
Introduction to Tailwind CSS: A Complete Guide
#tailwindcss#css#automationtesting#softwaretesting
An Intuitive Guide To CSS Glassmorphism
#css#glassmorphism#guide#automationtesting
DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp