CSS Houdini is probably the most exciting advance in the CSS field. Houdini consists of multiple independent APIs, each delivered to the browser separately, and some APIs have been delivered (see this for browser support). The Paint API is one of them. I'm very interested in it and recently started thinking about how to use it at work.
One way I can do this is to use it as a way to avoid duplicate wheels. In this article, we will discuss this while comparing it to the methods we currently use in JavaScript and CSS. (I won't go into the depth of how to write CSS Houdini, as there are many excellent articles that have already been introduced to this, such as this, this and this one.)
Houdini brings modularity and configurability to CSS
The way CSS Houdini works brings two major advantages: modularity and configurability . Both methods can often make our lives easier as developers. We often see these concepts in the JavaScript world, but rarely in the CSS world… Until now.
The following table lists the workflow for some use cases, comparing traditional CSS with Houdini. I also added JavaScript for further comparison. You can see that CSS Houdini allows us to use CSS more effectively, similar to how the JavaScript world evolved into components.
Traditional CSS | CSS Houdini | JavaScript | |
---|---|---|---|
When we need common code snippets | Write from scratch or copy and paste from elsewhere. | Import a worklet. | Import a JS library. |
Customize code snippets based on use case | Manually adjust the values in CSS. | Edit custom properties exposed by the worklet. | Edit the configuration provided by the library. |
Code Sharing | Share the original style code with comments on how to adjust each section. | Share the worklet (in the future, share to the package management service) and record custom properties. | Share the library to a package management service (such as npm) and record how it is used and configured. |
Modular
With Houdini, you can import a worklet and start using it with a line of code.
<code>CSS.paintWorklet.addModule('my-useful-paint-worklet.js');</code>
This means there is no need to implement common styles every time. You can have your own set of worklets that can be used for any of your projects and can even be shared with each other.
If you want to provide modularity for HTML and JavaScript in addition to styles, Web Components is the solution.
This is very similar to what we already have in the JavaScript world. Most people don't reimplement common functions, such as throttling or deep copying objects. We just need to import libraries, such as Lodash.
If CSS Houdini becomes more popular, I can imagine we will have CSS Houdini package management service where anyone can import worklets for interesting waterfall layouts, background patterns, complex animations, and more.
Configurability
Houdini works well with CSS variables, which greatly enhances its own abilities. Using CSS variables, users can configure Houdini worklets.
<code>.my-element { background-image: paint(triangle); --direction: top; --size: 20px; }</code>
In this code snippet, --direction
and --size
are CSS variables that are used in triangle worklets (defined by the author of triangle worklets). Users can change properties to update how they appear, and even dynamically update CSS variables in JavaScript.
If we compare it again to what we already have in JavaScript, JavaScript libraries usually have options that can be passed. For example, we can pass values to the carousel library for speed, direction, size, etc. so that it will be performed the way we want it to. It is useful to provide these APIs at the element level in CSS.
Houdini workflow makes my development process more efficient
Let's look at a complete example of how the entire process works together to simplify development. We will use the tooltip design pattern as an example. I find myself using this pattern often on different websites, but somehow every new project has to be re-implemented.
Let's briefly review my previous experience:
- OK, I need a tooltip.
- It is a box with a triangle on one side. I will use pseudo-elements to draw triangles.
- I can use the transparent border trick to draw triangles.
- At this point, it is very likely that I will dig out my past projects to copy the code. Let me think about it... this needs to be pointed upward, which side is transparent?
- Oh, design borders that require tooltips. I have to use another pseudo-element and fake a border for the pointing triangle.
- What? They decided to change the direction of the triangle? ! OK, OK. I'll adjust all values of the two triangles...
This is not difficult. The whole process may only take five minutes. But let's see how to do better with Houdini.
I built a simple worklet to draw tooltips and provided a number of options to change its appearance. You can download it on GitHub.
Thanks to Houdini, here is my new process:
- OK, I need a tooltip.
- I'll import this tooltip worklet and use it.
- Now I will modify it with custom properties.
<code></code>
Here is a prompt ``` CSS.paintWorklet.addModule('my-tooltip-worklet.js') .tooltip-1 { background-image: paint(tooltip); padding: calc(var(--triangle-size) * 1px .5em) 1em 1em; --round-radius: 0; --background-color: #4d7990; --triangle-size: 20; --position: 20; --direction: top; --border-color: #333; --border-width: 2; color: #fff; }
<code>这是一个演示!继续尝试使用变量! CSS Houdini 打开了模块化、可配置样式共享的大门。我期待看到开发人员使用和共享CSS Houdini worklet。我正在尝试添加更多有用的Houdini 使用示例。如果您有任何想法或想为此存储库贡献代码,请与我联系。</code>
The above is the detailed content of CSS Houdini Could Change the Way We Write and Manage CSS. For more information, please follow other related articles on the PHP Chinese website!

If you've ever had to display an interactive animation during a live talk or a class, then you may know that it's not always easy to interact with your slides

With Astro, we can generate most of our site during our build, but have a small bit of server-side code that can handle search functionality using something like Fuse.js. In this demo, we’ll use Fuse to search through a set of personal “bookmarks” th

I wanted to implement a notification message in one of my projects, similar to what you’d see in Google Docs while a document is saving. In other words, a

Some months ago I was on Hacker News (as one does) and I ran across a (now deleted) article about not using if statements. If you’re new to this idea (like I

Since the early days of science fiction, we have fantasized about machines that talk to us. Today it is commonplace. Even so, the technology for making

I remember when Gutenberg was released into core, because I was at WordCamp US that day. A number of months have gone by now, so I imagine more and more of us

The idea behind most of web applications is to fetch data from the database and present it to the user in the best possible way. When we deal with data there

Let's do a little step-by-step of a situation where you can't quite do what seems to make sense, but you can still get it done with CSS trickery. In this


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.