【Editor's Note】In fact, there are many articles about web page rendering, but the relevant information is scattered and the discussion is not very complete. If we want to have a general understanding of this topic, we still have to learn a lot. Therefore, web developer Alexander Skutin decided to write an article. He believes that this article can not only help beginners, but also be beneficial to advanced front-end developers who want to refresh their knowledge structure. The original address
The translation is as follows:
Web page rendering must be carried out at a very early stage, which can be as early as the page layout has just been finalized. Because styles and scripts will have a critical impact on web page rendering. Therefore, professional developers must know some techniques to avoid encountering performance problems in practice.
This article will not study the detailed mechanism inside the browser, but propose some general rules. After all, different browser engines work differently, which will undoubtedly make developers' research on browser features more complicated.
How does the browser complete web page rendering?
First, let’s review the actions of the browser when rendering a web page:
form the Document Object Model (DOM) based on the HTML code from the server side
load and parse styles to form the CSS object model.
On top of the document object model and CSS object model, create a rendering tree consisting of a set of objects to be rendered (in Webkit these objects are called renderers or rendering objects, and in Gecko they are called "frame".) The render tree reflects the structure of the document object model, but does not contain invisible elements such as tags or with the display:none attribute. In the render tree, each text string is represented by an independent renderer. Each render object contains its corresponding DOM object, or text block, plus calculated styles. In other words, the render tree is a visual representation of the document object model.
For each element in the rendering tree, calculate its coordinates, which is called layout. Browsers use a flow approach, laying out an element in one pass, but table elements require multiple passes.
Finally, the elements on the rendering tree are finally displayed in the browser. This process is called "painting".
When the user interacts with the web page, or the script program changes and modifies the web page, some of the operations mentioned above will be performed repeatedly because the internal structure of the web page has changed.
Redraw
When changing the style of elements that do not affect the position of the element in the web page, such as background-color (background color), border-color (border color), visibility (visibility), the browser will only Redraw the element with the new style (this is redrawing, or re-structuring the style).
Reflow
Reflow or rearrangement occurs when changes affect text content or structure, or element positioning. These changes are usually triggered by the following events:
DOM operations (element addition, deletion, modification, or change of element order);
Content changes, including text changes within form fields;
CSS property calculations or changes;
Add or remove stylesheets;
Change properties of "classes";
Manipulation of browser windows (zoom, scroll);
Pseudo-class activation (:hover).
How does the browser optimize rendering?
Browsers try to limit redraw/reconstruction to the area of the changed element as much as possible. For example, for an element with a fixed or absolute position, the size change only affects the element itself and its sub-elements. However, the size change of a statically positioned element will trigger the reflow of all subsequent elements.
Another optimization technique is that when running several pieces of JavaScript code, the browser will cache these changes, and then apply these changes in a single pass after the code has finished running. For example, the following code will only trigger a refactor and repaint:
var $body = $('body'); $body.css('padding', '1px'); // reflow, repaint $body.css('color', 'red'); // repaint $body.css('margin', '2px'); // reflow, repaint // only 1 reflow and repaint will actually happen
However, as mentioned before, changing the properties of the element will trigger a forced reflow. This behavior will happen if we add a line of code to the above code block to access the element's properties.
var $body = $('body'); $body.css('padding', '1px'); $body.css('padding'); // reading a property, a forced reflow $body .css('color', 'red'); $body.css('margin', '2px');
The result is that the reflow occurs twice. Therefore, you should group operations that access element attributes together to optimize web page performance. (You can find more detailed examples on JSBin)
Sometimes, you have to trigger a forced reflow. For example, we must assign the same attribute (such as left margin) to the same element twice. Initially, it should be set to 100px with no animation. Next, it must be changed to 50px via a transition animation. You can follow this example on JSbin right now, but I'll cover it in more detail here.
First, we create a CSS class with transition effect: .has-transition { -webkit-transition: margin-left 1s ease-out; -moz-transition: margin-left 1s ease-out; -o-transition: margin-left 1s ease-out; transition: margin-left 1s ease-out; }
Then continue: // our element that has a "has-transition" class by default var $targetElem = $('#targetElemId' );
// remove the transition class $targetElem.removeClass('has-transition'); // change the property expecting the transition to be off, as the class is not there // anymore $targetElem.css('margin-left', 100); // put the transition class back $targetElem.addClass('has-transition'); // change the property $targetElem.css('margin-left', 50);
However, this execution fails to work. All changes are cached and only executed at the end of the code block. What we need is a forced reordering, which we can achieve with the following changes:
// remove the transition class $(this).removeClass('has-transition');
// change the property $(this).css('margin-left', 100); // trigger a forced reflow, so that changes in a class/property get applied immediately $(this)[0].offsetHeight; // an example, other properties would work, too // put the transition class back $(this).addClass('has-transition'); // change the property $(this).css('margin-left', 50);
Now the code executes as expected .
Practical Advice on Performance Optimization
Summarizing the available information, I offer the following advice:
Create valid HTML and CSS files, and don’t forget to indicate how the document is encoded. Styles should be included within the tag, and script code should be added at the end of the tag.
Simplify and optimize CSS selectors as much as possible (this optimization method is almost uniformly ignored by developers using CSS preprocessors) and keep nesting to a minimum. Here is the performance ranking of CSS selectors (starting with the fastest)
1. 识别器:#id 2. 类:.class 3. 标签:div 4. 相邻兄弟选择器:a + i 5. 父类选择器:ul> li 6. 通用选择器:* 7. 属性选择:input[type="text"] 8. 伪类和伪元素:a:hover
You should remember that browsers process selectors from right to left, so the rightmost selector should be the fastest: #id or .class: div * {...} // bad .list li {...} // bad .list-item {...} // good #list .list-item {... } // good * 1. In your script code, reduce DOM operations as much as possible. Cache everything, including element attributes and objects if they are reused. When performing complex operations, it is better to use "isolated" elements, which can be added to the DOM later (the so-called "isolated" elements are elements that are separated from the DOM and only stored in memory).
2. If you use jQuery to select elements, please follow jQuery selector best practices.
3. In order to change the style of an element, modifying the "class" attribute is one of the effective methods. When performing this change, the deeper you are in the DOM rendering tree, the better (it also helps to decouple logic from presentation).
4. Try to only add animation effects to elements with absolute or fixed positions.
5. Disable complex hover animations when using scrolling (for example, add an additional non-hover class in). Readers can read an article on this issue.
If you want to know more details, you can also read these two articles:
1, How browsers work?
2,Rendering: repaint, reflow/relayout, restyle

