Sass (stylesheet language)
Sass is a preprocessor scripting language that is interpreted or compiled into Cascading Style Sheets. SassScript is the scripting language itself.
Sass consists of two syntaxes. The original syntax, called "the indented syntax," uses a syntax similar to Haml. It uses indentation to separate code blocks and newline characters to separate rules. The newer syntax, "SCSS", uses block formatting like that of CSS. It uses braces to denote code blocks and semicolons to separate rules within a block. The indented syntax and SCSS files are traditionally given the extensions.sass and.scss, respectively.
CSS3 consists of a series of selectors and pseudo-selectors that group rules that apply to them. Sass extends CSS by providing several mechanisms available in more traditional programming languages, particularly object-oriented languages, but that are not available to CSS3 itself. When SassScript is interpreted, it creates blocks of CSS rules for various selectors as defined by the Sass file. The Sass interpreter translates SassScript into CSS. Alternatively, Sass can monitor the.sass or.scss file and translate it to an output.css file whenever the.sass or.scss file is saved.
The indented syntax is a metalanguage. SCSS is a nested metalanguage, as valid CSS is valid SCSS with the same semantics.
SassScript provides the following mechanisms: variables, nesting, mixins, and selector inheritance.
History
Sass was initially designed by Hampton Catlin and developed by Natalie Weizenbaum. After its initial versions, Weizenbaum and Chris Eppstein have continued to extend Sass with SassScript, a scripting language used in Sass files.Major implementations
SassScript was implemented in multiple languages, the noteworthy implementations are:- The original open-source Ruby implementation created in 2006, since deprecated due to the lack of maintainers and reached End-of-Life in March 2019.
- The official open-source Dart implementation.
- libSass, the official open-source C++ implementation.
- the official JavaScript implementation, published as "sass" module on npm.
- JSass, an unofficial Java implementation.
- phamlp, an unofficial SASS/SCSS implementation in PHP.
- Vaadin has a Java implementation of Sass.
- Firebug, a Firefox XUL extension for web development. It has been since deprecated in favor of developer tools integrated into Firefox itself. It stopped working since Firefox 57 dropped support for XUL extensions.
Features
Variables
Sass allows variables to be defined. Variables begin with a dollar sign. Variable assignment is done with a colon.SassScript supports four data types:
- Numbers
- Strings
- Colors
- Booleans
SCSS | Sass | Compiled CSS |
$primary-color: #3bbfce; $margin: 16px; .content-navigation .border | $primary-color: #3bbfce $margin: 16px .content-navigation border-color: $primary-color color: darken .border padding: $margin/2 margin: $margin/2 border-color: $primary-color | .content-navigation .border |
Nesting
CSS does support logical nesting, but the code blocks themselves are not nested. Sass allows the nested code to be inserted within each other.SCSS | Sass | Compiled CSS |
table.hl li | table.hl margin: 2em 0 td.ln text-align: right li font: family: serif weight: bold size: 1.3em | table.hl table.hl td.ln li |
More complicated types of nesting including namespace nesting and parent references are discussed in the Sass documentation.
SCSS | Sass | Compiled CSS |
@mixin table-base
| =table-base th text-align: center font-weight: bold td, th padding: 2px
|
|
Loops
Sass allows for iterating over variables using, and, which can be used to apply different styles to elements with similar classes or ids.Sass | Compiled CSS |
$squareCount: 4 @for $i from 1 through $squareCount #square-# background-color: red width: 50px * $i height: 120px / $i |
|
Arguments
Mixins also support arguments.Sass | Compiled CSS |
=left float: left margin-left: $dist
|
|
In combination
Sass | Compiled CSS |
=table-base th text-align: center font-weight: bold td, th padding: 2px =left float: left margin-left: $dist
+table-base |
|
Selector inheritance
While CSS3 supports the Document Object Model hierarchy, it does not allow selector inheritance. In Sass, inheritance is achieved by inserting a line inside of a code block that uses the @extend keyword and references another selector. The extended selector's attributes are applied to the calling selector.Sass | Compiled CSS |
.error border: 1px #f00 background: #fdd .error.intrusion font-size: 1.3em font-weight: bold .badError @extend.error border-width: 3px | .error,.badError .error.intrusion, .badError.intrusion .badError |
Sass supports multiple inheritance.
libSass
At the 2012 HTML5 Developer Conference, Hampton Catlin, the creator of Sass, announced version 1.0 of libSass, an open source C++ implementation of Sass developed by Catlin, Aaron Leung, and the engineering team at Moovweb. Current Sass maintainer, Chris Eppstein, has expressed intent to contribute as well.According to Catlin, libSass can be "drop into anything and it will have Sass in it...You could drop it right into Firefox today and build Firefox and it will compile in there. We wrote our own parser from scratch to make sure that would be possible."
The design goals of libSass are:
- Performance – Developers have reported 10x speed up increases over the Ruby implementation of Sass.
- Easier integration – libSass makes it easier to integrate Sass into more software. Before libSass, tightly integrating Sass into a language or software product required bundling the entire Ruby interpreter. By contrast, libSass is a statically linkable library with zero external dependencies and C-like interface, making it easy to wrap Sass directly into other programming languages and tools. For example, open source libSass bindings now exist for Node, Go, and Ruby.
- Compatibility – libSass's goal is full compatibility with the official Ruby implementation of Sass. This goal has been achieved on libsass 3.3.
IDE integration