{"id":4255,"date":"2018-04-06T04:06:35","date_gmt":"2018-04-06T04:06:35","guid":{"rendered":"http:\/\/b.parsons.edu\/~dejongo\/?p=4255"},"modified":"2025-10-14T20:52:20","modified_gmt":"2025-10-14T20:52:20","slug":"10-css-variables","status":"publish","type":"post","link":"https:\/\/b.parsons.edu\/~dejongo\/10-css-variables\/","title":{"rendered":"10 CSS Variables"},"content":{"rendered":"<p>CSS variables are entities defined by CSS authors that contain specific values to be reused throughout a document. Variables or <a href=\"https:\/\/www.w3.org\/TR\/css-variables\/\">CSS Custom Properties for Cascading Variables<\/a> have a precursor of sorts in curentcolor. Currentcolor sets the current color as a variable with that name. It was originally introduced by Opera. Various CSS preprocessors are popular because they implemented variables.\n<\/p>\n<p>Variables save time and help in the organization of complex documents. It is part of the computer programer&#8217;s ethic known as DRY or don\u2019t repeat yourself. Create a variable for colors used in developing the website. To change a color only the definition has to be updated. CSS variables work like any other CSS custom property, like normal CSS properties. CSS variables can be used almost everywhere there are values, including in @media queries.<br \/>\nThe variable is defined by <code>--<\/code> dash dash, like an empty prefix. Think of it as -webkit- without webkit.  The variable is case sensitive and defined as a <strong> :root pseudo-class <\/strong>. For example: <code> :root { --main-color: orange;}<\/code>.\n<\/p>\n<p>The variable is then applied as a value: <code>  background-color: var(--main-color); <\/code><\/p>\n<div class=\"handson_demo\">\n<h3>CSS Code View<\/h3>\n<pre>\r\n<style contenteditable=\"true\">\r\n:root { --main-color: orange; --text-color: black;}\r\n\/*Change the color in the definitions *\/\r\np {\r\ncolor: var(--text-color);\r\n}\r\n#variable div {\r\nwidth: 80%; \r\npadding: 2px; \r\nmargin: 10px auto; \r\nborder: 8px solid var(--main-color);\r\n}<\/style><\/pre>\n<\/div>\n<div id=\"variable\">\n<div>\n<div>\n<div>\n<div>\n<div>\n<div>\n<div>\n<div>\n<div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p><!--\nThere are a \n#1 \nNO, like CSS values. Another name for the specification is custom properties. \nAll variables inherit. \n* {--outline: initial;}\n--foo: initial.\nIf used with universal selector\n--corners\n#2 CSS Variables are inherited properties (but you can change that)\n\nempty value is invalid, --foo:;\nbut  --foo: ; is valid. value is an empty space.\nUnlike any other CSS property, Variables are case sensitive: \nfoo \u2260 FOO\n\nFallbacks use second argument of Var function, (#1,#2), \nif the var has not been determined. \nif variables are not supported, then CSS fallback. \nif variables are supported, then second argument. \nif variable determined then determined variable. \nIf variable is not logical, then initial value (transparent)\n\nThese arguments can be daisy-chained. \nvar(--color1, var(--color2, var(--color3, red)))\nRefined Use\n\ufffc\n\nAdd calc. If want to use only numbers, specify second variable. , convert 6 to ems using calc. \nCannot divine by length. Can convert number to unit, cannot convert unit to number. \n\nThat SASS is dumb replacement. Where as CSS is dynamic variables. They are live with real consequences. Not dumb. \nTakeaway #4, \nuse variables for pure data, not CSS values.\n\nCSS variables in animations.\n@keyframes\ncan not animate variables but can use their values in animations, without the 50% limit.\nTransitions\nInteresting uses beyond replacement.\n\ufffc\ufffc\n\ndefault default values: by using another variable within a variable\n\ufffc\n\nresponsive design made easier.\n\n\ufffc\n\nSetting up shortcuts\n\n\ufffc\n\ufffc\n\njavascript\ncomputed style\n\ufffc\n--><\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"y57KbxpAQ7\"><p><a href=\"https:\/\/css-tricks.com\/difference-between-types-of-css-variables\/\">What is the difference between CSS variables and preprocessor variables?<\/a><\/p><\/blockquote>\n<p><iframe class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;What is the difference between CSS variables and preprocessor variables?&#8221; &#8212; CSS-Tricks\" src=\"https:\/\/css-tricks.com\/difference-between-types-of-css-variables\/embed\/#?secret=4uFFCl4eUa#?secret=y57KbxpAQ7\" data-secret=\"y57KbxpAQ7\" width=\"584\" height=\"329\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>CSS variables are entities defined by CSS authors that contain specific values to be reused throughout a document. Variables or CSS Custom Properties for Cascading Variables have a precursor of sorts in curentcolor. Currentcolor sets the current color as a &hellip; <a href=\"https:\/\/b.parsons.edu\/~dejongo\/10-css-variables\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[15],"tags":[],"_links":{"self":[{"href":"https:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/posts\/4255"}],"collection":[{"href":"https:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/comments?post=4255"}],"version-history":[{"count":11,"href":"https:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/posts\/4255\/revisions"}],"predecessor-version":[{"id":6544,"href":"https:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/posts\/4255\/revisions\/6544"}],"wp:attachment":[{"href":"https:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/media?parent=4255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/categories?post=4255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/tags?post=4255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}