🌐
Sass
sass-lang.com › documentation › variables
Sass: Variables
Sass variables are simple: you assign a value to a name that begins with $, and then you can refer to that name instead of the value itself. But despite their simplicity, they’re one of the most useful tools Sass brings to the table.
🌐
W3Schools
w3schools.com › sass › sass_variables.asp
Sass Variables
Sass String Sass Numeric Sass List Sass Map Sass Selector Sass Introspection Sass Color ... Variables are a way to store information that you can re-use later.
🌐
CodiLime
codilime.com › blog › software development › frontend › css vs. scss variables — main differences and use cases
CSS vs. SCSS variables — main differences and use cases
August 13, 2023 - SCSS accepts any valid CSS syntax but with the added benefits of advanced features that makes writing CSS code easier and more efficient. SCSS variables are declared using the `$` symbol followed by a name and a value, like this:
🌐
Sass
sass-lang.com › guide
Sass: Sass Basics
You can store things like colors, font stacks, or any CSS value you think you’ll want to reuse. Sass uses the $ symbol to make something a variable.
🌐
Sass
sass-lang.com › documentation › breaking-changes › css-vars
Sass: Breaking Change: CSS Variable Syntax
The CSS spec allows almost any string of characters to be used in a custom property declaration. Even though these values might not be meaningful for any CSS property, they could be accessed from JavaScript. When they were parsed as SassScript values, syntax that would have been valid plain ...
🌐
Tailwind CSS
tailwindcss.com › docs › theme
Theme variables - Core concepts - Tailwind CSS
Theme variables are special CSS variables defined using the @theme directive that influence which utility classes exist in your project.
🌐
Bootstrap
getbootstrap.com › docs › 5.3 › customize › sass
Sass · Bootstrap v5.3
Some of our Sass maps are merged into empty ones by default. This is done to allow easy expansion of a given Sass map, but comes at the cost of making removing items from a map slightly more difficult. All variables in the $theme-colors map are defined as standalone variables.
Find elsewhere
🌐
Vuetify
vuetifyjs.com › en › features › sass-variables
SASS variables — Vuetify
Customize Vuetify's internal styles by modifying SASS variables.
🌐
Vanseo Design
vanseodesign.com › home › blog › css › how to use sass variables
How To Use Sass Variables - Vanseo Design
October 13, 2015 - Sass Variables are a way to store information that you want to reuse throughout your stylesheet. Variables are coming to CSS, but there’s not much support at the moment and it’s going to be awhile before you can use them.
🌐
Devhints
devhints.io › css › sass cheatsheet
Sass cheatsheet
Variables · mixins · darken() · adjust-color() · @for @each @while @if @else · $list: (a b c) · $map: (a: b, c: d) · One-page guide to Sass
🌐
Kevin Powell
courses.kevinpowell.co › view › courses › sass-essentials › 50294-variables
Variables
Do you want to learn how to make some kick-ass websites? Between teaching in a physical classroom, my YouTube channel, and the premium courses available here, I teach people how to make the web, and how to make it look good while they are at it.
🌐
SitePoint
sitepoint.com › blog › gems › an introduction to sass in rails
An Introduction to Sass in Rails - May 2020 Edition
November 6, 2024 - If you’re like me, you like throwing around the same colors or gradients on the page with some slight variation. The hardest part is remembering them all, but SASS lets you store colors as variables. Wouldn’t it make more sense to use $androidgreen rather than #A4C639?
🌐
Portland Community College
pcc.edu › web-services › style-guide › basics › extras › variables
Sass variables | Web services at PCC
There are variables we use in our Sass stylesheets to make sure things like color and spacing are consistent throughout the stylesheet. Variables make it so you don’t have to repeat the same HEX or pixel value over and over, which cuts down on development time and leaves less room for human error.
Top answer
1 of 9
456

You can do it like this:

I have a folder named utilities and inside that I have a file named _variables.scss

