I know how to get and set single custom properties (CSS variables). But i have no idea how i can get a list of all custom-properties (key: value) ?
Example for get my custom properties (if i know the names)
const bodyStyles = window.getComputedStyle(document.body);
const colorText = bodyStyles.getPropertyValue('--color-text');
const colorBg = bodyStyles.getPropertyValue('--color-background');
console.log('color text is =>', colorText);
console.log('color background is =>', colorBg);
:root {
--color-text: red;
--color-background: #ffedd0;
}
body {
font-family: Arial, Helvetica, sans-serif;
color: var(--color-text);
background: var(--color-background);
}
<p>how to get all custom properties (key: value) per javascript ?</p>
My question is how can i get a list of all properties if i don't know their names ?
In my case i want an array like this:
allBodyProperties = [
{ "--color-text": "red" },
{ "--color-background": "#ffedd0" }
];
Is there something like getPropertyEntries()
?
EDIT
This is no duplicate because the linked answer is technically outdated and not runnable on todays browsers -> getMatchedCSSRules