DEV Community

Cover image for Learning SCSS in 5 Minutes?
Rahul
Rahul

Posted on

Learning SCSS in 5 Minutes?

Let's learn the basics of SCSS in 5 Minutes. So basically SCSS means Sassy CSS. It's a CSS preprocessor that gives you access to useful features that are not available in Vanilla(normal) CSS.


VARIABLES

To create variable just add a $ sign to the variable name and set them like a normal CSS property.

//color palette for a project to maintain consistency

$product-dark-blue: #324e85;
$product-light-blue: #4c7396;

.element {
    coloe: $product-dark-blue;
}
Enter fullscreen mode Exit fullscreen mode

NESTING

SCSS helps you write cleaner and concise CSS.

.container {
    width: 100%; 
    color: grey; 
    background-color: green; 

    div {
        border: 1px solid black; 
        a {
            text-decoration: none; 
            color: white;
            &::hover {
                color: #b59a9a;
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

INHERITANCE

@extends help you inherit the properties of another class.

.header {
    color: grey; 
}
.sub-header{
    @extend .header; 
    font-size: 40px;
}
Enter fullscreen mode Exit fullscreen mode

@Mixin

Mix in is another way SCSS implement inheritance using @mixin.

// create mixin
@mixin red-color {
    color: grey; 
}

.header {
    @include red-color; /*add mixin*/
}
Enter fullscreen mode Exit fullscreen mode

Operators

SCSS offers you different kind of operators that you can use in your CSS. Arithmetic operators like +,-,*,/ etc.

@mixin top-margin ($margin){
    margin-top: 30px + $margin; 
}
.container {
    width: 800px -80px;
    @include top-margin(10px); 
}
Enter fullscreen mode Exit fullscreen mode

LOOPS

We can use for loops in our CSS thanks again to SCSS. You can create utility classes for your color, font-size and a lot of other properties.

/* generate utility classes for font-size */

@for $x from 1 through 70 {
    .font-size-#{$x} {
        font-size: 0px + $x;
    }
}

Enter fullscreen mode Exit fullscreen mode

CONDITIONS

Another awesome feature of scss is the ability to use if/else statements in CSS.

$bg: pink;
$bg-mobile: red;

p{
    @if $bg == pink {

        color: blue;
    } @else if $bg-mobile == red {
        color: green;
    } @else {
        color: grey;
    }
}
Enter fullscreen mode Exit fullscreen mode

DONE PEOPLE.
Now you know something about SCSS.

⚡Thanks For Reading | Happy Coding🤩

Get weekly newsletter of amazing articles I posted this week and some offers or announcement. Subscribe from Here

Top comments (3)

Collapse
 
khangnd profile image
Khang

Although the article is estimated "2 mins read" 😄
Jk, thanks for sharing.

Collapse
 
shaijut profile image
Shaiju T • Edited

Valuable and to the point 😄

Collapse
 
hemazyn profile image
Hemazyn

Woah
I really love it.