Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to change a css variable with javascript

var root = document.querySelector(':root');
root.style.setProperty('--variable', 'lightblue');
/or/
root.style.setProperty('--variable', myColor); 
Comment

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');
Comment

javascript get css variable

getComputedStyle(document.documentElement)
    .getPropertyValue('--my-variable-name'); // #999999
Comment

how to change css variable in javascript

document.documentElement.style.setProperty("--main-background-color", "green");
Comment

set css variable from javascript

document.documentElement.style.cssText = "--tab-count: 3";
/or/
document.documentElement.style.setProperty("--tab-count", 5);
/or/
document.documentElement.setAttribute("style", "--tab-count: 5");
Comment

how to change css variable in javascript

document.documentElement.setAttribute("style", "--main-background-color: green");
Comment

change css variable with javascript

document.documentElement.style.setProperty("--tab-count", 5);
Comment

js set css variable

document.documentElement.style.setProperty("--main-background-color", "green");
Comment

how to set css variables in javascript

		const docStyles = document.documentElement.style;

		docStyles.setProperty('--accent', '#ffffff');
		docStyles.setProperty('--textColor', '#ffffff');
Comment

set css var with javascript

// this is for site-navigation with more sub ul-blocks.
// in css root{--ul-hover-height:100px} //fallbackvalue 
// in css: nav ul ul:hover{height:--ul-hover-height;}

// to make it dynamic using JS: 
// set variable ul ul height depending on nr. of li's:
function setUlHeight(elem){        
    parentHeight=window.getComputedStyle(elem.parentNode).height
    parentHeight=parseInt(parentHeight)*1.2 //to cover li padding/margin..
    elemHeight=parentHeight * elem.childElementCount
  
    let root = document.documentElement;           
    root.style.setProperty('--ul-hover-height', elemHeight); }
Comment

set css var with javascript

// this is for site-navigation with more sub ul-blocks.
// in css root{--ul-hover-height:100px} //fallbackvalue 
// in css: nav ul ul:hover{height:--ul-hover-height;}

// to make it dynamic using JS: 
// set variable ul ul height depending on nr. of li's:
function setUlHeight(elem){        
    parentHeight=window.getComputedStyle(elem.parentNode).height
    parentHeight=parseInt(parentHeight)*1.2 //to cover li padding/margin..
    elemHeight=parentHeight * elem.childElementCount
Comment

how to change css variable in javascript

document.documentElement.style.cssText = "--main-background-color: red";
Comment

css variable value changing with javascript

const cssVar = ( name, value ) => {

    if(name.substr(0, 2) !== "--") {
        name = "--" + name;
    }

    if(value) {
        document.documentElement.style.setProperty(name, value)
    }

    return getComputedStyle(document.documentElement).getPropertyValue(name);

}
Comment

change css variable with javascript

let root = document.documentElement;

root.addEventListener("mousemove", e => {
  root.style.setProperty('--mouse-x', e.clientX + "px");
  root.style.setProperty('--mouse-y', e.clientY + "px");
});
Comment

change css variable with javascript

element.style.setProperty("--my-var", jsVar + 4);
Comment

PREVIOUS NEXT
Code Example
Javascript :: We often use anonymous functions as arguments of other functions. For example: 
Javascript :: simple game engine in javascript 
Javascript :: how to update a json file javascript 
Javascript :: jquery validator no space 
Javascript :: how to reload a module in node.js 
Javascript :: /on in jquery 
Javascript :: uppercase in word javascript 
Javascript :: jquery trigger event when scroll to div 
Javascript :: require is undefined 
Javascript :: invalid time value react datepicker 
Javascript :: react deep copy 
Javascript :: splidejs autoscroll pauseOnHover 
Javascript :: timestamps in mongoose 
Javascript :: best javascript ide 
Javascript :: generate random hex code 
Javascript :: convert json string to json object in java 
Javascript :: javascript string unique characters 
Javascript :: js get day month year 
Javascript :: lodash remove undefined values from array 
Javascript :: set time to zero in js date 
Javascript :: node get root directory 
Javascript :: angular goto top of page 
Javascript :: how to iterate through dates range in javascript 
Javascript :: how to set current date and time in jquery datetime-local 
Javascript :: react native submit on enter key 
Javascript :: get result and write to file node 
Javascript :: How to create react app with yarn, npx or npm 
Javascript :: how to get json data from json file in node js 
Javascript :: get next element of array javascript 
Javascript :: Get the Status Code of a Fetch HTTP Response 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =