Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to prevent page rendering from being affected when the browser loads CSS

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to prevent page rendering from being affected when the browser loads CSS". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's way of thinking to study and learn "how to prevent page rendering from being affected when the browser loads CSS".

CSS Code copies content to the clipboard

As soon as the stylesheet is loaded, the media property must be set to an available value so that the style rules can be applied to the html document and the onload event can be used to switch the media property to all:

CSS Code copies content to the clipboard

This method of loading CSS will be much faster than the standard method in transmitting useful information to visitors. Critical CSS loading can still be handled as normal blocking (or you can inline it for final performance), while unimportant styles can be downloaded slowly and applied later in the parsing / rendering process.

This technique uses JavaScript, but you can also encapsulate an equivalent blocking element in an element to handle browsers that cannot run JavaScript:

CSS Code copies content to the clipboard

This technology has a side effect. When a non-blocking stylesheet finishes loading, the document is redrawn to reflect any new style rules it defines. Injecting new styles into the page triggers content backflow, but this is only a problem during the first loading of a page without a history cache. Because of anything related to performance, you will have to make the necessary adjustments when you need to control the cost of a reflux that exceeds the potential speed advantage.

Load fonts using non-blocking CSS

The performance of fonts drawn for the first time is a problem. They are blocking resources and make the text to which they are applied invisible when the font is downloaded. Using the non-blocking link in the above example, it is possible to download a stylesheet containing font data behind the scenes without blocking the rendering of the pressing surface:

CSS Code copies content to the clipboard

Font.css contains a base64-encoded WOFF version of the Merriweather font.

CSS Code copies content to the clipboard

@ font-face {

Font-family: Merriweather

Font-style: normal

Font-weight: 400

Src: local ('Merriweather'), url (_' data:application/x-font-woff;charset=utf-8;base64,...')

}

Main.css contains all the style rules that need to be applied to the site. The following is a declaration of the font:

CSS Code copies content to the clipboard

Body {

Font-family: Merriweather, Lucida Grande,...

}

When the font is being downloaded, the first matching alternate fallback font (in this case, Lucida Grande) is used to render the contents of the page. Once the font stylesheet is applied, Merriweather will be used. I try to make sure that fallback fonts share similar layout features with preferred fonts so that the inevitable backflow is as subtle as possible.

Thank you for your reading, the above is the content of "how to prevent page rendering from being affected when the browser loads CSS". After the study of this article, I believe you have a deeper understanding of how to prevent page rendering from being affected when the browser loads CSS, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report