SASS syntax (not SCSS) documentation and tools - css

I find SASS syntax much easier and cleaner than SCSS, but the documentation is not as widespread and complete so I often stumble in time wasting syntax errors. Many articles mentioning SASS actually only contain information about SCSS. Even the official SASS documentation (https://sass-lang.com/documentation/file.SASS_REFERENCE.html#if) contains examples that apparently only refer to SCSS. For example, what's the SASS syntax for conditional statements in mixins? For example:
=button-colors($primary, $secondary, $border: true)
background-color: $primary
color: $secondary
if $border
border-color: $secondary
else
border-color: $primary
This example does not compile correctly: the if statement is ignored and there's no border-color in the resulting CSS.
Does anybody know about useful websites with documentation and examples of SASS syntax or tools to convert from CSS/SCSS to SASS online?

As you saw, The reference document is written in scss syntax maybe because of its more popularity and I'm going to highly recommend you choose scss syntax because of its most benefits that maybe is not clear at first view!
But anyway if you prefer to use sass one, I think you have to follow main document and check this link to watch the differences and then correct your syntax.
You can also use these tools to convert stylesheets:
css2sass: css to scss/sass.
sassmeister: scss/sass to css.

Related

New To SASS - Not Compiling

Just decided to learn SASS, pretty easy stuff except a very basic file won't compile.
It's literally this basic:
$test: #ffffff;
Command:
sass test.sass
Error Message:
error: expected "
".
$test:#ffffff;
^
test.sass 1:14 root stylesheet
I've no idea what it means by expected " - expected where??
please change extension test.sass to test.scss. and you good to go. 100% :)
As per sass-lang.com.
There are two syntaxes available for Sass. The first, known as SCSS
(Sassy CSS) and used throughout this reference, is an extension of the
syntax of CSS. This means that every valid CSS stylesheet is a valid
SCSS file with the same meaning. In addition, SCSS understands most
CSS hacks and vendor-specific syntax, such as IE's old filter syntax.
This syntax is enhanced with the Sass features described below. Files
using this syntax have the .scss extension.
The second and older syntax, known as the indented syntax (or
sometimes just "Sass"), provides a more concise way of writing CSS. It
uses indentation rather than brackets to indicate nesting of
selectors, and newlines rather than semicolons to separate properties.
Some people find this to be easier to read and quicker to write than
SCSS. The indented syntax has all the same features, although some of
them have slightly different syntax; this is described in the indented
syntax reference. Files using this syntax have the .sass extension.

What is the difference between CSS and SCSS?

