Skip to content

Commit 4e9ba11

Browse files
committed
Add Define CSS Custom Properties With SCSS Variables as a css til
1 parent fa1a77e commit 4e9ba11

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket.
99

1010
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
1111

12-
_894 TILs and counting..._
12+
_895 TILs and counting..._
1313

1414
---
1515

@@ -102,6 +102,7 @@ _894 TILs and counting..._
102102
- [Clean Up Repetition With :is() Pseudo-Class](css/clean-up-repetition-with-is-pseudo-class.md)
103103
- [Conditional Styling For Unsupported CSS Features](css/conditional-styling-for-unsupported-css-features.md)
104104
- [Create A Pulsing Background With CSS Animation](css/create-a-pulsing-background-with-css-animation.md)
105+
- [Define CSS Custom Properties With CSS Variables](css/define-css-custom-properties-with-scss-variables.md)
105106
- [Dry Up SCSS With Mixins](css/dry-up-scss-with-mixins.md)
106107
- [Give Elements The Same Width With Flexbox](css/give-elements-the-same-width-with-flexbox.md)
107108
- [Lighten And Darken With CSS Brightness Filter](css/lighten-and-darken-with-css-brightness-filter.md)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Define CSS Custom Properties With SCSS Variables
2+
3+
It doesn't work to directly declare a CSS custom property using SCSS variables
4+
like this:
5+
6+
```css
7+
$primary-action: #057A5B;
8+
9+
.btn-primary {
10+
--button-color: $primary-action;
11+
}
12+
```
13+
14+
After SCSS pre-processing, the resulting CSS will look like this:
15+
16+
```css
17+
.btn-primary {
18+
--button-color: $primary-action;
19+
}
20+
```
21+
22+
Instead of the variable being translated into its declared value (`#057A5B` in
23+
this case), it is left as is.
24+
25+
This is because CSS custom property syntax is unusual enough that it gets
26+
processed literally. The only way to incorporate a variable is with
27+
_interpolation_.
28+
29+
```css
30+
$primary-action: #057A5B;
31+
32+
.btn-primary {
33+
--button-color: #{$primary-action};
34+
}
35+
```
36+
37+
Wrapping the SCSS variable in interpolation syntax (`#{ ... }`) will do the
38+
job.
39+
40+
[source](https://sass-lang.com/documentation/style-rules/declarations#custom-properties)

0 commit comments

Comments
 (0)