Loading...

Go Back

Next page
Go Back Course Outline

CSS Full Course


CSS Preprocessors (Introduction)

CSS Preprocessors Tutorial

CSS preprocessors are scripting languages that extend the capabilities of regular CSS. These preprocessors allow for more efficient and maintainable styles, with features such as variables, mixins, nesting, and more. Let's explore the fundamentals of popular CSS preprocessors like SASS/SCSS, LESS, and Stylus.

1. What are CSS Preprocessors?

CSS preprocessors enhance CSS with additional functionality. They allow developers to write more efficient and organized code using variables, mixins, nesting, and more. After writing in a preprocessor language, it needs to be compiled into standard CSS that browsers can interpret.

2. SASS/SCSS Fundamentals

SASS and SCSS are the most popular CSS preprocessors. SCSS is an extension of CSS and includes additional features. SASS has a more compact syntax, but SCSS is widely used due to its similarity to CSS.

SCSS Example:

                        // Variables
                        $primary-color: #3498db;
                        $font-size: 16px;
                        
                        // Nesting
                        nav {
                          ul {
                            list-style-type: none;
                          }
                          li {
                            display: inline;
                            margin: 0 10px;
                          }
                          a {
                            text-decoration: none;
                            color: $primary-color;
                          }
                        }
                        
                        // Mixins
                        @mixin border-radius($radius) {
                          -webkit-border-radius: $radius;
                          -moz-border-radius: $radius;
                          border-radius: $radius;
                        }
                        
                        // Using Mixin
                        button {
                          @include border-radius(5px);
                          background-color: $primary-color;
                          color: white;
                        }
                                

Output (Compiled CSS):

                        nav ul {
                          list-style-type: none;
                        }
                        
                        nav li {
                          display: inline;
                          margin: 0 10px;
                        }
                        
                        nav a {
                          text-decoration: none;
                          color: #3498db;
                        }
                        
                        button {
                          -webkit-border-radius: 5px;
                          -moz-border-radius: 5px;
                          border-radius: 5px;
                          background-color: #3498db;
                          color: white;
                        }
                                    


3. LESS Example

LESS is another CSS preprocessor with similar features. It allows variables, mixins, and nesting just like SASS/SCSS, but with a slightly different syntax.

LESS Example:

                        // Variables
                        @primary-color: #3498db;
                        @font-size: 16px;
                        
                        // Nesting
                        nav {
                          ul {
                            list-style-type: none;
                          }
                          li {
                            display: inline;
                            margin: 0 10px;
                          }
                          a {
                            text-decoration: none;
                            color: @primary-color;
                          }
                        }
                        
                        // Mixins
                        .border-radius(@radius) {
                          -webkit-border-radius: @radius;
                          -moz-border-radius: @radius;
                          border-radius: @radius;
                        }
                        
                        // Using Mixin
                        button {
                          .border-radius(5px);
                          background-color: @primary-color;
                          color: white;
                        }
                                

Output (Compiled CSS):

                        nav ul {
                          list-style-type: none;
                        }
                        
                        nav li {
                          display: inline;
                          margin: 0 10px;
                        }
                        
                        nav a {
                          text-decoration: none;
                          color: #3498db;
                        }
                        
                        button {
                          -webkit-border-radius: 5px;
                          -moz-border-radius: 5px;
                          border-radius: 5px;
                          background-color: #3498db;
                          color: white;
                        }
                                    

4. Stylus Example

Stylus is a more flexible preprocessor, allowing developers to omit semicolons, curly braces, and parentheses. It's very dynamic and gives developers more control over the syntax.

Stylus Example:

                        // Variables
                        primary-color = #3498db
                        font-size = 16px
                        
                        // Nesting
                        nav
                          ul
                            list-style-type none
                          li
                            display inline
                            margin 0 10px
                          a
                            text-decoration none
                            color primary-color
                        
                        // Mixins
                        border-radius(radius)
                          -webkit-border-radius radius
                          -moz-border-radius radius
                          border-radius radius
                        
                        // Using Mixin
                        button
                          border-radius(5px)
                          background-color primary-color
                          color white
                                

Output (Compiled CSS):

                        nav ul {
                          list-style-type: none;
                        }
                        
                        nav li {
                          display: inline;
                          margin: 0 10px;
                        }
                        
                        nav a {
                          text-decoration: none;
                          color: #3498db;
                        }
                        
                        button {
                          -webkit-border-radius: 5px;
                          -moz-border-radius: 5px;
                          border-radius: 5px;
                          background-color: #3498db;
                          color: white;
                        }
                                    

5. Compilation Process

CSS preprocessors require compilation before they can be used in the browser. Tools like Gulp, Webpack, or Node-sass are often used for automatic compilation. This allows you to write preprocessor code and then automatically generate the standard CSS files.

6. Variables

Variables allow you to store values such as colors, font sizes, or measurements that can be reused throughout your stylesheets.

Example (SCSS):

                        // Variables
                        $primary-color: #3498db;
                        $font-size: 16px;
                        
                        body {
                          color: $primary-color;
                          font-size: $font-size;
                        }
                                

Output (Compiled CSS):

                        body {
                          color: #3498db;
                          font-size: 16px;
                        }
                                    


7. Nesting

Nesting allows you to write your CSS in a hierarchical way, mirroring the structure of your HTML.

Example (SCSS):

                        // Nesting
                        nav {
                          ul {
                            list-style-type: none;
                          }
                          li {
                            display: inline;
                            margin: 0 10px;
                          }
                          a {
                            text-decoration: none;
                            color: $primary-color;
                          }
                        }
                                

Output (Compiled CSS):

                        nav ul {
                          list-style-type: none;
                        }
                        
                        nav li {
                          display: inline;
                          margin: 0 10px;
                        }
                        
                        nav a {
                          text-decoration: none;
                          color: #3498db;
                        }
                                    

8. Mixins

Mixins allow you to create reusable pieces of code that can be included in other selectors.

Example (SCSS):

                        // Mixins
                        @mixin border-radius($radius) {
                          -webkit-border-radius: $radius;
                          -moz-border-radius: $radius;
                          border-radius: $radius;
                        }
                        
                        button {
                          @include border-radius(5px);
                          background-color: $primary-color;
                          color: white;
                        }
                                

Output (Compiled CSS):

                        button {
                          -webkit-border-radius: 5px;
                          -moz-border-radius: 5px;
                          border-radius: 5px;
                          background-color: #3498db;
                          color: white;
                        }
                                    

9. Inheritance

Inheritance allows you to extend existing styles and apply them to other selectors.

Example (SCSS):

                        // Inheritance
                        .button {
                          background-color: $primary-color;
                          color: white;
                          padding: 10px 20px;
                          border-radius: 5px;
                        }
                        
                        .primary-button {
                          @extend .button;
                          background-color: green;
                        }
                                

Output (Compiled CSS):

                        .button {
                          background-color: #3498db;
                          color: white;
                          padding: 10px 20px;
                          border-radius: 5px;
                        }
                        
                        .primary-button {
                          background-color: green;
                          color: white;
                          padding: 10px 20px;
                          border-radius: 5px;
                        }
                                    
Go Back

Next page