css custom variable without type code example
Example 1: javascript use css custom properties
Values in JavaScript
To use the values of custom properties in JavaScript, it is just like standard properties.
element.style.getPropertyValue("--my-var");
getComputedStyle(element).getPropertyValue("--my-var");
element.style.setProperty("--my-var", jsVar + 4);
Example 2: css variables
:root {
--primary-bg: #8a2be2;
--btn-font-size: 18px;
--btn-padding: 10px 15px;
}
.btn-primary {
background-color: var(--primary);
font-size: var(--btn-font-size);
padding: var(--btn-padding);
color: #E2E2E2;
}