in that file i declare variables like so:

$black: #000;
$white: #fff;

then I have the style.scss file in which i import all of my other scss files like this:

// Utilities
@import "utilities/variables";

// Base Rules
@import "base/normalize";
@import "base/global";

then, within any of the files I have imported, I should be able to access the variables I have declared.

Just make sure you import the variable file before any of the others you would like to use it in.

2 of 9
283

This question was asked a long time ago so I thought I'd post an updated answer.

You should now avoid using @import. Taken from the docs:

As of Dart Sass 1.80.0, the @import rule is deprecated and will be removed from the language in Dart Sass 3.0.0. Prefer the @use rule instead.

A full list of reasons can be found here

You should now use @use as shown below:

_variables.scss

$text-colour: #262626;

_otherFile.scss

@use 'variables'; // Path to _variables.scss Notice how we don't include the underscore or file extension

body {
  // namespace.$variable-name
  // namespace is just the last component of its URL without a file extension
  color: variables.$text-colour;
}

You can also create an alias for the namespace:

_otherFile.scss

@use 'variables' as v;

body {
  // alias.$variable-name
  color: v.$text-colour;
}

EDIT As pointed out by @und3rdg at the time of writing (November 2020) @use is currently only available for Dart Sass and not LibSass (now deprecated) or Ruby Sass. See https://sass-lang.com/documentation/at-rules/use for the latest compatibility

🌐
Start Bootstrap
startbootstrap.com › theme › sb-admin-2
SB Admin 2 - Free Bootstrap Admin Theme
A free Bootstrap admin theme, dashboard, or web application UI. All Start Bootstrap templates are free to download and open source.
Top answer
1 of 1
44

SCSS is a preprocessor. That means it is not CSS, but is converted into CSS at 'compile time'. In the resulting CSS code there is no resemblance to the original SCSS code. Hence you cannot change the variable values at CSS 'runtime'.

Historically SCSS is a fairly old technique. Actually it dates back to as far as 2007. It was invented by the motivation that CSS lacks certain features amongst which are variables (and nesting and loops and mixins etc.).

CSS variables are a quite recent addition to the CSS standard (The last call working draft seams to be from 2014). They didn't render SCSS variables useless, because you can do things to SCSS variables which you can't do with CSS variables (to my knowledge) like color calculations.

On the other hand you can do things with CSS variables that you can't do with SCSS variables like changing at runtime.

BTW: CSS variables are not the official jargon. They should be called custom properties. But everyone calls them variables.

Addendum 1

One difference I was not talking about. You can't change SCSS variables with JavaScript. CSS Custom Properties however can be accessed and changed with JS

Addendum 2

This article on CSS Tricks has a good overview: https://css-tricks.com/difference-between-types-of-css-variables/

Addendum 3

As pointed out setting custom properties in CSS is the prefered way for reasons of freedom for later manipulation. However media queries can not handle custom propertien. So for media queries for instance things like breakpoints and the like SCCS or Sass variables are still (2024) the way to go.

🌐
CSS-Tricks
css-tricks.com › introducing-sass-modules
Introducing Sass Modules | CSS-Tricks
October 14, 2019 - This is similar to the $with argument in load-css(). but rather than using variable-names as keys, we use the variable itself, starting with $. I love how explicit this makes configuration, but there’s one rule that has tripped me up several times: a module can only be configured once, the first time it is used. Import order has always been important for Sass, even with @import.
🌐
GitHub
github.com › sass › sass › issues › 3091
Convert Sass variables to CSS variables · Issue #3091 · sass/sass
It turns out that there is a possibility to have dynamic Sass variables. The implementation shouldn't be that hard. Just compile $primary to var(--primary) and CSS will do the work for us! Honestly, it seems that I wasn't the first to have this idea. Sass-to-css-variables have already implemented that; however, I'm not sure about this project as it has been updated 4 years ago.
Author   ghost