Makara Nuol
Technology

CSS Preprocessors (Sass, Less)

Author

Makara Nuol

Date Published

CSS Preprocessor

What are CSS Preprocessors?

CSS preprocessors are scripting languages that compile to CSS. They offer enhanced features and functionality beyond what's available in vanilla CSS, enabling developers to write more modular, maintainable, and efficient code.

Key points to consider:

- Preprocessors allow developers to use variables, nesting, mixins, and other programming concepts in their CSS code.

- The preprocessed code is compiled into regular CSS that browsers can understand.

- Preprocessors aim to solve common CSS limitations and improve development workflows.

Sass (Syntactically Awesome StyleSheets)

Sass is the most popular and oldest CSS preprocessor, initially released in 2006

Key features of Sass:

1. Variables: Allow storing and reusing values throughout your stylesheet.

2. Nesting: Enables writing more organized and readable CSS by nesting selectors.

3. Mixins: Reusable pieces of CSS that can accept parameters.

4. Functions: Built-in and custom functions for manipulating colors, numbers, and strings.

5Control Directives: Conditional statements and loops for creating dynamic styles

1// Variables
2$primary-color: #333;
3$font-stack: Helvetica, sans-serif;
4
5// Nesting
6nav {
7 ul {
8 margin: 0;
9 padding: 0;
10 list-style: none;
11 }
12
13 li { display: inline-block; }
14
15 a {
16 display: block;
17 padding: 6px 12px;
18 text-decoration: none;
19 }
20}
21
22// Mixin
23@mixin transform($property) {
24 -webkit-transform: $property;
25 -ms-transform: $property;
26 transform: $property;
27}
28
29.box { @include transform(rotate(30deg)); }
30