<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Anson Li on Medium]]></title>
        <description><![CDATA[Stories by Anson Li on Medium]]></description>
        <link>https://medium.com/@coding_li?source=rss-d94366e79aa0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Xk52icE-W0HiAEjQ2tet3A.jpeg</url>
            <title>Stories by Anson Li on Medium</title>
            <link>https://medium.com/@coding_li?source=rss-d94366e79aa0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 04:08:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@coding_li/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[An Experiment with CSS Variables]]></title>
            <link>https://medium.com/@coding_li/an-experiment-with-css-variables-7acc5b04dfbe?source=rss-d94366e79aa0------2</link>
            <guid isPermaLink="false">https://medium.com/p/7acc5b04dfbe</guid>
            <category><![CDATA[variables]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[html]]></category>
            <category><![CDATA[css]]></category>
            <dc:creator><![CDATA[Anson Li]]></dc:creator>
            <pubDate>Sun, 30 Jul 2017 18:13:41 GMT</pubDate>
            <atom:updated>2017-07-30T20:17:09.530Z</atom:updated>
            <content:encoded><![CDATA[<h4>A Codepen Experiment on Variables and Manipulating Them</h4><h4>The Experiment</h4><p>A little background before this. I’ve recently been introduced to the concept of using variables in CSS without the use of SCSS, SASS, preprocessors, etc., which was something I’ve missed since I’ve started learning web development. While preprocessors and the like have many other functions that still provide a lot of value as a tool to be used alongside of straight HTML/CSS, my opinion of them is that it “clutters” the workflow. I find that the fewer the amount of tools you need for a job, you have less and less of a chance of complications or confusion.</p><p>Coming from a programming background (Python and C) I believe that being able to use variables in any programming language is an extremely powerful and important tool and I’m glad that browser support for CSS Variables have finally caught up to the point where it’s generally acceptable for use.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w43x2zL5PVBlJhlEx5hKAA.png" /><figcaption>Source: <a href="https://caniuse.com/#search=variables">https://caniuse.com/#search=variables</a></figcaption></figure><p>Yes, the axiom of “somebody somewhere out there is already working on your idea” exists, but for learning and demonstration purposes I find that it has much more meaning to perform the experiment myself so I can learn it first-hand. Since I also come from an engineering background, let’s stick to a typical lab report style of approach.</p><p><strong><em>Abstract</em></strong></p><p>This experiment examined some of the use cases of CSS Variables and its potential in providing developers with a more robust and adaptive code that can also be modified easily in the future.</p><p><strong><em>Introduction</em></strong></p><p>The purpose of this experiment was to test the capabilities of using CSS Variables on a surface-level and how similar they can be used like in other programming languages (like Python or C).</p><p>A brief look into how CSS Variables work would conclude that they simply store a string and when called upon will replace the variable’s location with said string. The syntax for defining and calling variables can be seen below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/561/1*DAyM4jcVhMnlXeoVaL3IGQ.png" /><figcaption>Syntax for using CSS variables</figcaption></figure><p>Mandatory code includes <em>:root{} </em>and <em>var(), </em>where<em> variableName </em>is where you enter your desired variable name, “value” is the value you want (e.g. 200px, #1E1E1E, a string inside the quotes, etc.). You set the variable in the <em>:root</em> selector (the double dashes are necessary as a prefix to every variable name) and place the variable within the parentheses of var() to call upon it (again, don’t forget the double dashes).</p><p><strong><em>Method</em></strong></p><p>Codepen was the tool used to demonstrate and perform the experiments on, mainly because it is very easy to share and also does not require the typical setup you would need to build a web page from scratch.</p><p>In addition to CSS Variables, a key function of CSS called <em>calc</em> was used in conjunction to test the limits of what can be done with variables.</p><p><strong><em>Procedure</em></strong></p><p>This experiment tested the limits of what can be stored in a variable and how/if they could be manipulated with <em>calc</em>.</p><p><strong><em>Results</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/846/1*GMROyrkjD8e1xE3k0eYnEQ.png" /><figcaption><a href="https://codepen.io/CodingLi/pen/oebMxB">https://codepen.io/CodingLi/pen/oebMxB</a></figcaption></figure><p>Basic storage of any value that you would normally set an attribute at, as it should be written in proper syntax, will work. Setting up the HTML with a simple <em>div</em> with a class of <em>box</em> and the CSS above yielded this result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/115/1*4pJ9A4SG2sM4mdA4JSwcnQ.png" /><figcaption><a href="https://codepen.io/Incendie/full/eEJvqK">https://codepen.io/CodingLi/full/eEJvqK</a></figcaption></figure><p>To demonstrate the manipulation of variables, the following CSS code was used:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/943/1*pYYaUxgPJZ2sp-ERwHpB2w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/942/1*_PCWc1c2djRpNGlOeI6C5w.png" /><figcaption><a href="https://codepen.io/Incendie/pen/oebMxB">https://codepen.io/CodingLi/pen/oebMxB</a></figcaption></figure><p>The resulting output was as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/357/1*-IIdPKjgG5FlqSe9MVs_8g.png" /><figcaption><a href="https://codepen.io/Incendie/full/oebMxB">https://codepen.io/CodingLi/full/oebMxB</a></figcaption></figure><p><strong><em>Discussion</em></strong></p><p>As long as the syntax was correct, then the location where the variable is called upon will be replaced <em>EXACTLY </em>as it was stored in the <em>:root </em>selector. Any mistake in the syntax will yield an invalid result and be treated as <em>null, </em>as can be seen in the <em>font-family</em> attribute where the font did not actually change to “Comic Sans” (thankfully)<em>.</em></p><p>Further testing of how the variables can be manipulated showed that:</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*I9V9RKooCG8g_nhbmW6Bjg.png" /></figure><ol><li>Calling an existing variable when declaring a new variable (cascading rule applies).</li><li>Variables can be manipulated within the <em>:root </em>selector as well as when declaring the attribute in a separate selector.</li><li>Stored hex codes cannot be manipulated by using <em>calc. </em>If colour were to be manipulated, then a number value would have to be stored and <em>rbg() </em>or <em>rgba() </em>would have to be used (see <em>box2</em> and <em>box3</em>)</li><li>Variables cannot store selector names, nor can they store an entire selector with attributes (see the last two variables in the <em>:root </em>selector).</li><li>Variables need not be declared in the <em>:root </em>selector and can be done further down the code in another selector. The downside to this would be that the variable cannot be called upon from above its location in the code (cascading rule).</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/339/1*RveVpsKWS_ubK5dDsNb_zg.png" /><figcaption>a “local” variable</figcaption></figure><p><strong><em>Conclusion</em></strong></p><p>Storing variables can be very useful when coding a design because it allows for flexibility, in case of any changes in the design and even allows for an easier debugging process since you would only need to make any changes in one location, usually at the top of the code, and not throughout the code.</p><p>With future updates to browsers and HTML/CSS I would hope that the compatibility and functionality of CSS Variables and other functions take further steps into removing the need for preprocessors in order to allow for a more streamlined workflow.</p><p><strong><em>Further Reading</em></strong></p><ul><li><a href="https://css-tricks.com/difference-between-types-of-css-variables/">What Is The Difference Between CSS Variables And Preprocessor Variables? | CSS-Tricks</a></li><li><a href="https://philipwalton.com/articles/why-im-excited-about-native-css-variables/">Why I&#39;m Excited About Native CSS Variables</a></li><li><a href="https://developers.google.com/web/updates/2016/02/css-variables-why-should-you-care">CSS Variables: Why Should You Care? | Web | Google Developers</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7acc5b04dfbe" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>