Project 3:CSS Variables
Tags: javascript, css, webdev, html
Published: 2021-12-04 08:25:36

Welcome to my "Build 30 Js Projects in 30 Days" Series .This is day 3 and project 3. If you haven't read the other articles in this series please check them out first. I'll list them at the end of this article.
 

As mentioned in my previous article. This is the Day 3 challenge of [Wes Bos Javascript30 course](https://javascript30.com/).

Here is the final result:


As always before starting download the starter files from [here](https://github.com/wesbos/JavaScript30). I've made a separate article on how to download starter files, you can check it out [here](https://dev.to/prachigarg19/build-30-javascript-projects-in-30-daysday-0-2m8j).

##**CODE**
{% codepen https://codepen.io/prachigarg19/pen/yLzYoLW %}
## *PART 1:HTML*

In html part as you can see I've created 3 inputs. The thing to notice here is that I've given name to the input similar to variable names as we will later use them to manipulate variable values. I've created data-sizing variable which will store px. Keep reading to understand why it's needed. 

## *PART 2:CSS*
Now we are going to style our project.
Here we have created 3 variables in root. As I said, notice variable names are similar to name of inputs.

## *PART 3:JAVASCRIPT*
Now we will make our project interactive.
The idea is to get all input tags from document, traverse them and call function updateValues when input is clicked or when mouse is moved upon range input to change values. Without mousemove event Listener the function will not take values until range input is stopped at a point. 

<u>updateValue function-></u>

```
 let suffix=this.dataset.sizing||"";
    document.documentElement.style.setProperty(`--${this.name}`,this.value+suffix);
```
suffix will store the dataset sizing value that is px. This is required as we want to set variable values to somethingpx.
Example- If user sets padding value to 18 then we need to update
 --padding variable to 18px. Thus we need to concatenate 18 and string px to achieve this. 
Also since image does not have a data-sizing set to it we have set an or condition in suffix initialisation to prevent concatenation of undefined.This can also be achieved by setting data-sizing="" in img but this is not a good method for larger code bases as we'd then need to add data-sizing="" to all the inputs.

Now we will update css value by calling setProperty function of style .of document element ,which is further a part of document ,for name of the input that called the function (same as variable name) to the value of this input(returns value set by user/initial value set by programmer). (Tip: this is a complicated part hence try breaking it and printing on console to get the idea or you can comment below and I'll be happy to help you)
For more details on this part ,read [here](https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration/setProperty).


## *<u>Previous article from this series:</u>*
[Day 2 Project 2](https://dev.to/prachigarg19/project-2javascript-clock-5a0c)

Here is the working code :[github](https://github.com/prachigarg19/Javacript_beginner_projects/tree/main/js30)
##**<u>Things learnt:</u>**

1.mousemove event listener.
2.css variables.
3.updating variables from javascript
4.dataset
5.filter property in css (here used to blur image)

## *<u>Conclusion</u>*

That's it for today's project.Next project will be Array Cardio Day 1.

If you have any doubts or suggestions please do let me know in the comment section. I'll be more than happy to interact with you.

If you like this series and want to be a part of it, do consider following me at @prachigarg19

Thanks for reading. :)