Webdev

SASS/SCSS (CSS Preprocessor)

SASS (Syntactically Awesome Stylesheets) is a CSS preprocessor that enhances the capabilities of regular CSS by adding features such as variables, nesting, mixins, inheritance, and functions. SCSS is the most popular syntax for SASS, and it’s fully compatible with CSS. It provides a more powerful and efficient way to write and manage styles for complex projects.

Difference Between SASS and SCSS

  1. SASS: The original syntax, inspired by HAML, does not use curly braces ({}) or semicolons (;), but relies on indentation. It’s concise but can be harder to read for those used to CSS.
  2. SCSS: A more modern syntax that looks exactly like CSS but with the additional features of SASS. It uses curly braces and semicolons, making it more familiar for developers used to CSS.

Example:

Key Features of SASS/SCSS

1. Variables

Variables allow you to store values (like colors, fonts, spacing) and reuse them throughout the stylesheet. This makes maintaining and updating your code much easier.

$primary-color: #3498db;
$secondary-color: #2ecc71;
$base-font-size: 16px;

body {
  background-color: $primary-color;
  color: $secondary-color;
  font-size: $base-font-size;
}

2. Nesting

Nesting allows you to nest selectors inside one another, following the same visual hierarchy as the HTML structure. It improves code readability and reduces repetition.

nav {
  background-color: #333;

  ul {
    list-style: none;
    padding: 0;

    li {
      display: inline-block;

      a {
        text-decoration: none;
        color: white;
      }
    }
  }
}

3. Partials and Import

You can split your CSS into multiple files (called “partials”) and import them into a main stylesheet. Partials are files that start with an underscore (_), and they are not compiled into CSS on their own.

Example:

/* _variables.scss */
$primary-color: #3498db;
$font-stack: 'Arial', sans-serif;

/* _header.scss */
header {
  background-color: $primary-color;
  font-family: $font-stack;
}

/* main.scss */
@import 'variables';
@import 'header';

4. Mixins

Mixins allow you to define reusable chunks of code that can be included in other selectors. You can also pass arguments to mixins to make them more dynamic.

@mixin button-style($bg-color) {
  background-color: $bg-color;
  color: white;
  padding: 10px 20px;
  border-radius: 5px;
}

.button-primary {
  @include button-style(#3498db);
}

.button-secondary {
  @include button-style(#2ecc71);
}

5. Inheritance (Extend)

Inheritance allows you to share styles between selectors using @extend. It reduces duplication and lets you inherit common properties from other selectors.

%button {
  padding: 10px 20px;
  border-radius: 5px;
  font-size: 16px;
}

.button-primary {
  @extend %button;
  background-color: #3498db;
}

.button-secondary {
  @extend %button;
  background-color: #2ecc71;
}

6. Functions

SASS functions are similar to mixins but return a value instead of producing a block of CSS. Functions allow you to create reusable, dynamic values.

@function calculate-rem($px-value) {
  @return $px-value / 16 * 1rem;
}

body {
  font-size: calculate-rem(24);
}

7. Control Directives (Loops, Conditionals)

SASS includes programming logic such as loops and conditionals, which allows for more dynamic and efficient CSS generation.

Loops

You can use loops to generate repetitive CSS code.

@for $i from 1 through 5 {
  .column-#{$i} {
    width: 20% * $i;
  }
}
Conditionals

Conditionals allow you to apply styles based on conditions.

@mixin theme($dark) {
  @if $dark == true {
    background-color: black;
    color: white;
  } @else {
    background-color: white;
    color: black;
  }
}

body {
  @include theme(true); // Dark theme
}

Example: Full SCSS in Action

/* _variables.scss */
$primary-color: #3498db;
$secondary-color: #2ecc71;
$base-font-size: 16px;

/* _mixins.scss */
@mixin center-content {
  display: flex;
  justify-content: center;
  align-items: center;
}

/* _buttons.scss */
%button {
  padding: 10px 20px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}

.button-primary {
  @extend %button;
  background-color: $primary-color;
  color: white;
}

.button-secondary {
  @extend %button;
  background-color: $secondary-color;
  color: white;
}

/* main.scss */
@import 'variables';
@import 'mixins';
@import 'buttons';

body {
  font-size: $base-font-size;
}

.container {
  @include center-content;
  height: 100vh;
  background-color: $primary-color;
}

Compiling SASS/SCSS

To convert your SASS/SCSS code into regular CSS, you need a SASS compiler. There are several ways to do this:

  1. SASS CLI: You can install SASS globally via npm and compile your SCSS using the command line:
    npm install -g sass
    sass input.scss output.css
    
  2. Task Runners: You can integrate SASS with task runners like Gulp, Webpack, or Grunt.
  3. Online Compilers: Websites like Sassmeister allow you to write and compile SASS/SCSS directly in the browser.

Conclusion

SASS/SCSS provides advanced features that significantly improve the workflow of managing large-scale stylesheets. By leveraging variables, mixins, nesting, inheritance, and more, SASS allows for a cleaner, more maintainable, and scalable approach to CSS development.


next ->Common Css Properties

go back