javascript get css variable
let docStyle = getComputedStyle(document.documentElement);
//get variable
let myVarVal docStyle.getPropertyValue('--my-variable-name');
//set variable
docStyle.setProperty('--my-variable-name', '#fff');
javascript get css variable
let docStyle = getComputedStyle(document.documentElement);
//get variable
let myVarVal docStyle.getPropertyValue('--my-variable-name');
//set variable
docStyle.setProperty('--my-variable-name', '#fff');
javascript use css custom properties
Values in JavaScript
To use the values of custom properties in JavaScript, it is just like standard properties.
// get variable from inline style
element.style.getPropertyValue("--my-var");
// get variable from wherever
getComputedStyle(element).getPropertyValue("--my-var");
// set variable on inline style
element.style.setProperty("--my-var", jsVar + 4);
css variables
/* "css variables" */
/* not for ie but good for Edge and other browser :D */
/*
By declaring a custom property on the :root pseudo-class
and using it where needed throughout the document
*/
: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;
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us