浏览器如何读取和解释 CSS? [英] How do browsers read and interpret CSS?

查看:25
本文介绍了浏览器如何读取和解释 CSS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两部分问题:

  1. 浏览器是否有像 JavaScript 那样的内置 CSS 解释器?
  2. 浏览器究竟何时读取 CSS 以及何时应用 CSS?

具体来说,我想澄清一下 JavaScript 和 CSS 的不同之处,因为 JavaScript 需要专门等到 window.onload,这样解释器才能正确 getElementById.但是,在 CSS 中,您可以巧妙地选择样式并将其应用于类和 ID.

Specifically, I would like clarification on how or why JavaScript and CSS are different in that with JavaScript you need to specifically wait until window.onload so the interpreter can correctly getElementById. However, in CSS you can select and apply styles to classes and ids all wily nily.

(如果它甚至重要,假设我指的是一个基本的 HTML 页面,头部带有外部样式表)

(If it even matters, assume I am referring to a basic HTML page with an external stylesheet in the head)

推荐答案

如果您最近遇到过慢速连接,您会发现 CSS 将在元素(缓慢)出现时应用于元素,实际上是在重排页面DOM 结构加载时的内容.由于 CSS 不是一种编程语言,它不依赖于在给定时间可用的对象来正确解析 (JavaScript),并且浏览器能够在检索更多 HTML 时简单地重新评估页面的结构将样式应用于新元素.

If you've worked with a slow connection anytime recently, you'll find that CSS will be applied to elements as they (slowly) appear, actually reflowing page content as the DOM structure loads. Since CSS is not a programming language, it doesn't rely on objects being available at a given time to be parsed properly (JavaScript), and the browser is able to simply re-assess the structure of the page as it retrieves more HTML by applying styles to new elements.

也许这就是为什么,即使在今天,Mobile Safari 的瓶颈始终不是 3G 连接,而是页面渲染.

Perhaps this is why, even today, the bottleneck of Mobile Safari isn't the 3G connection at all times, but it is the page rendering.

这篇关于浏览器如何读取和解释 CSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