The css file should be placed in the head at the top of the HTML. Reason: The browser parses the HTML document from top to bottom. If the CSS is placed at the bottom, the page can be rendered gradually, but after the CSS is downloaded and parsed, the text and images that have been rendered will need to be redrawn according to the new style. , which is a bad user experience.
The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.
css files should be placed in the head at the top of the HTML.
#why?
The href (HyperText reference) attribute in the link tag represents a hypertext reference. When CSS uses href reference, the browser will recognize it The document is CSS and is downloaded in parallel. It will not stop loading the current document. When loading HTML to generate a DOM tree, the DOM tree can be rendered at the same time. This can prevent flickers, white screens or confusing layouts.
In the most ideal situation, we hope that the browser will gradually render the downloaded CSS and gradually display the page to the user. However, in order to avoid re-rendering page elements when styles change, the browser blocks the gradual rendering of content. The browser waits for all styles to be loaded before rendering the page at once.
If the CSS file is placed at the bottom, the browser prevents the content from being rendered gradually. While the browser is waiting for the last CSS file to be downloaded, a "white screen" appears (white screen when a new connection is opened, and then Text appears first, images appear last, and styles appear last). This is very serious, because when the network speed is very slow, the CSS download time is relatively long, which naturally brings a "white screen" to the user for a long time, and the user experience is very poor.
The CSS file is placed at the top because the order of placement determines the priority of downloading, and more importantly, the rendering mechanism of the browser.
css will not affect the generation of the DOM tree during the loading process, but it will affect the generation of the Render tree, which in turn affects the layout. Therefore, generally speaking, the link tag of style needs to be placed as much as possible In the head, because the DOM tree is parsed from top to bottom, and the css style is loaded asynchronously, in this case, parsing the body node under the DOM tree and loading the css style can be as parallel as possible, speeding up the Render tree. The speed of generation.
Put the CSS at the bottom, and the page can be rendered gradually. However, after the CSS is downloaded and parsed, the text and images that have been rendered need to be redrawn according to the new style. This is a bad user experience. experience.
(Learning video sharing:css video tutorial)
The above is the detailed content of Where should the css file be placed?. For more information, please follow other related articles on the PHP Chinese website!