If I have this SCSS/SASS:
$a: #fff;
$b: #fff;
$c: #fff;
$d: #fff;
Is there any shortcut for this? In other words, how do I easily assign the same value to multiple variables?
CSS Variables to the Rescue CSS variables is a standard which enables you to have variables in CSS. SCSS is compiled to CSS during compile time, and SCSS variables are replaced with resolved value during compile time, which means there is no way to change the variable during run time.
SASS (Syntactically Awesome Style Sheets) is a pre-processor scripting language that will be compiled or interpreted into CSS. SassScript is itself a scripting language whereas SCSS is the main syntax for the SASS which builds on top of the existing CSS syntax.
To convert the SASS variable to a CSS custom property you put curly brackets around it, and a hash in front. If you've used template literals in JavaScript it's the same thing, just with a # instead of a $ (because we already have $ in the variable name).
If you for some reason need them to hold the same value but need to keep them as separate variables for semantics or readability, or for enabling simple changes down the road you could just do this:
$a: #fff;
$b: $a;
$c: $a;
$d: $a;
I do this from time to time when dealing with colors that might change during the development process. Here's an example:
$background_color: white;
$text_color: #444;
$link_color: $text_color;
$overlay_color: rgba($background_color, 0.7);
This is useful since it will allow me to change the value of $text_color
and have $link_color
reflect the same changes, while at the same time being able to set $link_color
to something entirely different. Using only one variable for $text_color
and $link_color
alike would mean I'd have to manually look all instances over to see which relates to text and which relates to links. I think a good practice is to name you variables by what they're used for, i.e. $text_color
and $link_color
rather than $blueish_color
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With