{"id":1472,"date":"2018-02-14T09:21:34","date_gmt":"2018-02-14T14:21:34","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=1472"},"modified":"2018-02-14T09:21:34","modified_gmt":"2018-02-14T14:21:34","slug":"css-define-use-variables","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/02\/14\/css-define-use-variables\/","title":{"rendered":"CSS: How to define and use variables"},"content":{"rendered":"<p>CSS has a mechanism that allows you to set up custom properties and then use them in CSS definitions as if they are variables. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">\/* CSS Variables *\/\r\n:root {\r\n  --primary: #fff;\r\n  --dark: #333;\r\n  --light: #fff;\r\n  --shadow: 0 1px 5px rgba(104, 104, 104, 0.8);\r\n}<\/pre>\n<p>When defining CSS variables, there are two syntactical things to note:<\/p>\n<ol>\n<li>You must declare the scope of the variable. In the above example it was defined as <code class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">:root<\/code>, which signifies that the variable can be used anywhere in the CSS.<\/li>\n<li>Variable names begin with double dashes and are case sensitive. For example: <code class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">--light<\/code><\/li>\n<\/ol>\n<p>To use the value of the newly defined variable in some other property, the syntax CSS requires is to use the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">var()<\/code> function, where the name of the variable is placed in the parenthesis. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">.btn {\r\n  background: var(--dark);\r\n  color: var(--light);\r\n}<\/pre>\n<p>For more information about CSS variables and use of the var( ) function, see:<br \/>\n<a href=\"https:\/\/www.w3schools.com\/css\/css3_variables.asp\">https:\/\/www.w3schools.com\/css\/css3_variables.asp<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>CSS has a mechanism that allows you to set up custom properties and then use them in CSS definitions as if they are variables. For example: \/* CSS Variables *\/ :root { &#8211;primary: #fff; &#8211;dark: #333; &#8211;light: #fff; &#8211;shadow: 0 1px 5px rgba(104, 104, 104, 0.8); } When defining CSS variables, there are two syntactical &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/02\/14\/css-define-use-variables\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">CSS: How to define and use variables<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[104],"tags":[10,105],"class_list":["post-1472","post","type-post","status-publish","format-standard","hentry","category-html-css","tag-css","tag-html"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1472","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=1472"}],"version-history":[{"count":6,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1472\/revisions"}],"predecessor-version":[{"id":1478,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1472\/revisions\/1478"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=1472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=1472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=1472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}