You can scope your variables locally inside each parent container, set them to your desired color, and the icon inside each container will inherit the appropriate color from its parent. You can order your army of minions to do whatever you want in your code.

You can’t do any of this with preprocessor variables. So, you delegate it to a minion, as shown in example 2. Minion 1, who started at line 1, passes the task along of completing lines 4–5 to Minion 2 so Minion 2 can wait on the completion of the show() method in line 3 before starting work on 4–5. If fallbacks are not included, in case of invalid or unset custom properties, the inherited value will be applied instead. You cannot show a child div if the parent div is hidden. Minion 3 originally received instructions from line 10, and had been waiting to complete them until it was called. Preprocessors like Sass and Less certainly help to keep your CSS codebase organized and maintainable. Depending on your needs, *ngIf or [ngClass]="{hide_element: item.hidden}" where CSS class hide_element is { display: none; } *ngIf can cause issues if you're changing state variables *ngIf is removing, in those cases using CSS display: none; is required. At line 16, the callback has the ability to use the products of the API request on line 14 because the request call has a callback itself! The neat thing about using CSS variables in this case is that you can tweak your animations simply by modifying the variables’ values inside the appropriate selectors. You are the minion master, with hordes of squealing little servants ready to do your bidding. There is a callback within a method at line 9, similar to the jQuery click handler example. If you couldn’t force minions to carry out your orders, you would have to do it all yourself. Things to take in consideration Fallback Color

All right, a little more complicated example! This allows for much more flexibility.

You can’t be distracted with a 20 line long function when you need to be taking other instructions from the user as well! In short, variables have now become an exciting reality in the world of CSS, and as you’ll soon see for yourself, this awesome new technology is pretty straightforward to learn and use. Just remember to define the custom properties inside the selector that targets the element you want to animate, and refer to them with the var() function inside the @keyframes block. So, I am going to use these lovable minions to explain callbacks. This is more convenient than repeating the same chunck of code over and over again to recreate the image. And use text-overflow ellipsis to prevent breakage of the design and let user know more text is there. The minions must take orders from you. Minion 1 can move on to other work. The second is a callback- a function named reportOrders in this case. Minion 3 originally received instructions from line 10, and had been waiting to complete them until it was called. Although in this article I use the term CSS variables for simplicity’s sake, the official spec refers to them as CSS custom properties for cascading variables.

For instance, consider the following JavaScript snippet: number1 and number2 are two variables which store the number 2 and 3 respectively. Notice the value of the stroke property inside the circle element and of the fill property inside the text element of the SVG symbol: they both apply a CSS variable, i.e., --icon-color, which is defined in the :root selector inside the CSS document, like so: This is what the icon looks like at this point: If you now place an SVG icon inside different container elements and localize your variable inside each parent element’s selector with a different color value, you’ll have icons of different colors without having to add any more style rules.

Data sharing is an essential concept to understand before diving into your first Angular project. For instance: The snippet above creates the --my-gradient variable and sets it to the value of both the --top-color and --bottom-color variables to create a gradient.

Now is the time to complete logRes()! Now, localize the --icon-color variable by assigning the value green to it inside the .success selector and check the result. Now that minion is done too. The entire point is to do the request call then pass the results on. I use analogies and imagery. Line 15–16: The global variable “results” is set to the body value. Here’s what the spec has to say about this: [Using CSS variables] makes it easier to read large files, as seemingly-arbitrary values now have informative names, and makes editing such files much easier and less error-prone, as one only has to change the value once, in the custom property, and the change will propagate to all uses of that variable automatically. One way you might have been taking advantage of the flexibility of variables when styling websites is by using preprocessors like Sass and Less. For example, in Math.pow(7, 3), 7 is the base and 3 is the exponent, so the result of the expression is 343. Minion 1 then moves on to the rest of the outer function, without needing to wait. This opens up a whole world of possibilities! This is what I suggest you do. That means every time the readResult() function is called, there must be a callback parameter. Your minion cannot reportOrders until you have spoken the orders! Here is a visual example that explains it better. Callbacks are important here to clearly trace a path that the object must follow. On line 20, I declared the callback function to be reportOrders, but it could be anything!

For example, editing the primary color in your project will be much easier when what you need to change is one value for the --primary-color CSS custom property, rather than change that value inside multiple CSS properties in various places. Finally, you can include one or more fallback value/s with your CSS variable. For instance, to animate an element with the class of .bubble inside an SVG graphic, your CSS could look like this: Notice how you can perform calculations with the var() function using CSS calc(), which adds even more flexibility to your code. The differences spring from the fact that CSS variables are live CSS properties running in the browser, while preprocessor variables get compiled into regular CSS code, therefore the browser knows nothing about them. In this example, if you read the code in order and didn’t have minions at your side, the console would log “One”, “Two”, “Three”. But every tutorial instantly started with complex terms. We probably don't need to teach you how to do basic math, but we would like to test your understanding of the syntax involved. If there were no callbacks in the above example, line 5 could cause an error because the show() method in line 3 had not completed yet. Line 15: I know from line 20 that my minion will need to carry out the reportOrders function. Write powerful, clean and maintainable JavaScript.RRP $11.95. Download Minion Pro font free in ttf format for Windows and Mac. Features like variables, mixins, loops, etc. See the Pen Basic Use of SVG Icon and CSS Variables by SitePoint (@SitePoint) on CodePen.