내가 보려고 만든 개발 공부 일지

JS 에서 css 변수값 접근 , 수정 본문

CSS

JS 에서 css 변수값 접근 , 수정

kwangsunny 2021. 7. 2. 00:54
:root {
  --myColor : #ffffff;
  --myLength : 100px;
}

만약 위처럼 css 속성값을 전역으로 선언해놨을 때

javascript 에서 저 값에 접근하려면 아래처럼 

var cs = window.getComputedStyle(엘리먼트);
cs.getPropertyValue('--myColor');
document.documentElement.style.setProperty('--myColor', '#afafaf');

(document.documentElement == <html>)

이런 식으로 값을 가져오거나 수정해줄 있다.

Comments