How to Use Variables in SCSS for Better Styling

How to Use Variables in SCSS for Better Styling

April 13, 2026

What are Variables in SCSS?

Variables in SCSS are used to store reusable values like colors, fonts, sizes, and spacing. They help avoid repeating the same values multiple times in your stylesheet. Variables start with the $ symbol. This feature makes your code cleaner and more maintainable. Instead of hardcoding values, you can define them once and reuse them. It is especially useful in large projects. Variables improve consistency across your design.

Example:

$primary-color: #3498db;

h1 {
color: $primary-color;
}

Why Use Variables in SCSS?

Using variables saves time and effort when updating styles. If you change a variable value, it updates everywhere it is used. This reduces errors and improves efficiency. Variables help maintain a consistent design system. They are useful for themes and branding. Developers can quickly adjust layouts and colors. It also makes teamwork easier by keeping styles standardized.

Example:

$font-size: 16px;

p {
font-size: $font-size;
}

Defining and Naming Variables

Variables should be named clearly based on their purpose. Good naming improves readability and understanding. You can define variables at the top of your SCSS file. Use meaningful names like $primary-color, $heading-size, etc. Avoid using vague names. This helps in maintaining the project easily. Organized variables make styling faster.

Example:

$primary-color: #ff5733;
$secondary-color: #2ecc71;
$base-font: 14px;

Using Variables in Styles

Once variables are defined, you can use them anywhere in your SCSS file. Just call them using the $ symbol. This reduces repetition of values. It keeps your code clean and readable. Variables can be used for colors, spacing, borders, and more. They make updates very simple. This improves workflow efficiency.

Example:

$btn-color: #007bff;

button {
background-color: $btn-color;
border: 1px solid $btn-color;
}

Scope of Variables

SCSS variables have scope, meaning they can be local or global. Global variables are available throughout the file. Local variables are limited to a specific block. Understanding scope helps avoid conflicts. You can control where variables are used. This improves code structure. It is important for large projects.

Example:

$color: blue;

.container {
$color: red;
color: $color; // red
}

p {
color: $color; // blue
}

Default Values in Variables

SCSS allows setting default values using !default. This means the variable will only be assigned if it is not already defined. It is useful for creating flexible themes. Developers can override default values easily. This is commonly used in frameworks. It improves customization options. Helps in reusable code design.

Example:

$primary-color: blue !default;

body {
color: $primary-color;
}

Real-Time Use Case (Theme Styling)

Variables are widely used in theme customization. For example, you can define brand colors and apply them across the site. If branding changes, just update the variable. This saves a lot of time. It ensures consistent UI design. It is very helpful in large websites. Makes design updates quick and easy.

Example:

$brand-color: #e74c3c;

.header {
background: $brand-color;
}

.footer {
background: $brand-color;
}

Final Tip

Always group your variables in a separate file like _variables.scss and import it into your main file. This keeps your project well-organized and easy to manage.