简体   繁体   English

HTML文件末尾的CSS

[英]CSS at the end of HTML files

I would like to know how the browser handles CSS rules that come after most (if not all) of the HTML. 我想知道浏览器如何处理大多数(如果不是全部)HTML之后的CSS规则。 Will it have to reparse the whole page due to the new rules or does it use some other kind of technique to handle this type of situation? 是否需要根据新规则重新解析整个页面,还是使用其他某种技术来处理这种情况? Thanks. 谢谢。

There are many cases when a repaint must occur, and in many occurences in a page lifetime the DOM is changed. 在许多情况下,必须进行重绘,并且在页面生存期的许多情况下,都更改了DOM。

But once the page is parsed, there is no reason to parse it again, all changes are made on the in memory DOM. 但是一旦页面被解析,就没有理由再次解析它,所有更改都在内存DOM中进行。

This being said, you should put the CSS links in the HEAD because 话虽如此,您应该将CSS链接放在HEAD中,因为

Relayout and repaint, perhaps. 重新布局并重新粉刷。 (That is, if it has already started rendering it and the styles loaded require different display.) (也就是说,如果它已经开始渲染它,并且加载的样式需要不同的显示。)

Reparse, no. 修复,不。 Style sheets are purely presentational; 样式表纯粹是呈现形式; they do not affect the parsing. 它们不影响解析。

Assuming that the browser has already started rendering the page when it sees the additional CSS (there are quite a few browser-specific triggers for this behavior) and assuming that the new rules result in CSS property changes for at least one element, the browser will simply mark that element as one that needs redrawing. 假设浏览器在看到其他CSS时已经开始呈现页面(此行为有很多特定于浏览器的触发器),并且假设新规则导致CSS属性至少改变了一个元素,那么浏览器将只需将该元素标记为需要重绘的元素即可。

This will result in any visible changes to the page being shown the next time the browser repaints part of its window. 这将导致在下次浏览器重新绘制其窗口的一部分时,对页面进行任何可见的更改。

It's important to keep in mind that modern browsers do all of this asynchronously and schedule events like applying new CSS, recalculating layout and painting to the screen mostly (but not totally) independently of each other. 重要的是要记住,现代浏览器会异步地完成所有这些工作,并安排事件(例如应用新CSS,重新计算布局和在屏幕上绘画)在很大程度上(但不是完全)彼此独立。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM