You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- 1.6. [CSS Variables vs Preprocessor Variables](#16-css-variables-vs-preprocessor-variables) | [Difference Between CSS Variables and Preprocessor Variables](#16-difference-between-css-variables-and-preprocessor-variables)
65
-
65
+
- 1.7. [CSS Variables with JavaScript](#17-css-variables-with-javascript)
66
+
66
67
1 Introduction to CSS Variables Custom Properties
67
68
=====================
68
69
@@ -768,3 +769,134 @@ Variables are one of the major reasons why CSS preprocessors like `SASS` or `LES
768
769
| One can easily access and overwrite CSS variables inside Media Query (as and when media or resolution changes the browser recheck/reassign/repaints the variable if needed | Sometimes it is not possible with preprocessor variables |
769
770
770
771
> **Note**: As Browser understands only CSS styling, the preprocessor code/variables would do nothing in a browser, The browser wouldn't understand the declarations and toss them out, that's the reason why Preprocessors files need to compile/converted into native CSS before sending/viewing into the browser
772
+
773
+
1.7. CSS Variables with JavaScript
774
+
---------------------
775
+
776
+
- One more super cool thing you can do is access CSS variables directly from your JavaScript code
777
+
- One of the important benefits of CSS Variables is that it can interact via the power of JavaScript
778
+
- While dealing with CSS Variables JavaScript widely uses `getComputedStyle()``getProperty()` and `style.setProperty()` methods
// get all the styles/CSSStyleDeclaration for root
878
+
var rootStyles =getComputedStyle(root);
879
+
console.log('rootStyles', rootStyles);
880
+
881
+
// get --base-bg-color variable value available inside root styles
882
+
// var baseBgColor = rootStyles.getPropertyValue('--base-bg-color');
883
+
// console.log('baseBgColor', baseBgColor);
884
+
885
+
root.style.setProperty('--base-bg-color', '#f66969') // red- #f66969; green - #66f969; blue- #6696f9;
886
+
```
887
+
888
+
<p>
889
+
<figure>
890
+
<img src="_images-css-variables/1.7.1.1-css-variables-javascript-interaction-original-green.png" alt="CSS variable interaction with JavaScript Original Green output" title="CSS variable interaction with JavaScript Original Green output" width="1000" border="2" />
891
+
<figcaption> Image 1.7.1.1 - CSS variable interaction with JavaScript Original Green output </figcaption>
892
+
</figure>
893
+
</p>
894
+
895
+
<hr/>
896
+
897
+
<p>
898
+
<figure>
899
+
<img src="_images-css-variables/1.7.1.2-css-variables-javascript-interaction-set-red.png" alt="CSS variable interaction with JavaScript updated Red output" title="CSS variable interaction with JavaScript updated Red output" width="1000" border="2" />
900
+
<figcaption> Image 1.7.1.2 - CSS variable interaction with JavaScript updated Red output </figcaption>
0 commit comments