简体   繁体   English

最小化CSS规则VS单个样式表文件

[英]minimizing CSS rules VS single stylesheet file

For better reflow handling, it is said that unused CSS rules should be avoided, because the browser spends time trying to find match all these rules. 为了更好地进行重排处理,据说应该避免使用未使用的CSS规则 ,因为浏览器会花时间尝试查找所有这些规则的匹配项。 But, in real life, we have websites with many pages, and popups and what not, and it's also a best-practice to use as few HTTP requests as possible, and in most cases it's best to create a single CSS file that includes everything, (and that file will be cached). 但是,在现实生活中,我们的网站上有很多页面,弹出窗口等等,并且最好的做法是使用尽可能少的HTTP请求,并且在大多数情况下,最好创建一个包含所有内容的CSS文件 ,(该文件将被缓存)。 So, these two performance best-practice approaches seems to collide with each other, because, by creating a single file, it will be filled with rules which aren't in use most of the time (per page), and by splitting it up to many stylesheet files and only uses the ones the page (or components) need will cause many HTTP requests to be fired and might also be difficult to manage. 因此,这两种性能最佳实践方法似乎相互冲突,因为通过创建一个文件,文件中将填充大部分时间(每页)未使用的规则并将其拆分到许多样式表文件,并且仅使用页面(或组件)所需的文件会导致许多HTTP请求被触发,并且可能也难以管理。

So, what is best? 那么,什么是最好的? is reducing reflow is better than less HTTP requests? 减少回流比减少HTTP请求好吗?

some links on the matter : 关于此事的一些链接
https://developers.google.com/speed/docs/best-practices/payload#RemoveUnusedCSS https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Writing_efficient_CSS https://developers.google.com/speed/docs/best-practices/payload#RemoveUnusedCSS https://developer.mozilla.org/zh-CN/docs/Web/Guide/CSS/Writing_efficient_CSS

Warning: Does not answer the question (I misread) 警告:没有回答问题(我读错了)

One way to solve both your problems would be to use stylesheet extension language such as SASS or LESS . 解决这两个问题的一种方法是使用样式表扩展语言,例如SASSLESS This greatly improves how you can organize your CSS since it allows multiple SASS/LESS files to compile into a single, minimized CSS file. 这可以极大地改善组织CSS的方式,因为它允许将多个SASS / LESS文件编译成一个最小的CSS文件。 It also provides additional features which should come in handy for anyone with complex stylesheets such as mixins, definitions and a lot more. 它还提供了其他功能,这些功能对于具有复杂样式表的任何人(如混入,定义等)都将派上用场。

Unfortunately I do not have an answer to what is the best practice or a good balance if you are unable to use SASS or LESS. 不幸的是,如果您无法使用SASS或LESS,那么我对最佳实践或最佳平衡没有任何答案。

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

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