I know CSS very well, but am confused about Sass. How is SCSS different from CSS, and if I use SCSS instead of CSS will it work the same?
In addition to Idriss answer:
CSS
In CSS we write code as depicted bellow, in full length.
body{
width: 800px;
color: #ffffff;
}
body content{
width:750px;
background:#ffffff;
}
SCSS
In SCSS we can shorten this code using a #mixin so we don’t have to write color and width properties again and again. We can define this through a function, similarly to PHP or other languages.
$color: #ffffff;
$width: 800px;
#mixin body{
width: $width;
color: $color;
content{
width: $width;
background:$color;
}
}
SASS
In SASS however, the whole structure is visually quicker and cleaner than SCSS.
It is sensitive to white space when you are using copy and paste,
It seems that it doesn't support inline CSS currently.
$color: #ffffff
$width: 800px
$stack: Helvetica, sans-serif
body
width: $width
color: $color
font: 100% $stack
content
width: $width
background:$color
CSS is the styling language that any browser understands to style webpages.
SCSS is a special type of file for SASS, a program written in Ruby that assembles CSS style sheets for a browser, and for information, SASS adds lots of additional functionality to CSS like variables, nesting and more which can make writing CSS easier and faster.
SCSS files are processed by the server running a web app to output a traditional CSS that your browser can understand.
css has variables as well. You can use them like this:
--primaryColor: #ffffff;
--width: 800px;
body {
width: var(--width);
color: var(--primaryColor);
}
.content{
width: var(--width);
background: var(--primaryColor);
}
Variable definitions right:
$ => SCSS, SASS
-- => CSS
# => LESS
All answers is good but question a little different than answers
"about Sass. How is SCSS different from CSS" : scss is well formed CSS3 syntax. uses sass preprocessor to create that.
and if I use SCSS instead of CSS will it work the same? yes. if your ide supports sass preprocessor. than it will work same.
Sass has two syntaxes. The most commonly used syntax is known as “SCSS” (for “Sassy CSS”), and is a superset of CSS3’s syntax. This means that every valid CSS3 stylesheet is valid SCSS as well. SCSS files use the extension .scss.
The second, older syntax is known as the indented syntax (or just “.sass”). Inspired by Haml’s terseness, it’s intended for people who prefer conciseness over similarity to CSS. Instead of brackets and semicolons, it uses the indentation of lines to specify blocks. Files in the indented syntax use the extension .sass.
Furher Information About:
What Is A CSS Preprocessor?
CSS in itself is devoid of complex logic and functionality which is required to write reusable and organized code. As a result, a developer is bound by limitations and would face extreme difficulty in code maintenance and scalability, especially when working on large projects involving extensive code and multiple CSS stylesheets. This is where CSS Preprocessors come to the rescue.
A CSS Preprocessor is a tool used to extend the basic functionality of default vanilla CSS through its own scripting language. It helps us to use complex logical syntax like – variables, functions, mixins, code nesting, and inheritance to name a few, supercharging your vanilla CSS. By using CSS Preprocessors, you can seamlessly automate menial tasks, build reusable code snippets, avoid code repetition and bloating and write nested code blocks that are well organized and easy to read.
However, browsers can only understand native vanilla CSS code and will be unable to interpret the CSS Preprocessor syntax. Therefore, the complex and advanced Preprocessor syntax needs to be first compiled into native CSS syntax which can then be interpreted by the browsers to avoid cross browser compatibility issues. While different Preprocessors have their own unique syntaxes, eventually all of them are compiled to the same native CSS code.
Moving forward in the article, we will take a look at the 3 most popular CSS Preprocessors currently being used by developers around the world i.e Sass, LESS, and Stylus.
Before you decide the winner between Sass vs LESS vs Stylus, let us get to know them in detail first.
Sass – Syntactically Awesome Style Sheets
Sass is the acronym for “Syntactically Awesome Style Sheets”. Sass is not only the most popular CSS Preprocessor in the world but also one of the oldest, launched in 2006 by Hampton Catlin and later developed by Natalie Weizenbaum.
Although Sass is written in Ruby language, a Precompiler LibSass allows Sass to be parsed in other languages and decouple it from Ruby. Sass has a massive active community and extensive learning resources available on the net for beginners. Thanks to its maturity, stability and powerful logical prowess, Sass has established itself to the forefront of CSS Preprocessor ahead of its rival peers.
Sass can be written in 2 syntaxes either using Sass or SCSS. What is the difference between the two? Let’s find out.
Syntax Declaration: Sass vs SCSS
SCSS stands for Sassy CSS. Unlike Sass, SCSS is not based on
indentation.
.sass extension is used as original syntax for Sass, while SCSS
offers a newer syntax with .scss extension.
Unlike Sass, SCSS has curly braces and semicolons, just like CSS.
Contrary to SCSS, Sass is difficult to read as it is quite deviant
from CSS. Which is why SCSS it the more recommended Sass syntax as it
is easier to read and closely resembles Native CSS while at the same
time enjoying with power of Sass.
Consider the example below with Sass vs SCSS syntax along with Compiled CSS code.
Sass SYNTAX
$font-color: #fff
$bg-color: #00f
#box
color: $font-color
background: $bg-color
SCSS SYNTAX
$font-color: #fff;
$bg-color: #00f;
#box{
color: $font-color;
background: $bg-color;
}
In both cases, be it Sass or SCSS, the compiled CSS code will be the same –
#box {
color: #fff;
background: #00f;
Usage of Sass
Arguably the most Popular front end framework Bootstrap is written in Sass. Up until version 3, Bootstrap was written in LESS but bootstrap 4 adopted Sass and boosted its popularity. A few of the big companies using Sass are – Zapier, Uber, Airbnb and Kickstarter.
LESS – Leaner Style Sheets
LESS is an acronym for “Leaner Stylesheets”. It was released in 2009 by Alexis Sellier, 3 years after the initial launch of Sass in 2006. While Sass is written in Ruby, LESS is written JavaScript. In fact, LESS is a JavaScript library that extends the functionality of native vanilla CSS with mixins, variables, nesting and rule set loop. Sass vs LESS has been a heated debate. It is no surprise that LESS is the strongest competitor to Sass and has the second-largest user base. However, When bootstrap dumped LESS in favor of Sass with the launch of Bootstrap 4, LESS has waned in popularity. One of the few disadvantages of LESS over Sass is that it does not support functions. Unlike Sass, LESS uses # to declare variables which might cause confusion with #media and #keyframes. However, One key advantage of LESS over Sass and Stylus or any other preprocessors, is the ease of adding it in your project. You can do that either by using NPM or by incorporating Less.js file.
Syntax Declaration: LESS
Uses .less extension. Syntax of LESS is quite similar to SCSS with the exception that for declaring variables, instead of $ sign, LESS uses #.
#font-color: #fff;
#bg-color: #00f
#box{
color: #font-color;
background: #bg-color;
}
COMPILED CSS
#box {
color: #fff;
background: #00f;
}
Usage Of LESS
The popular Bootstrap framework until the launch of version 4 was written in LESS. However, another popular framework called SEMANTIC UI is still written in LESS. Among the big companies using Sass are – Indiegogo, Patreon, and WeChat
Stylus
The stylus was launched in 2010 by former Node JS developer TJ Holowaychuk, nearly 4 years after the release of Sass and 1 year after the release of LESS. The stylus is written Node JS and fits perfectly with JS stack. The stylus was heavily influenced by the logical prowess of the Sass and simplicity of LESS. Even though Stylus is still popular with Node JS developers, it hasn’t managed to carve out a sizeable share for itself. One advantage of Stylus over Sass or LESS, is that it is armed with extremely powerful built-in functions and is capable of handling heavy computing.
Syntax Declaration: Stylus
Uses .styl extension. Stylus offers a great deal of flexibility in writing syntax, supports native CSS as well as allows omission of brackets colons and semicolons. Also, note that Stylus does not use # or $ symbols for defining variables. Instead, Stylus uses the assignment operators to indicate a variable declaration.
STYLUS SYNTAX WRITTEN LIKE NATIVE CSS
font-color = #fff;
bg-color = #00f;
#box {
color: font-color;
background: bg-color;
}
OR
STYLUS SYNTAX WITHOUT CURLY BRACES
font-color = #fff;
bg-color = #00f;
#box
color: font-color;
background: bg-color;
OR
STYLUS SYNTAX WITHOUT COLONS AND SEMICOLONS
font-color = #fff
bg-color = #00f
#box
color font-color
background bg-color
And this is less
#primarycolor: #ffffff;
#width: 800px;
body{
width: #width;
color: #primarycolor;
.content{
width: #width;
background:#primarycolor;
}
}
Sass is a language that provides features to make it easier to deal with complex styling compared to editing raw .css. An example of such a feature is allowing definition of variables that can be re-used in different styles.
The language has two alternative syntaxes:
A JSON like syntax that is kept in files ending with .scss
A YAML like syntax that is kept in files ending with .sass
Either of these must be compiled to .css files which are recognized by browsers.
See https://sass-lang.com/ for further information.

