What is sass?
The sass npm package is a preprocessor scripting language that is interpreted or compiled into Cascading Style Sheets (CSS). It enables developers to use variables, nested rules, mixins, functions, and more, which can help in writing CSS in a more structured and maintainable way.
What are sass's main functionalities?
Variables
Variables allow you to store values that you can reuse throughout your stylesheet.
$primary-color: #333;
body {
color: $primary-color;
}
Nesting
Nesting enables you to nest your CSS selectors in a way that follows the same visual hierarchy of your HTML.
nav {
ul {
margin: 0;
padding: 0;
list-style: none;
}
li { display: inline-block; }
a {
display: block;
padding: 6px 12px;
text-decoration: none;
}
}
Partials and Import
Partials are Sass files named with a leading underscore. You can import these partials into other Sass files to modularize your CSS and help keep things easier to maintain.
// _reset.scss
html,
body,
ul,
ol {
margin: 0;
padding: 0;
}
// main.scss
@import 'reset';
body {
font-family: sans-serif;
}
Mixins
Mixins allow you to define styles that can be reused throughout your stylesheet.
@mixin border-radius($radius) {
-webkit-border-radius: $radius;
-moz-border-radius: $radius;
-ms-border-radius: $radius;
border-radius: $radius;
}
.box { @include border-radius(10px); }
Extend/Inheritance
Extend/Inheritance lets you share a set of CSS properties from one selector to another.
.message {
border: 1px solid #ccc;
padding: 10px;
color: #333;
}
.success {
@extend .message;
border-color: green;
}
Operators
Sass supports standard math operators like +, -, *, /, and %.
.container {
width: 100%;
}
.article {
width: 600px / 960px * 100%;
}
Other packages similar to sass
less
Less is a backward-compatible language extension for CSS. It provides similar features to Sass, such as variables, mixins, and nesting. However, Less uses JavaScript for compilation, whereas Sass was originally written in Ruby and now primarily uses a C implementation (Dart Sass).
stylus
Stylus is a preprocessor that offers a more flexible syntax than Sass and Less, with optional semicolons and braces. It also provides powerful features like variable interpolation and iteration. Stylus can be more terse and expressive in some cases but might have a steeper learning curve for those used to more traditional CSS syntax.
postcss
PostCSS is a tool for transforming CSS with JavaScript plugins. While it is not a preprocessor in the traditional sense, it can be used to achieve many of the same goals as Sass through its extensive plugin ecosystem. PostCSS is highly customizable and can be tailored to specific build processes and requirements.
Sass.js
JavaScript implementation of Sass. Great for node.js and other
frameworks supporting the CommonJS module system.
Installation
npm:
$ npm install sass
Usage
var sass = require('sass')
sass.render('... string of sass ...')
// => '... string of css ...'
sass.collect('... string of sass ...')
// => { selectors: [...], variables: { ... }, mixins: { ... }}
// foo
body
// bar
a
:color #fff
compiles to
body a {
color: #fff;}
Variables
!red = #ff0000
body
:color !red
and
red: #ff0000
body
:color !red
compile to
body {
color: #ff0000;}
Selector Continuations
a
:color #fff
&:hover
:color #000
&.active
:background #888
&:hover
:color #fff
compiles to
a {
color: #fff;}
a:hover {
color: #000;}
a.active {
background: #888;}
a.active:hover {
color: #fff;}
Literal JavaScript
type: "solid"
size: 1
input
:border { parseInt(size) + 1 }px {type} #000
compiles to
input {
border: 2px "solid" #000;}
Property Expansion
div
=border-radius 5px
compiles to
div {
-webkit-border-radius: 5px;
-moz-border-radius: 5px;}
Mixins
+large
:font-size 15px
+striped
tr
:background #fff
+large
&:odd
:background #000
table
+striped
:border none
compiles to
table {
border: none;}
table tr {
background: #fff;}
table tr {
font-size: 15px;}
table tr:odd {
background: #000;}
Testing
Update Git submodules and execute:
$ make test
More Information
License
(The MIT License)
Copyright (c) 2009 TJ Holowaychuk <tj@vision-media.ca>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.