{"id":15935,"date":"2017-07-25T06:30:57","date_gmt":"2017-07-25T12:30:57","guid":{"rendered":"http:\/\/vanseodesign.com\/?p=15935"},"modified":"2023-05-24T15:10:06","modified_gmt":"2023-05-24T21:10:06","slug":"custom-properties-and-javascript","status":"publish","type":"post","link":"https:\/\/vanseodesign.com\/css\/custom-properties-and-javascript\/","title":{"rendered":"CSS Custom Properties&mdash;Dynamic Changes With And Without Javascript"},"content":{"rendered":"<p>Have you ever wished you could change the value of a Sass or Less variable after your page has loaded? I have. Unfortunately, it\u2019s not something you can do as the variables cease to be variables once the preprocessed code is compiled into CSS. You don\u2019t have the same limitation with CSS custom properties.<\/p>\n<p><!--more--><\/p>\n<p>I\u2019ve been talking about custom properties for a few weeks now. I began by showing you <a href=\"http:\/\/vanseodesign.com\/css\/custom-properties\/\">how to define and use them<\/a> and then offered some simple <a href=\"http:\/\/vanseodesign.com\/css\/custom-properties-and-readability\/\">examples to make your code more readable and to help with contextual styling<\/a>. Last week I showed you how you can <a href=\"http:\/\/vanseodesign.com\/css\/custom-properties-media-queries\/\">work with custom properties inside @media queries<\/a>.<\/p>\n<p>At the start of this series I mentioned that one of the major advantages of custom properties is their dynamic nature. Where preprocessor variables are turned into fixed values on compile, custom properties can change their values after a page has loaded.<\/p>\n<p>I touched on this dynamic ability last week when talking about @media queries. Today I want to continue and look at how we can make changes through user interaction and Javascript.<\/p>\n<h2 id=\"changingcsscustompropertieswithoutjavascript\">Changing CSS Custom Properties Without Javascript<\/h2>\n<p>While most of the dynamic magic will require a little bit of Javascript, let\u2019s start with a simple example and change the value of a custom property on <a href=\"http:\/\/vanseodesign.com\/css\/combinators-pseudo-classes\/\">:hover<\/a>. Know that this will work on any <a href=\"http:\/\/vanseodesign.com\/css\/pseudo-elements-classes\/\">pseudo class<\/a> and not only :hover<\/p>\n<p>Here\u2019s how we might change the background color of divs using custom properties. I added some CSS to define a width and height and defined a custom property &#8211;background with a value of #ccc. I then set the background-color property using the var() function and gave it a transition of 1s just because.<\/p>\n<p>[code type=css]<br \/>\ndiv {<br \/>\n width: 15em;<br \/>\n height: 15em;<\/p>\n<p>  &#8211;background: #ccc;<br \/>\n  background-color: var(&#8211;background);<br \/>\n  transition: background-color 1s;<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>To change the background color I <a href=\"https:\/\/codepen.io\/malyw\/pen\/ZWygyv\">redefined the custom property on the :hover pseudo class<\/a> and assigned it a new value of #cce.<\/p>\n<p>[code type=css]<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\"> div:hover {\r\n  --background: #cce;\r\n}<\/pre>\n<p>[\/code]<\/p>\n<p>Realistically we could have done this without the use of custom properties by changing the background-color property directly or even with the use of preprocessor variables.<\/p>\n<p>It\u2019s a fair point, particularly with an example as simple as this one, but imagine you\u2019re <a href=\"http:\/\/tutorialzine.com\/2016\/03\/what-you-need-to-know-about-css-variables\/\">changing more than the background color<\/a>. Maybe you want to change background color, text color, and various font properties. You still wouldn\u2019t need custom properties, but as we saw the last couple of weeks this method can result in cleaner and more readable code.<\/p>\n<h2 id=\"changingcsscustompropertieswithjavascript\">Changing CSS Custom Properties With Javascript<\/h2>\n<p>Where things get really interesting is when we use Javascript to change the values of custom properties.<\/p>\n<p>There are two things we need to be able to do to use Javascript with custom properties. We need to know how to get the value of a custom property and also how to set a new value on a custom property.<\/p>\n<h3 id=\"getthevalueofacustomproperty\">Get the Value of a Custom Property<\/h3>\n<p>To read the value you use two Javascript functions, <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Window\/getComputedStyle\">window.getComputedStyle<\/a> and <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/CSSStyleDeclaration\/getPropertyValue\">getPropertyValue<\/a>. The former method will get every computed CSS property and value of every element and the latter method can be used to get the value of specific properties.<\/p>\n<p>[code type=javascript]<br \/>\nvar style = window.getComputedStyle(element[, pseudoElt]);<br \/>\nvar value = style.getPropertyValue(property);<br \/>\n[\/code]<\/p>\n<p>If you work with Javascript, this is probably nothing new. However, if you aren\u2019t familiar with Javascript, you might like an example. Here\u2019s a little CSS where I defined a &#8211;color custom property inside :root and assigned it a value of red.<\/p>\n<p>[code type=css]<br \/>\n:root {<br \/>\n &#8211;color: red<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>We\u2019ll use the getComputedStyle() and getPropertyValue() methods to read the value of &#8211;color. The first line of Javascript below gets all the styles of <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Document\/documentElement\">document.documentElement<\/a> (the way to reference :root) and assigns the result to the Javascript variable, styles.<\/p>\n<p>The second line gets the specific value for the &#8211;color custom property defined in :root and assigns that value to a new variable colorValue. In this case &#8211;color is the only property, but in practice there would likely be others.<\/p>\n<p>[code type=javascript]<br \/>\nvar styles = getComputedStyle(document.documentElement);<br \/>\nvar colorValue = styles.getPropertyValue(&#8216;&#8211;color&#8217;);<br \/>\n[\/code]<\/p>\n<p>The variable colorValue should now hold a value of red, which is the value I initially set on &#8211;color in the CSS. To proves this to yourself you can use the alert() function to show the value on your screen.<\/p>\n<p>[code type=javascript]<br \/>\nalert(colorValue);<br \/>\n[\/code]<\/p>\n<h3 id=\"setthevalueofacustomproperty\">Set the Value of a Custom Property<\/h3>\n<p>To set a new value on a custom property you use the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/CSSStyleDeclaration\/setProperty\">style.setProperty method<\/a>.<\/p>\n<p>[code type=javascript]<br \/>\nstyle.setProperty(propertyName, value, priority);<br \/>\n[\/code]<\/p>\n<p>Note that setProperty can take three parameters. The first two are necessary as they identify the property to change and the new value it will be assigned. The third parameter, priority, is optional and allows you to set !important, though as a general rule you don\u2019t want to do this.<\/p>\n<p>Technically the value parameter is also optional and if you leave it out, it will be treated as an empty string. Most of the time you\u2019ll want to set a value, but leaving it out is a way to change the custom property to an empty string.<\/p>\n<p>It\u2019s easier to set a new value than to read the existing one as we don\u2019t need to hold anything temporarily in a variable. Again document.documentElement is the :root so this line of code changes the value of the :root custom property &#8211;color to green.<\/p>\n<p>[code type=javascript]<br \/>\ndocument.documentElement.style.setProperty(&#8216;&#8211;color&#8217;, &#8216;green&#8217;);<br \/>\n[\/code]<\/p>\n<p>In fact, the property doesn\u2019t even need to exist before setting a new value. You could set the custom property initially inside a setProperty method, if you\u2019d like.<\/p>\n<h2 id=\"removethevalueofacustomproperty\">Remove the Value of a Custom Property<\/h2>\n<p>There\u2019s also a removeProperty() function to remove the entire property and its value. If for some reason you want to remove the &#8211;color custom property, you could do this.<\/p>\n<p>[code type=javascript]<br \/>\ndocument.documentElement.style.removeProperty(&#8216;&#8211;color&#8217;);<br \/>\n[\/code]<\/p>\n<p>Together getPropertyValue, setProperty, and removeProperty offer a lot of options for changing custom properties on the fly.<\/p>\n<h2 id=\"changingcolorvalueswithacolorpicker\">Changing Color Values with a Color Picker<\/h2>\n<p>Now that we\u2019ve seen how to get and set the values of custom properties, as well as how to remove the property entirely, let\u2019s try an example showing how you might use this information in practice. Again, I\u2019ll keep the example as simple as possible for the sake of explanation.<\/p>\n<p>Here I created an empty div and an input with a type of color so users can access their default color picker. We want to let users select a color and then have their selection become the background color for the div.<\/p>\n<p>[code type=html]<\/p>\n<div><\/div>\n<p><input type=\"color\" id=\"div-bkgd\" value=\"#cccccc\"><br \/>\n[\/code]<\/p>\n<p>The CSS is simple as well. I created a custom property (&#8211;background) inside the :root and assigned it a value of #ccc. I gave the div a width and height and using the var() function set its background color to the value of the &#8211;background custom property. I also centered the div using auto left and right margins.<\/p>\n<p>I then set the input to display as a <a href=\"http:\/\/vanseodesign.com\/css\/display-property\/\">block level element<\/a>, gave it a width, and also used <a href=\"http:\/\/vanseodesign.com\/css\/centering-with-css\/\">auto margins to center it<\/a>. None of this is necessary, but I think it makes for a more presentable example.<\/p>\n<p>The important part for this example is the custom property.<\/p>\n<p>[code type=css]<br \/>\n:root {<br \/>\n &#8211;background: #ccc;<br \/>\n}<\/p>\n<p>div {<br \/>\n width: 20em;<br \/>\n height: 20em;<br \/>\n margin: 1em auto;<br \/>\n background: var(&#8211;background);<br \/>\n}<\/p>\n<p>input {<br \/>\n display: block;<br \/>\n width: 10em;<br \/>\n margin: 1em auto;<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>Now for the Javascript. First we need to grab a hook to the color input. I used the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Document\/querySelector\">document.querySelector method<\/a>, which matches the specified selector, in this case the element with an id of div-bkgd, which is the input element. I assigned the value to a variable called colorInput.<\/p>\n<p>Next I added an event listener to colorInput, which will listen for any change to the color input and then run a function. The function uses setProperty to redefine the &#8211;background custom property with the new color value.<\/p>\n<p>[code type=javascript]<br \/>\nvar colorInput = document.querySelector(&#8220;#div-bkgd&#8221;);<\/p>\n<p>colorInput.addEventListener(&#8220;change&#8221;, function() {<br \/>\n document.documentElement.style.setProperty(&#8220;&#8211;background&#8221;, this.value);<br \/>\n});<br \/>\n[\/code]<\/p>\n<p>Here\u2019s a demo I set up so you can see this in action. Select a color from the color picker and the background color of the div should change to your selection.<\/p>\n<p><strong>Note<\/strong>: The <a href=\"http:\/\/caniuse.com\/#feat=input-color\">color input doesn\u2019t yet have universal support<\/a>. It doesn\u2019t work in IE or any version of Safari, though the latter should let you type in a hex color as opposed to letting you choose one from a color picker.<\/p>\n<p>I also added another type of input (the range input) to the demo to allow you to control the font-size of some text I added inside the div. The code is very similar to how I changed the background color here. The main difference (aside from the different type of input) is that you have to append the units to the font-size.<\/p>\n<p>I also made a few tweaks in the demo to set dimensions in terms of rem units instead of em units so that the divs don\u2019t change size as you adjust the font-size.<\/p>\n<h2 id=\"additionalexamples\">Additional Examples<\/h2>\n<p>Because I kept the examples on the simpler side, I want to point you to a few that are a little more complex so you can see more of the power of custom properties when combined with Javascript.<\/p>\n<p>Here are three examples that use sliders and color pickers to alter the values of custom properties. They all use similar techniques to the demo I created, though each does so in greater detail.<\/p>\n<ul>\n<li><a href=\"https:\/\/googlechrome.github.io\/samples\/css-custom-properties\/\">CSS Custom Properties (CSS Variables) Sample<\/a><\/li>\n<li><a href=\"http:\/\/codepen.io\/wesbos\/pen\/adQjoY\">Update CSS Variables with JS<\/a><\/li>\n<li><a href=\"https:\/\/codepen.io\/keithclark\/pen\/qbdVJr\">Using CSS custom properties for theme previews<\/a><\/li>\n<\/ul>\n<p>You\u2019ll notice the last link mentions theme previews. Theming crops up often in articles and examples featuring CSS custom properties. Here are a couple of demos showing what you can do.<\/p>\n<ul>\n<li><a href=\"http:\/\/alembic-v2.surge.sh\/styles\/\">Theme styles<\/a><\/li>\n<li><a href=\"http:\/\/danielfurze.co.uk\/blog\/theme-switcher-with-css-custom-properties\/\">Theme switcher with CSS custom properties<\/a><\/li>\n<\/ul>\n<p>And here are some articles walking you through the process of theming a site.<\/p>\n<ul>\n<li><a href=\"https:\/\/www.fdp.io\/blog\/2016\/11\/08\/theming-via-css-properties\/\">Theming Via CSS Properties<\/a><\/li>\n<li><a href=\"https:\/\/csswizardry.com\/2016\/10\/pragmatic-practical-progressive-theming-with-custom-properties\/\">Pragmatic, Practical, and Progressive Theming with Custom Properties<\/a><\/li>\n<li><a href=\"https:\/\/publishing-project.rivendellweb.net\/theming-the-web-with-css-custom-properties\/\">Theming the web with CSS Custom Properties<\/a><\/li>\n<li><a href=\"https:\/\/justmarkup.com\/log\/2016\/02\/theme-switcher-using-css-custom-properties\/\">Theme switcher using CSS custom properties<\/a><\/li>\n<li><a href=\"https:\/\/blog.mariano.io\/stateful-theming-css-custom-properties-3b30b40669d9\">Stateful Theming with CSS Custom Properties<\/a><\/li>\n<li><a href=\"https:\/\/medium.com\/geckoboard-under-the-hood\/how-we-made-our-product-more-personalized-with-css-variables-and-react-b29298fde608\">How we made our product more personalized with CSS Variables and\u00a0React<\/a><\/li>\n<\/ul>\n<h2 id=\"closingthoughts\">Closing Thoughts<\/h2>\n<p>Combining CSS custom properties with Javascript or another scripting language really shows off what custom properties can do. And if Javascript isn\u2019t your thing, know that there isn\u2019t a lot you have to learn in order to get, set, and remove values and properties. You can even copy the code here and update it with the name of your particular custom property.<\/p>\n<p>If you prefer to stay away from Javascript entirely, you can still update CSS variables through the usual pseudo selectors like :hover, :visited, :first-child, :nth-child, etc.), though I\u2019d suggest it\u2019s worth taking the time to learn the little bit of Javascript, since you can do so much more with it.<\/p>\n<p>Next week I want to wrap up this series showing you how to combine custom properties with transforms, transitions, and keyframe animation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever wished you could change the value of a Sass or Less variable after your page has loaded? I have. Unfortunately, it\u2019s not something you can do as the variables cease to be variables once the preprocessed code is compiled into CSS. You don\u2019t have the same limitation with CSS custom properties.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[398,233,184],"series":[],"class_list":["post-15935","post","type-post","status-publish","format-standard","hentry","category-css","tag-custom-properties","tag-javascript","tag-variables"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>CSS Custom Properties\u2014Dynamic Changes With Javascript<\/title>\n<meta name=\"description\" content=\"One of the main advantages of CSS custom properties is the ability to change them in response to Javascript or user interaction\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/vanseodesign.com\/css\/custom-properties-and-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"CSS Custom Properties\u2014Dynamic Changes With Javascript\" \/>\n<meta property=\"og:description\" content=\"One of the main advantages of CSS custom properties is the ability to change them in response to Javascript or user interaction\" \/>\n<meta property=\"og:url\" content=\"https:\/\/vanseodesign.com\/css\/custom-properties-and-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Vanseo Design\" \/>\n<meta property=\"article:published_time\" content=\"2017-07-25T12:30:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-05-24T21:10:06+00:00\" \/>\n<meta name=\"author\" content=\"Steven Bradley\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Steven Bradley\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/vanseodesign.com\\\/css\\\/custom-properties-and-javascript\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/vanseodesign.com\\\/css\\\/custom-properties-and-javascript\\\/\"},\"author\":{\"name\":\"Steven Bradley\",\"@id\":\"https:\\\/\\\/vanseodesign.com\\\/#\\\/schema\\\/person\\\/470cda6ba835fb52596b2e26bcbd8fe4\"},\"headline\":\"CSS Custom Properties&mdash;Dynamic Changes With And Without Javascript\",\"datePublished\":\"2017-07-25T12:30:57+00:00\",\"dateModified\":\"2023-05-24T21:10:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/vanseodesign.com\\\/css\\\/custom-properties-and-javascript\\\/\"},\"wordCount\":1880,\"commentCount\":5,\"keywords\":[\"custom properties\",\"javascript\",\"variables\"],\"articleSection\":[\"CSS\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/vanseodesign.com\\\/css\\\/custom-properties-and-javascript\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/vanseodesign.com\\\/css\\\/custom-properties-and-javascript\\\/\",\"url\":\"https:\\\/\\\/vanseodesign.com\\\/css\\\/custom-properties-and-javascript\\\/\",\"name\":\"CSS Custom Properties\u2014Dynamic Changes With Javascript\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/vanseodesign.com\\\/#website\"},\"datePublished\":\"2017-07-25T12:30:57+00:00\",\"dateModified\":\"2023-05-24T21:10:06+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/vanseodesign.com\\\/#\\\/schema\\\/person\\\/470cda6ba835fb52596b2e26bcbd8fe4\"},\"description\":\"One of the main advantages of CSS custom properties is the ability to change them in response to Javascript or user interaction\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/vanseodesign.com\\\/css\\\/custom-properties-and-javascript\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/vanseodesign.com\\\/css\\\/custom-properties-and-javascript\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/vanseodesign.com\\\/css\\\/custom-properties-and-javascript\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/vanseodesign.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"CSS\",\"item\":\"https:\\\/\\\/vanseodesign.com\\\/css\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"CSS Custom Properties&mdash;Dynamic Changes With And Without Javascript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/vanseodesign.com\\\/#website\",\"url\":\"https:\\\/\\\/vanseodesign.com\\\/\",\"name\":\"Vanseo Design\",\"description\":\"Helping you build search engine friendly websites\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/vanseodesign.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/vanseodesign.com\\\/#\\\/schema\\\/person\\\/470cda6ba835fb52596b2e26bcbd8fe4\",\"name\":\"Steven Bradley\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"\\\/\\\/www.gravatar.com\\\/avatar\\\/0f47bf400c3c1bafe06ee2884405baaa?s=96&#038;r=pg&#038;d=https%3A%2F%2Fvanseodesign.com%2Fblog%2Fwp-content%2Fthemes%2Fvanseo-design%2Fimages%2Fgravatar.png\",\"url\":\"\\\/\\\/www.gravatar.com\\\/avatar\\\/0f47bf400c3c1bafe06ee2884405baaa?s=96&#038;r=pg&#038;d=https%3A%2F%2Fvanseodesign.com%2Fblog%2Fwp-content%2Fthemes%2Fvanseo-design%2Fimages%2Fgravatar.png\",\"contentUrl\":\"\\\/\\\/www.gravatar.com\\\/avatar\\\/0f47bf400c3c1bafe06ee2884405baaa?s=96&#038;r=pg&#038;d=https%3A%2F%2Fvanseodesign.com%2Fblog%2Fwp-content%2Fthemes%2Fvanseo-design%2Fimages%2Fgravatar.png\",\"caption\":\"Steven Bradley\"},\"sameAs\":[\"https:\\\/\\\/www.vanseodesign.com\\\/about\\\/\"],\"url\":\"https:\\\/\\\/vanseodesign.com\\\/author\\\/vangogh\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"CSS Custom Properties\u2014Dynamic Changes With Javascript","description":"One of the main advantages of CSS custom properties is the ability to change them in response to Javascript or user interaction","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/vanseodesign.com\/css\/custom-properties-and-javascript\/","og_locale":"en_US","og_type":"article","og_title":"CSS Custom Properties\u2014Dynamic Changes With Javascript","og_description":"One of the main advantages of CSS custom properties is the ability to change them in response to Javascript or user interaction","og_url":"https:\/\/vanseodesign.com\/css\/custom-properties-and-javascript\/","og_site_name":"Vanseo Design","article_published_time":"2017-07-25T12:30:57+00:00","article_modified_time":"2023-05-24T21:10:06+00:00","author":"Steven Bradley","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Steven Bradley","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/vanseodesign.com\/css\/custom-properties-and-javascript\/#article","isPartOf":{"@id":"https:\/\/vanseodesign.com\/css\/custom-properties-and-javascript\/"},"author":{"name":"Steven Bradley","@id":"https:\/\/vanseodesign.com\/#\/schema\/person\/470cda6ba835fb52596b2e26bcbd8fe4"},"headline":"CSS Custom Properties&mdash;Dynamic Changes With And Without Javascript","datePublished":"2017-07-25T12:30:57+00:00","dateModified":"2023-05-24T21:10:06+00:00","mainEntityOfPage":{"@id":"https:\/\/vanseodesign.com\/css\/custom-properties-and-javascript\/"},"wordCount":1880,"commentCount":5,"keywords":["custom properties","javascript","variables"],"articleSection":["CSS"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/vanseodesign.com\/css\/custom-properties-and-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/vanseodesign.com\/css\/custom-properties-and-javascript\/","url":"https:\/\/vanseodesign.com\/css\/custom-properties-and-javascript\/","name":"CSS Custom Properties\u2014Dynamic Changes With Javascript","isPartOf":{"@id":"https:\/\/vanseodesign.com\/#website"},"datePublished":"2017-07-25T12:30:57+00:00","dateModified":"2023-05-24T21:10:06+00:00","author":{"@id":"https:\/\/vanseodesign.com\/#\/schema\/person\/470cda6ba835fb52596b2e26bcbd8fe4"},"description":"One of the main advantages of CSS custom properties is the ability to change them in response to Javascript or user interaction","breadcrumb":{"@id":"https:\/\/vanseodesign.com\/css\/custom-properties-and-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/vanseodesign.com\/css\/custom-properties-and-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/vanseodesign.com\/css\/custom-properties-and-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/vanseodesign.com\/"},{"@type":"ListItem","position":2,"name":"CSS","item":"https:\/\/vanseodesign.com\/css\/"},{"@type":"ListItem","position":3,"name":"CSS Custom Properties&mdash;Dynamic Changes With And Without Javascript"}]},{"@type":"WebSite","@id":"https:\/\/vanseodesign.com\/#website","url":"https:\/\/vanseodesign.com\/","name":"Vanseo Design","description":"Helping you build search engine friendly websites","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/vanseodesign.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/vanseodesign.com\/#\/schema\/person\/470cda6ba835fb52596b2e26bcbd8fe4","name":"Steven Bradley","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"\/\/www.gravatar.com\/avatar\/0f47bf400c3c1bafe06ee2884405baaa?s=96&#038;r=pg&#038;d=https%3A%2F%2Fvanseodesign.com%2Fblog%2Fwp-content%2Fthemes%2Fvanseo-design%2Fimages%2Fgravatar.png","url":"\/\/www.gravatar.com\/avatar\/0f47bf400c3c1bafe06ee2884405baaa?s=96&#038;r=pg&#038;d=https%3A%2F%2Fvanseodesign.com%2Fblog%2Fwp-content%2Fthemes%2Fvanseo-design%2Fimages%2Fgravatar.png","contentUrl":"\/\/www.gravatar.com\/avatar\/0f47bf400c3c1bafe06ee2884405baaa?s=96&#038;r=pg&#038;d=https%3A%2F%2Fvanseodesign.com%2Fblog%2Fwp-content%2Fthemes%2Fvanseo-design%2Fimages%2Fgravatar.png","caption":"Steven Bradley"},"sameAs":["https:\/\/www.vanseodesign.com\/about\/"],"url":"https:\/\/vanseodesign.com\/author\/vangogh\/"}]}},"_links":{"self":[{"href":"https:\/\/vanseodesign.com\/wp-json\/wp\/v2\/posts\/15935","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vanseodesign.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vanseodesign.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vanseodesign.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vanseodesign.com\/wp-json\/wp\/v2\/comments?post=15935"}],"version-history":[{"count":13,"href":"https:\/\/vanseodesign.com\/wp-json\/wp\/v2\/posts\/15935\/revisions"}],"predecessor-version":[{"id":17664,"href":"https:\/\/vanseodesign.com\/wp-json\/wp\/v2\/posts\/15935\/revisions\/17664"}],"wp:attachment":[{"href":"https:\/\/vanseodesign.com\/wp-json\/wp\/v2\/media?parent=15935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vanseodesign.com\/wp-json\/wp\/v2\/categories?post=15935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vanseodesign.com\/wp-json\/wp\/v2\/tags?post=15935"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/vanseodesign.com\/wp-json\/wp\/v2\/series?post=15935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}