Confusion on Sass and SCSS

My confusion comes from (aside from being relatively new) the term "Sass" being used to refer to both Sass and SCSS syntax.
Based on this guide, they are distinct: http://sass-lang.com/guide
So when reading a discussion, or a job listing, unless syntax is provided I am not sure which dialect they are talking about.
Has Sass syntax been replaced by SCSS syntax (or vice-versa) and just falls under the umbrella term "Sass"?
Just want to make sure I have been using and learning is the current syntax. I have been learning the SCSS syntax.
SASS is SASS, SCSS is SASS.
They are practically the same thing, except the main differences between them being that SASS does not require curly braces or semi-colons and some find this a bit daunting at first, so the good people behind it created SCSS, which likes its curly braces { } and semi-colons ; .
SASS
section
header
width: 100%
height: 40px
SCSS
section {
header {
width: 100%;
height: 40px;
}
}
More detailed information about this can be found at the main SASS website.
And ofcourse, the file-extension being changed from *.sass to *.scss so that one can be aware of the fact.
These are the main differences, there are other bits and nits but the choice between both comes down to personal preference or project requirements.
Verdict: If you know SASS, you know SCSS.

analyze sass files to see where I could use compass mixins?

When you're in flow, it's hard to stop yourself and make sure all the css properties you are using have the right compliance.
Is there a way to analyze css files, and see where I could have used compass mixins to maximize browser compatibility?
SassMeister is a playground that specializes only in Sass-related stuff.
http://sassmeister.com/
Of course, SassMeister lets you choose the syntax you feel more comfortable with and you even can choose the output style (expanded, compressed, and so on). Most importantly, because SassMeister is specific to Sass, it allows you to import Sass libraries (Compass extensions actually). You can even propose your own extensions if you think they should be added.

