简体   繁体   English

LESS / SASS CSS与缩小/优化相反?

[英]LESS/SASS CSS opposite from minification/optimizations?

I wonder can I say that LESS/SASS CSS "pre-processors( I think they are called? )" is the opposite from optimizations like minification? 我想我能否说LESS / SASS CSS“预处理器( 我认为它们被称为? )”与缩小等优化相反? I wonder if there will be any noticeable performance impact? 我想知道是否会有明显的性能影响? or do you think easy of development is more important? 或者你认为发展容易更重要?

I ask this because what LESS CSS generates is something like 我问这个是因为LESS CSS生成的东西是这样的

body #div1 #div2 p
body #div1 #div2 p a:link, body #div1 #div2 p a:visited
...

and I think it can really bloat up my CSS quite a bit. 而且我觉得它真的能让我的CSS变得非常膨胀。 as you can see, such specificity is not required and it makes reading CSS hard (at least what I see in firebug). 正如你所看到的,这种特殊性并不是必需的,这使得阅读CSS很难(至少我在firebug中看到的)。

In Sass you can control 'specificity bloat' by understanding how nesting works. 在Sass中,您可以通过了解嵌套的工作原理来控制“特异性膨胀”。 You don't have to do any nesting at all if you don't want - it's something you can control. 如果你不想要的话,你根本不需要做任何嵌套 - 这是你可以控制的。

Using the new @extend directive , you can actually reduce redundancy in your CSS by applying the priciples of OOCSS and can thus improve performance. 使用新的@extend指令 ,您可以通过应用OOCSS的原则来实际减少 CSS中的冗余,从而可以提高性能。 Here's a good article to get you started with @extend . 这是一篇很好的文章, 可以帮助您开始使用@extend And a few more OOCSS resources: 还有一些OOCSS资源:

The great advantage of @extend in Sass is that it takes the manual effort involved in applying OOCSS and makes it wonderfully painless and easy. @extend in Sass的巨大优势在于它需要手动工作才能应用OOCSS,并使其非常轻松和简单。

Finally, as Andrea pointed out, Sass has a variety of options for minifying CSS (see the :compressed style ), so overall you've actually got a pretty potent toolkit for not only improving your performance as a developer, but also improving the performance of your CSS. 最后,正如Andrea指出的那样,Sass有很多缩小CSS的选项(参见:压缩样式 ),所以总体而言,你实际上有一个非常强大的工具包,不仅可以提高你作为开发人员的性能,还可以提高性能你的CSS For an example of this in action, see how Chris Eppstein, author of Compass and core contributor of Sass, refactors the Digg stylesheet down from 125 lines of code to 85 lines of code (a 32% reduction). 有关此示例,请参阅Compass的作者和Sass的核心贡献者Chris Eppstein如何将Digg样式表从125行代码重构为85行代码(减少32%)。

You'd be surprised, but gzipped CSS files that have repeated blocks of code shouldn't be too much larger than ones with the shorter selectors. 你会感到惊讶,但是重复编码的CSS文件不应该比选择器较短的文件大得多。

This is thanks to how the compression algorithm handles duplicate strings in a file. 这要归功于压缩算法如何处理文件中的重复字符串。 Instead of including the same string twice, it replaces the second occurrence with a reference to the first one, and so that string only appears in the compressed file once. 它不是包含两次相同的字符串,而是使用对第一个字符串的引用替换第二个字符串,因此该字符串仅出现在压缩文件中一次。 Take a look at how repetitive the selectors in your generated CSS file are - this should make them compress fairly well. 看一下生成的CSS文件中选择器的重复性 - 这应该使它们能够很好地压缩。

Naturally, the key to this is making sure your CSS files are gzipped - leaving them uncompressed will definitely increase the file size. 当然,关键是要确保你的CSS文件被gzip压缩 - 让它们解压缩肯定会增加文件大小。

Depending on the options , SASS can give output in different styles. 根据选项 ,SASS可以提供​​不同风格的输出。 For production, you will want to set the output style to 'compact'. 对于生产,您需要将输出样式设置为“紧凑”。

There is a firebug addon for sass that should make things easier to read. 有一个用于sass的firebug插件,可以使事情更容易阅读。 You don't really need to read the output directly anyway. 无论如何,您根本不需要直接读取输出。

Sass, less and xCSS will generate more output but I wouldn't call it bloat. Sass,less和xCSS将产生更多输出,但我不会称之为膨胀。

Hand written CSS, with it's many redundancies and duplications will degrade quickly as developers pile name spaces on top of others during the life cycle of the code. 手写CSS,随着开发人员在代码生命周期中将名称空间叠加在其他人之上,许多冗余和重复将迅速降级。 One of the symptoms of poorly maintained, designed or written software is bloat. 维护不善,设计或编写的软件的一个症状是膨胀。 And because CSS has some design deficiencies from the start, even the best CSS coders are effected by this limitation. 而且因为CSS从一开始就存在一些设计缺陷,即使是最好的CSS编码器也受到这种限制的影响。

So you got to weigh up the initial footprint of your well formatted sass/less file against a hand written CSS file that has been edited a few times over. 因此,您需要权衡格式良好的sass / less文件的初始占用空间与已编辑几次的手写CSS文件。

Another benefit on sass is that your HTML becomes leaner. sass的另一个好处是你的HTML变得更加精简。 You don't need to append class names throughout your code to make up for the flat, global structure of CSS. 您不需要在整个代码中附加类名来弥补CSS的扁平全局结构。

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

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