{"id":311697,"date":"2020-06-08T07:13:13","date_gmt":"2020-06-08T14:13:13","guid":{"rendered":"https:\/\/css-tricks.com\/?p=311697"},"modified":"2020-06-08T07:13:15","modified_gmt":"2020-06-08T14:13:15","slug":"how-to-get-all-custom-properties-on-a-page-in-javascript","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/how-to-get-all-custom-properties-on-a-page-in-javascript\/","title":{"rendered":"How to Get All Custom Properties on a Page in JavaScript"},"content":{"rendered":"\n
We can use JavaScript to get the value of a CSS custom property. Robin wrote up a detailed explanation about this in Get a CSS Custom Property Value with JavaScript<\/a>. To review, let\u2019s say we\u2019ve declared a single custom property on the HTML element:<\/p>\n\n\n\n In JavaScript, we can access the value with Perfect. Now we have access to our accent color in JavaScript. You know what\u2019s cool? If we change that color in CSS, it updates in JavaScript as well! Handy.<\/p>\n\n\n\n\n\n\n\n What happens, though, when it\u2019s not just one property we need access to in JavaScript, but a whole bunch of them?<\/p>\n\n\n\n We end up with JavaScript that looks like this:<\/p>\n\n\n\n We\u2019re repeating ourselves a lot. We could shorten each one of these lines by abstracting the common tasks to a function.<\/p>\n\n\n\n That helps reduce code repetition, but we still have a less-than-ideal situation. Every time we add a custom property in CSS, we have to write another line of JavaScript to access it. This can and does work fine if we only have a few custom properties. I\u2019ve used this setup on production projects before. But, it\u2019s also possible to automate this.<\/p>\n\n\n\n Let\u2019s walk through the process of automating it by making a working thing.<\/p>\n\n\n We\u2019ll make a color palette, which is a common feature in pattern libraries. We\u2019ll generate a grid of color swatches from our CSS custom properties. <\/p>\n\n\n\n Here\u2019s the complete demo<\/a> that we\u2019ll build step-by-step.<\/p>\n\n\n\nhtml {\n\u00a0 --color-accent: #00eb9b;\n}<\/code><\/pre>\n\n\n\n
getComputedStyle<\/code> and
getPropertyValue<\/code>:<\/p>\n\n\n\n
const colorAccent = getComputedStyle(document.documentElement)\n\u00a0 .getPropertyValue('--color-accent'); \/\/ #00eb9b<\/code><\/pre>\n\n\n\n
html {\n\u00a0 --color-accent: #00eb9b;\n\u00a0 --color-accent-secondary: #9db4ff;\n\u00a0 --color-accent-tertiary: #f2c0ea;\n\u00a0 --color-text: #292929;\n\u00a0 --color-divider: #d7d7d7;\n}<\/code><\/pre>\n\n\n\n
const colorAccent = getComputedStyle(document.documentElement).getPropertyValue('--color-accent'); \/\/ #00eb9b\nconst colorAccentSecondary = getComputedStyle(document.documentElement).getPropertyValue('--color-accent-secondary'); \/\/ #9db4ff\nconst colorAccentTertiary = getComputedStyle(document.documentElement).getPropertyValue('--color-accent-tertiary'); \/\/ #f2c0ea\nconst colorText = getComputedStyle(document.documentElement).getPropertyValue('--color-text'); \/\/ #292929\nconst colorDivider = getComputedStyle(document.documentElement).getPropertyValue('--color-text'); \/\/ #d7d7d7<\/code><\/pre>\n\n\n\n
const getCSSProp = (element, propName) => getComputedStyle(element).getPropertyValue(propName);\nconst colorAccent = getCSSProp(document.documentElement, '--color-accent'); \/\/ #00eb9b\n\/\/ repeat for each custom property...<\/code><\/pre>\n\n\n\n
What are we making?<\/h3>\n\n\n