前端开发趋势总是在不断发展,有些趋势会长期流行。本篇文章给大家总结了2023 年将突出的一些前端开发趋势,分享给大家~

昨天刚发了一篇Python桌面开发库大全的微头条,就被同事安利了Flet这个库。这是一个非常新的库,今年6月份才发布的第一个版本,虽然很新,但是它背靠巨人-Flutter,可以让我们使用Python开发全平台软件,虽然目前还不支持全平台,但是根据作者的计划,Flutter支持的,它以后都会支持的,昨天简单学习了一下,真的非常棒,把它推荐给大家。后面我们可以用它做一系列东西。什么是FletFlet是一个框架,允许用你喜欢的语言构建交互式多用户Web,桌面和移动应用程序,而无需拥有前端开发的经验。主

随着互联网的飞速发展,前端开发技术也在不断改进和迭代。PHP和Angular是两种广泛应用于前端开发的技术。PHP是一种服务器端脚本语言,可以处理表单、生成动态页面和管理访问权限等任务。而Angular是一种JavaScript的框架,可以用于开发单页面应用和构建组件化的Web应用程序。本篇文章将介绍如何使用PHP和Angular进行前端开发,以及如何将它们

node.red指Node-RED,是一款基于流的低代码编程工具,用于以新颖有趣的方式将硬件设备,API和在线服务连接在一起;它提供了一个基于浏览器的编辑器,使得我们可以轻松地使用编辑面板中的各种节点将流连接在一起,只需单击即可将其部署到其运行时。

掌握sessionStorage的作用,提升前端开发效率,需要具体代码示例随着互联网的快速发展,前端开发领域也日新月异。在进行前端开发时,我们经常需要处理大量的数据,并将其存储在浏览器中以便后续使用。而sessionStorage就是一种非常重要的前端开发工具,可以为我们提供临时的本地存储解决方案,提高开发效率。本文将介绍sessionStorage的作用,

前端开发中的JavaScript异步请求与数据处理经验总结在前端开发中,JavaScript是一门非常重要的语言,它不仅可以实现页面的交互和动态效果,还可以通过异步请求获取和处理数据。在这篇文章中,我将总结一些在处理异步请求和数据时的经验和技巧。一、使用XMLHttpRequest对象进行异步请求XMLHttpRequest对象是JavaScript用于发送

Webman:提供强大的视觉效果和动画效果的前端开发框架前端开发在不断发展和进步的技术领域中扮演着重要的角色。随着互联网的普及和用户对用户体验的不断追求,前端开发需要更加强大且能够提供令人印象深刻的视觉效果和动画效果。Webman作为一种前端开发框架,致力于提供强大的视觉效果和动画效果,为开发者创造出独特而令人印象深刻的用户体验。Webman集成了丰富的前端

前端开发中,JavaScript路由和页面跳转是必不可少的一部分。一个好的路由方案和页面跳转实现可以带来优秀的用户体验和页面性能。在本篇文章中,我们将从JavaScript路由的基础知识以及页面跳转的常见实现方式进行探讨,分享一些在实践中获得的经验和总结。一、JavaScript路由基础知识为了更好的理解什么是JavaScript路由,我们需要先了解下前端路


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

WebStorm Mac version
Useful JavaScript development tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools
