{"id":2290,"date":"2017-03-08T04:24:40","date_gmt":"2017-03-08T04:24:40","guid":{"rendered":"http:\/\/cssreset.com\/?p=2290"},"modified":"2017-03-08T04:24:40","modified_gmt":"2017-03-08T04:24:40","slug":"how-to-use-css-variables-and-custom-properties","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/how-to-use-css-variables-and-custom-properties\/","title":{"rendered":"How to Use CSS Variables and Custom Properties"},"content":{"rendered":"<p>CSS <a href=\"https:\/\/developers.google.com\/web\/updates\/2016\/02\/css-variables-why-should-you-care\" target=\"_blank\">custom properties<\/a> and variables are slowly gaining popularity now that they&#8217;re beginning to be supported on more major browsers. Custom properties and variables are a great new way to make the CSS language more flexible and dynamic, and best of all, they&#8217;re simple to use and work with, and if you&#8217;re a practiced CSS developer, you should have no problem adjusting to using them (it also might help if you have a tiny bit of JavaScript or jQuery knowledge &#8212; though this isn&#8217;t necessary at all!).<\/p>\n<p>To start using CSS variables, you&#8217;ll need to first create some custom properties. Custom properties are author chosen properties that can be named anything and assigned to any HTML element to use later as a variable. Custom properties always begin with two dashes, like this: &#8211;header-color, or &#8211;text-color. You can use any name you want, and you can assign them to any property you want, though your desired effect won&#8217;t apply to any HTML elements unless you call the variable within another property (more on that later). Check out the example below to see how to easily create your own custom properties:<\/p>\n<pre>div{\r\n\u00a0 \u00a0--text-color: #5b63fe;\r\n}<\/pre>\n<p>So how do we use these custom properties? That&#8217;s where the variables come in. Let&#8217;s say we want to use our new custom &#8211;text-color property to apply to all the text in every p element. You would be able to do that by calling the var() function and passing your desired custom property through that function, assigning it to the property that you&#8217;d like the variable to be applied to (so in this case, we want to set the color property to #5b63fe). See below for an example of just how that would look:<\/p>\n<pre>p{\r\n\u00a0 \u00a0 color: var(--text-color);\r\n}<\/pre>\n<p>That&#8217;s it! You can now apply the &#8211;text-color variable to any property you&#8217;d like (assign it to background colors, links, etc), it doesn&#8217;t have to be applied exclusively to text just because that&#8217;s what we&#8217;ve called it (remember, you can call it whatever you want). CSS variables are actually incredibly simple and straightforward to use. Try them out on your next project!<\/p>\n<div class=\"wp-socializer wpsr-share-icons \" data-lg-action=\"show\" data-sm-action=\"show\" data-sm-width=\"768\" ><h3>Share and Enjoy !<\/h3><div class=\"wpsr-si-inner\"><div class=\"wpsr-counter wpsrc-sz-32px\" style=\"color:#000\"><span class=\"scount\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook,twitter,linkedin,pinterest,print,pdf\">0<\/span><\/span><small class=\"stext\">Shares<\/small><\/div><div class=\"socializer sr-popup sr-32px sr-circle sr-opacity sr-pad sr-count-1 sr-count-1\"><span class=\"sr-facebook\"><a rel=\"nofollow\" href=\"https:\/\/www.facebook.com\/share.php?u=\" target=\"_blank\"  title=\"Share this on Facebook\"  style=\"color: #ffffff\" ><i class=\"fab fa-facebook-f\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-twitter\"><a rel=\"nofollow\" href=\"https:\/\/twitter.com\/intent\/tweet?text=%20-%20%20\" target=\"_blank\"  title=\"Tweet this !\"  style=\"color: #ffffff\" ><i class=\"fab fa-twitter\"><\/i><\/a><\/span>\n<span class=\"sr-linkedin\"><a rel=\"nofollow\" href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=\" target=\"_blank\"  title=\"Add this to LinkedIn\"  style=\"color: #ffffff\" ><i class=\"fab fa-linkedin-in\"><\/i><\/a><\/span>\n<span class=\"sr-pinterest\"><a rel=\"nofollow\" href=\"https:\/\/www.pinterest.com\/pin\/create\/button\/?url=&amp;media=&amp;description=\" target=\"_blank\"  title=\"Submit this to Pinterest\"  style=\"color: #ffffff\" data-pin-custom=\"true\"><i class=\"fab fa-pinterest\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"pinterest\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-print\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Print this article \"  style=\"color: #ffffff\" ><i class=\"fa fa-print\"><\/i><\/a><\/span>\n<span class=\"sr-pdf\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Convert to PDF\"  style=\"color: #ffffff\" ><i class=\"fa fa-file-pdf\"><\/i><\/a><\/span><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>CSS custom properties and variables are slowly gaining popularity now that they&#8217;re beginning to be supported on more major browsers. Custom properties and variables are a great new way to make the CSS language more flexible and dynamic, and best [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/how-to-use-css-variables-and-custom-properties\/\" title=\"Click to read 'How to Use CSS Variables and Custom Properties'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[41,6,42],"tags":[],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/2290"}],"collection":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/comments?post=2290"}],"version-history":[{"count":1,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/2290\/revisions"}],"predecessor-version":[{"id":2291,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/2290\/revisions\/2291"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=2290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=2290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=2290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}