When I was doing an internship on a mobile project, I implemented an animation effect. There was no problem debugging it on iPhone and Chrome, but it was a huge problem when testing it on an Android machine worth about a thousand yuan. The lag was very obvious, and I was puzzled and vomited blood. , died.
This sad story is the introduction of this article. I really understand that CSS has a very obvious impact on user experience, and a little carelessness can lead to a big pitfall. Next, let’s talk about CSS performance optimization.
Loading performance
Reduce file size, compress code
Reduce blocking loading, don’t use import
to improve concurrency (I don’t quite understand this)
Selector performance
The impact on overall performance is negligible, but the focus on selectors is more for standardization, maintainability, and robustness. For specific implementation, please refer to this sharing from Github: GitHub's CSS Performance by Jon Rohan
Rendering performance
Rendering performance is the most important focus of CSS optimization. Let’s first understand the browser’s rendering mechanism.
Browser rendering mechanism
The process of browser rendering and displaying web pages is roughly divided into the following steps:
1. Parse HTML (HTML Parser)
2. Build DOM Tree (DOM Tree)
3. Render Tree Construction (Render Tree)
4. Painting Render Tree (Painting)
Choose high-cost styles carefully
What CSS properties are high-cost? It is those properties that require the browser to perform a lot of calculations before drawing.
box-shadows
border-radius
transparency
transforms
CSS filters (performance killer)
Avoid excessive reflow (Reflow)
Simple Explain Reflow: When an element changes, it will affect the document content or structure, or the position of the element. This process is called Reflow.
How to reduce Reflow
1. Don’t modify the DOM style one by one, define the class in advance, and then modify the className of the DOM 2. Modify the DOM offline, for example: first DOM for display :none (one Reflow), then you modify it 100 times, and then display it
3. Don’t put the attribute values of the DOM node in a loop as variables in the loop
4. Try not to modify it as much as possible DOM with a relatively large impact range
5. Use absolute positioning for animated elements absolute / fixed
6. Do not use table layout, a small change may cause the entire table to be re-layout
Avoid excessive redrawing (Repaints)When the element changes, it will not affect the position of the element on the page (such as background-color, border-color, visibility). The browser will only apply the new style to redraw the element. This process It's called Repaint.
CSS3 animation is the top priority for optimization. In addition to the above two points,
reduce Reflow and Repaints, you also need to pay attention to the following aspects.
Enable GPU hardware accelerationGPU (Graphics Processing Unit) is image processor. GPU Hardware acceleration refers to applying the graphics performance of GPU to hand over some graphics operations in the browser to the GPU to complete, because GPU is specially designed for processing graphics , so it has speed and energy consumption More efficient. It should be noted that turning on hardware acceleration will also have additional overhead. See this article The Good and Bad of CSS Hardware Acceleration
GPU acceleration can be applied not only to 3D, but also to 2D. Here, GPU acceleration usually includes the following parts: Canvas2D, Layout Compositing , CSS3 transformations , CSS3 3D transformations , WebGL and Video ( video). /*
* 根据上面的结论
* 将 2d transform 换成 3d
* 就可以强制开启 GPU 加速
* 提高动画性能
*/div { transform: translate(10px, 10px);}div { transform: translate3d(10px, 10px, 0);}

In this week's roundup: Firefox gains locksmith-like powers, Samsung's Galaxy Store starts supporting Progressive Web Apps, CSS Subgrid is shipping in Firefox

In this week's roundup: Internet Explorer finds its way into Edge, Google Search Console touts a new speed report, and Firefox gives Facebook's notification

You’re probably already at least a little familiar with CSS variables. If not, here’s a two-second overview: they are really called custom properties, you set

Building websites is programming. Writing HTML and CSS is programming. I am a programmer, and if you're here, reading CSS-Tricks, chances are you're a

Here's what I'd like you to know upfront: this is a hard problem. If you've landed here because you're hoping to be pointed at a tool you can run that tells

Picture-in-Picture made its first appearance on the web in the Safari browser with the release of macOS Sierra in 2016. It made it possible for a user to pop

Gatsby does a great job processing and handling images. For example, it helps you save time with image optimization because you don’t have to manually

I learned something about percentage-based (%) padding today that I had totally wrong in my head! I always thought that percentage padding was based on the


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version
God-level code editing software (SublimeText3)