Skip to main content

Getting Started with Sass

Sass (Syntactically Awesome Style Sheets) is a popular CSS preprocessor that extends CSS with features like variables, nesting, mixins, and more. In this tutorial, we'll cover the basics of Sass, including its syntax and some of its core features.

Sass Syntax

Sass has two syntaxes: SCSS (Sassy CSS) and the older, indented syntax. In this tutorial, we'll focus on SCSS, which is more similar to regular CSS.

Installing Sass

To use Sass, you need to install it on your computer. Visit the official Sass website (https://sass-lang.com/install) for installation instructions.

Variables

In Sass, you can declare variables to store reusable values like colors, fonts, and sizes. Variables start with a $ symbol.

Example:

Sass
$primary-color: #ff9800;
$secondary-color: #673ab7;
$font-family: 'Roboto', sans-serif;

body {
background-color: $primary-color;
font-family: $font-family;
}

a {
color: $secondary-color;
}

Nesting

Sass allows you to nest selectors, making your code cleaner and more organized.

Example:

Sass
nav {
background-color: #333;

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

li {
display: inline-block;

a {
color: #fff;
text-decoration: none;
padding: 10px;
}
}
}
}

Mixins

Mixins are reusable blocks of code that can be included in other rulesets. You can define a mixin using the @mixin directive and include it with the @include directive.

Example:

Sass
@mixin box-shadow($x: 0, $y: 0, $blur: 4px, $color: rgba(0, 0, 0, 0.5)) {
box-shadow: $x $y $blur $color;
}

.button {
@include box-shadow(0, 2px, 8px);
background-color: #4caf50;
color: #fff;
padding: 10px 20px;
text-decoration: none;
}

Conclusion

In this tutorial, we introduced Sass, its syntax, and some of its core features like variables, nesting, and mixins. Sass can help you write more maintainable and efficient CSS code, making your development process faster and more enjoyable.