Importing & Extending raw CSS?

I have a preprocessor import problem. I have vendor css that i would like to copy functionality. Unfortunately, i don't control the html for this situation either.. so essentially i have vendor css & different vendor html, and i am trying to extend functionality of one css rule into another.
Now, the basic concept in many css preprocessors is to extend the rule.
// Vendor
.foo { font-weight: bold; }
.bar { text-color: red; }
// Mine
.bar { .foo }
In the above example, you don't have access to .foo or .bar, but with preprocessors, you can extend functionality from .foo into .bar, which works great for less/stylus/etc. The problem, is when the vendor rules are only defined in raw css.
So with that said, the most obvious solution is to have Stylus/Less import the raw css as stylus/less. However, neither seem to be able to actually do this.. that i've found at least. Less doesn't seem able to (#import "foo.css" directives are ignored) and while Stylus has an option to actually include the imported raw CSS, it did not seem to actually be processing it. That is to say, it would include the css, but #extend directives failed (perhaps i am wrong?).
The other option i could think of is simply renaming the .css files to the preprocessor language extension. Since we want to keep the vendor stuff out of our hands, allowing for proper versioning/etc, This involves a build process which copies the target css files and renames them into the target language extension, but this is error prone. Stylus ran into syntax errors immediately, and Less could just as easily (as not all CSS is valid Less. most is, but not all).
So.. any additional thoughts on this front? Hopefully there is an option i may be missing? Perhaps SCSS handles this better?
Currently, with what i have found is/isn't possible.. i feel the only stable way is to simply copy the raw css bits into my css code. If the vendor code changes, i'll have to update manually, but at least i don't run the risk of vendor stuff changing and all of a sudden the build process fails, because vendor introduced some CSS syntax not supported by less/etc. It is far from good.. but the worst that can happen is my css looks funny, and i need to update.
edit:
I'm going to try SCSS next.. we'll see how it goes. On the site, they claim all CSS(3 only?) syntax is valid SCSS
edit2: For clarity. My question is, give the above scenario, what would you do? Importing the .css files is not possible (See note below!), and renaming .css files to .less/.stylus can in turn be error prone, since less is not a perfect superset of CSS(namely due to proprietary features).
!Note!: I have since been digging around the less source to see what could be done to fix this problem, rather than work around it, and ran into a dozen support tickets on the matter. The main discussion of which seems to be taking place on #issue 1185, and more importantly, check out the changelog which *(i believe) addresses this issue! CHANGELOG 1.4.0 Beta
So with that said, the #import (less) "file.css" is likely the winning scenario here, even if it may have a few bugs due to the beta status. Yay :)
Your best bet is either what #Lee Olayvar said, or use the new ":extend()" feature in LESS. It sounds like you might have tried the extend feature, but you used the "SASS directive" syntax (which IMO is improper use of an at-rule), whereas LESS uses the CSS-like pseudo-selector syntax:
.foo:extend(.bar) {}
And if you are patient (in the coming days/weeks), you will see a new feature that is perfect for what you want to do, and it's unique to LESS. It will allow you to extend or mixin external "silent" styles. Meaning you will be able to "#import" and external stylesheet but the styles won't show up in your compiled code unless you extend them or use them as a mixin.
I'm on the core team for Less.js btw, so let me know if you have any other questions or if I can be of further help.
It looks like all imported .css files won't be prepocessed by less but will just be added with an #import at the top.
You can import both CSS and LESS files. Only LESS files import statements are processed, CSS file import statements are kept as they are. If you want to import a CSS file, and don’t want LESS to process it, just use the .css extension
Source: http://lesscss.org/
The best thing you could do would be the renaming I believe.
While still in beta, the newly added #import (less) "file.css" syntax should properly handle all CSS files. Due to the "official" support, it can also be expected/assumed that any proprietary syntax that breaks LESS would be fixed (though possibly not "supported").
So in theory this is the best option! Barring SCSS, which i have not tried yet in this scenario. Stylus still has issues though, in my tests.
For more information, check out this issue or this changelog.

Resources