Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do CSS pre-processors produce more efficient CSS code?

Tags:

css

less

By efficient I mean less code (less css rules).

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)

like image 413
Alex Avatar asked Sep 16 '12 21:09

Alex


People also ask

What is the advantage of CSS preprocessor?

With the freedom and capabilities that comes from using variables, mixins, and functions, CSS preprocessors can keep your stylesheets well-organized, flexible, reusable, and more maintainable to help you write better and cleaner CSS code.

What are CSS preprocessors and what are its advantages?

A CSS preprocessor is a program that lets you generate CSS from the preprocessor's own unique syntax. There are many CSS preprocessors to choose from, however most CSS preprocessors will add some features that don't exist in pure CSS, such as mixin, nesting selector, inheritance selector, and so on.

Should I use a CSS preprocessor?

CSS preprocessors make it easy to automate repetitive tasks, reduce the number of errors and code bloat, create reusable code snippets, and ensure backward compatibility. Each CSS preprocessor has its own syntax that they compile into regular CSS so that browsers can render it on the client side.


2 Answers

It really depends how you structure your code in 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.

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.

like image 71
Torsten Walter Avatar answered Oct 19 '22 10:10

Torsten Walter


LESS (when compiled) and Sass both minify your 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. 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.

.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.

like image 29
cimmanon Avatar answered Oct 19 '22 09:10

cimmanon