Both Sass and Less are CSS preprocessors.
From keycdn.com
A CSS preprocessor is basically a scripting language that extends CSS and then compiles it into regular CSS.
So Sass and Less don't change the functionality of CSS, as they are compiled into plain old CSS. What they do is make it easier to write and maintain CSS with tools such as mixins, nesting, variables, and more.
SCSS, which stands for 'Sassy CSS' is one of two syntaxes for Sass.
From the sass reference:
The second and older syntax, known as the indented syntax (or sometimes just "Sass"), provides a more concise way of writing CSS.
The difference
Both Sass and Less have very similar features. There are syntactical differences, for example, Sass uses $ for variables whereas less uses @ .
There are some slightly more subjective differences, this website claims that "LESS Has More User-Friendly Documentation Than SCSS", however personally I have found the SCSS documentation and examples very easy to use.
sass - Differences between SCSS and LESS - Stack Overflow
css - Styled-Components vs SASS (SCSS) or LESS - Stack Overflow
ELI5: SASS vs. LESS
Should i Learn SCSS , SCSS and LESS
Videos
Both Sass and Less are CSS preprocessors.
From keycdn.com
A CSS preprocessor is basically a scripting language that extends CSS and then compiles it into regular CSS.
So Sass and Less don't change the functionality of CSS, as they are compiled into plain old CSS. What they do is make it easier to write and maintain CSS with tools such as mixins, nesting, variables, and more.
SCSS, which stands for 'Sassy CSS' is one of two syntaxes for Sass.
From the sass reference:
The second and older syntax, known as the indented syntax (or sometimes just "Sass"), provides a more concise way of writing CSS.
The difference
Both Sass and Less have very similar features. There are syntactical differences, for example, Sass uses $ for variables whereas less uses @ .
There are some slightly more subjective differences, this website claims that "LESS Has More User-Friendly Documentation Than SCSS", however personally I have found the SCSS documentation and examples very easy to use.
SCSS and LESS are both "supersets" of vanilla CSS. That means valid CSS is automatically also valid SCSS and LESS, but not necessarily the other way around.
The difference between SCSS and LESS is that they have a different syntax. They mostly have the same functionalities.
I've been using SCSS (SASS) for many years now, and also have been using Styled-Components for a few projects, some large.
I love both, and Styled-Components feels, for me, like a step forward:
Styled-Components - Pros
- Total styling isolation; prevents potential bugs when working in teams, when one team member can never override a style of another. (unless multiple places share the same styled component)
- Remove the need to manually handle class names as they get auto-generated (name-picking components is somewhat easier IMHO than class names)
I've found it easier to work with CSS within the JSX file itself (Opposing my judgment for many years before)
Easily use javascript variables within styles (eliminate the need for 2-sets of theme variable)
Styled-Components - Cons
- Each style component is yet another wrapper function, and many styled-components means more functions, which means less efficient since all that code needs to be "compiled" and so on.
- Biggest drawback: changes to styles require bundle re-compilation and the app's state might reset.
The cons can be only viewed as such on some scenarios but not necessarily all.
SCSS/LESS pros can be viewed as opposite to the cons listed above, while having some more cons such as mixings and faster development when working with variables (IMHO). it can get "ugly" defining a local selector variable:
Compare these simplified examples:
SCSS example:
.icon{
$size: '20px';
width: $size;
height: $size;
margin-left: $size/2;
}
Styled-Components local scope example:
const Icon = styled.i(props => {
const size = 20; // easier to use Number instead of String for calculations
return `
width: ${size}px;
height: ${size}px;
margin-left: ${size/2}px;
`});
Obviously the variable could have been defined outside of the Icon styled wrapper and then internally used, but that won't make it isolated, because styled-component CSS might be composed of sub-components styled and look more like CSS:
const Header = styled.header`
> ul{
...
}
li{
...
}
img{...}
navigation{...}
`
Not always it is desirable to extract each individual HTML element to its own styled component. it is done mostly for components that are or might be repeated across the app.
Regarding SASS mixings, they can be converted to javascript functions, so there's not much advantage to SASS here.
Overall, working with Styled-Components is fun & easy, but has a side-effect of a tighter coupling between the styles and the framework/component and it obviously has some performance penalty (nothing that will actually slow you down, but still).
Browsers are evolved to parse CSS separately from Javascript parsing, why are we trying to deviate from this and fit all in Javascript?
When you mix both Javascript and HTML (namely JSX) and then also CSS (JSS or another), you make your component a solid module that fits into a single file. You don't need to keep styles in a separate file anymore.
Then, the functional magic happens: as JSX is a pure function of raw data that returns "HTML" (not really), the same way CSS-in-JS is a pure function or raw data that returns "CSS" (also not really). From this point, I think it's worth reading about JSX and also about CSS-in-JS.
Styled-component CSS ships its javascript library to the client end, which actually parses styles at the runtime and put inside
<style />tag when each component loads on demand. This means extra load and logic eventually contributing to execution cycles on browser.
Not only on the run-time. Because CSS-in-JSS is just a function of data that returns CSS, you can use it on any platform. Take Node, add SSR, and you have style elements delivered to the browser in response body, therefore parsed just like it would happen in the original case of getting CSS delivered.
Why need this?
Because it is convenient in development, just like React or Redux, just like jQuery, and just like any other lib, it comes at network load and browser performance cost.
You take a library because it solves a problem. If there seems to be no problem, why use library at all, right?
Does continuous computed style text banging via
<style />in the head tag cause browser reflow/repaint ?
There are so many things that force reflow.
Browsers are smart. They don't even make an attempt to repaint if the styles haven't changed. Which doesn't mean that they don't calculate the difference, which costs CPU time.
There's a good intro into the scope and complexity of style (re)calculations, it's really worth reading to understand the subject deeper.
hey guys,
i'm starting to get into web development on my own, just wanted a cool new hobby and I've always wanted to design my own website. I just finished the courses on html, CSS and Java from codeacademy. From browsing this forum and talking to people, I foudn out about SASS and LESS too, extremely interesting, i didn't even know about them when I first started! Just a question, I've been researching, but I still don't know the difference between SASS and LESS, can someone help? Which one is better to do? What do most professional use? and lastly, why are SASS and LESS better than CSS itself?
EDIT: changed "learning" to finishing courses on codeacademy, since I know there is a ton more to learn.