简体   繁体   English

CSS预处理器能否生成更高效的CSS代码?

[英]Do CSS pre-processors produce more efficient CSS code?

By efficient I mean less code (less css rules). 通过有效我的意思是更少的代码(更少的CSS规则)。

Because I'm converting a CSS file to less, and I'm surprised to find out that the compiled CSS file is quite small (I haven't finished yet :P) 因为我正在将CSS文件转换为更少,我很惊讶地发现编译后的CSS文件非常小(我还没有完成:P)

It really depends how you structure your code in LESS. 这实际上取决于您在LESS中构建代码的方式。 The compiled output can become quite unwieldy if you use a lot of mixins (for example for gradients) or the @ operator to nest selectors too liberally. 如果使用大量mixins(例如渐变)或@运算符过于宽松地嵌套选择器,编译后的输出会变得非常笨重。

All in all one could argue that they 'can' produce more efficient code if used properly. 总而言之,如果使用得当,他们可以说他们“可以”生成更有效的代码。 But then, so can you I you use plain CSS. 但是,你也可以使用纯CSS。

As noted above, it largely depends on whether your CSS was inefficient to start with. 如上所述,它在很大程度上取决于你的CSS开始时是否效率低下。 However, there is almost no situation in practise that requires more "efficient" CSS (I'm talking about performance rather than repetition) - yes it's bad to duplicate rules etc, but first and foremost should be code readability and maintainability. 但是,在实践中几乎没有需要更多“高效”CSS的情况(我说的是性能而不是重复) - 是的,复制规则等是不好的,但首先应该是代码可读性和可维护性。

In general, I find that the productivity gains from using a pre-processor far outweigh any issues with rule repetition, and if it's actual filesize you want to cut down on then WinLESS (and other compilers) can minify at source. 总的来说,我发现使用预处理器所带来的生产力收益远远超过规则重复的任何问题,如果它是你想减少的实际文件大小,那么WinLESS(和其他编译器)可以在源头缩小。

In general tools like LESS or SASS will not be able to produce as streamlined CSS as writing it manually (with proper knowledge), because the tools have no knowledge of the DOM on which they're operating. 通常,像LESS或SASS这样的工具将无法像手动编写(使用适当的知识)那样生成简化的CSS,因为这些工具不了解它们正在运行的DOM。 Any optimizer will only be as good as how thoroughly it can assess the runtime environment, and the missing DOM is a pretty big variable. 任何优化器只会像评估运行时环境的程度一样好,而缺少的DOM是一个非常大的变量。 If you structure your document properly and write the CSS to take advantage of that, then the output will be far smaller than generated code. 如果正确构造文档并编写CSS以利用它,则输出将远小于生成的代码。

The advantage to these tools like any form of code generation or even higher level languages for that matter is that they enhance productivity. 这些工具的优势,如任何形式的代码生成,甚至更高级别的语言,都可以提高生产力。 More consistent and possibly maintainable output can be produced more easily, but in order to do so these tools will be uncompromisingly explicit and "safe", and therefore produce code that a human would able to easily recognize as unnecessary when associated with the document. 可以更容易地产生更一致且可能可维护的输出,但是为了这样做,这些工具将是毫不妥协的明确和“安全”的,并且因此产生人类在与文档相关联时能够容易地识别为不必要的代码。 In general development and maintenance costs are higher than CPU cycles, so the productivity gains win out out. 一般而言,开发和维护成本高于CPU周期,因此生产率提高了。 There may be times like any other when there may be performance issues that need to be addressed, but "premature optimization is the root of all evil" - Knuth 当可能存在需要解决的性能问题时,可能会出现其他任何问题,但“过早优化是所有邪恶的根源” - Knuth

LESS (when compiled) and Sass both minify your CSS. 少(编译时)和Sass都缩小你的CSS。 In addition to stripping out whitespace, you'll sometimes see things like border: 0 10px 0 10px get turned into border: 0 10px or colors like #666666 turned into #666 . 除了剥离空格外,你有时会看到像border: 0 10px 0 10px这样的东西border: 0 10px 0 10px变成border: 0 10px或像#666666这样的颜色变成#666 It isn't any more efficient, but it does make a smaller download for the user (which is valuable for mobile devices). 它不是更高效,但它确实为用户提供了更小的下载(这对移动设备很有价值)。

That said, Sass has a unique directive called @extend, which allows you to group your styles logically in the pre-compiled state, but will group them together to avoid duplication in the compiled state. 也就是说,Sass有一个名为@extend的独特指令,它允许您在预编译状态下逻辑地对样式进行分组,但会将它们组合在一起以避免在编译状态下出现重复。

.classA { color: blue }

// 50 lines of code defining other elements...

.classB { @extend .classA }

Will output something that looks like this: 将输出如下所示的内容:

.classA, .classB { color: blue }

It doesn't seem like that big of a deal with 2 classes only having one thing in common, but its a bigger deal the more elements you are using and the more things they have in common. 看起来两个类只有一个共同点,但是它更大,你使用的元素越多,它们的共同点就越多。

Otherwise no. 否则没有。 The efficiency is entirely based on the author. 效率完全基于作者。

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

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