Languages
[Edit]
EN

SCSS - variable declaration

0 points
Created by:
Laylah-Walsh
654

In this article, we would like to show you how to declare variable in SCSS.

In order to declare variable we need to precede it with $ sign with the following syntax:

$variable-name: expression;

Where:

  • expression - may be some value, other variable, some calculation or a function call. 

Note:

Don't confuse SCSS variables with SASS variables.

 

Practical examples

Example 1

In this example, we create primary-color variable with a simple value of green and assign it as a background-color of our element class.

$primary-color: green;

.element {
    background-color: $primary-color;
}

Example 2

We can also use variables with more complex expression as their value, for example specify whole border:

$primary-border: 1px solid red;

.element {
    border: $primary-border;
}

 

See also

  1. SCSS / SASS - installation

  2. SCSS - compile and minify to css file

  3. React - style components using CSS modules

References

  1. Sass: Variables

Alternative titles

  1. SCSS - create variable in .scss file
  2. SCSS - how to create and use variable
  3. SCSS - how to declare and use variable
  4. SCSS - create variable
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join