https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration
์ฐธ๊ณ ๋ธ๋ก๊ทธ (ํ์ต ๋ด์ฉ)
CSSStyleDeclaration ์ฝ๊ฒ ๋งํด, ๋ ธ๋ css ์คํ์ผ!
HTMLElement ๊ฐ์ฒด์ style ํ๋กํผํฐ์ ์ํด์ ์ฐธ์กฐ๋๋ค.
์๋์ ๊ฐ์ด ์๋ฐ์คํฌ๋ฆฝํธ์์ DOM์ ์กฐ์ํ์ฌ ๋ ธ๋์ style์ ๋ฃ์ ์ ์๋ค.
const menu = document.querySelector('.menu-list');
menu.style.backgroundColor = 'skyblue';
menu.style.width = '200px';
์๋์ ๊ฐ์ ํํ๋ ๊ฐ๋ฅํ๋ค.
menu.style['backgroundColor'] = 'orange';
menu.style['width'] = '200px';
<div class="menu-container">
<ul class="menu-list">
<li>menu1</li>
<li>menu2</li>
<li>menu3</li>
</ul>
</div>
<div class="contents-container">
<p class="title">ํ
์คํธ1</p>
<p class="content">ํ
์คํธ2</p>
</div>
๋จผ์ , ulํ๊ทธ์ ์คํ์ผ์ ๋ฃ๊ธฐ.
.style์ด๋ผ๋ ํค์๋๋ฅผ ์ฌ์ฉํด css์ ์คํ์ผ ์์ฑ์ ๋ถ์ฌํ ์ ์๋ค.
๊ฐ์ธ ์ค์ต
<h1 class="title">PIXI STUDY</h1>
<script src="./cssStyleDeclaration.js" defer></script>
const title = document.querySelector('.title');
title.style.backgroundColor = 'skyblue';
title.style.width = '200px'; //js์์ ์ง์ ์ ์ธ style ์ ์ด ๋ฐฉ๋ฒ(1) .์ผ๋ก ์์ฑ ์ ๊ทผ
title.style['width'] = '200px';//js์์ ์ง์ ์ ์ธ style ์ ์ด ๋ฐฉ๋ฒ(2) ๋ฐฐ์ด๋ช
์ผ๋ก ์ ๊ทผ
title.style['font-size'] = '35px';
ํ๊ทธ์ style ์ฐ์ ๊ฒ์ด ๋ฐ๋ก
๊ฐ๋ฐ์๊ฐ ์ง์ ํด์ค ์์ฑ๋ค์ด ์์๋๋ก ๋ฐฐ์ด์ ๋ณด์ ๋ค์ด๊ฐ ์๋ค!
title.style.cssText
title.style.length
์ถ๋ ฅ 3
title.style.getPropertyValue('width')
์ถ๋ ฅ '200px'
title.style.getPropertyPriority('width') === 'important'
์ถ๋ ฅ false
title.style.item(0)
title.style.setProperty('background-color', 'pink') //๊ฐ๋ณ๊ฒฝ
๊ธ์... ์์ ์ฝ๋๋ ๊ตฌ๊ธ๋ง ํด๋ณด๋๊น ํฌ๋กฌ์์๋ ์ ๋จน์ง ์์๋ค.
title.style['fontSize'] = '20px' <- ์ด๊ฑฐ๋ ์ ๋จน์๋๋ฐ,
menu.style.removeProperty('width') //์์ฑ ์ ๊ฑฐ
์คํ์ผ ๊ด๋ จ ์ฌ๋ฌ ๋ฉ์๋ ์ฌ์ฉ์ด๋ ์คํ์ผ ์ ๋ณด๋ฅผ ํ์ธ
1. CSSStyleDeclaration.cssText : ์ ์ฉํ css๋ฅผ ํ
์คํธ๋ก ํ์ธ
2. CSSStyleDeclaration.length : ์ ์ฉํ css์ ๊ฐ์(๊ธธ์ด)๊ฐ ๋ฐํ
3. CSSStyleDeclaration.getPropertyPriority('priority') : ()์์ ๋ฃ์ ์คํ์ผ์ ์ค์๋๋ฅผ ํ์ธํ ๋ ์ฌ์ฉํ๋ค. ๋ฐํ ๊ฐ์ Boolean ํ์
4. CSSStyleDeclaration.getPropertyValue(key) : ์คํ์ผ ํค ๊ฐ์ ๋ฃ์ผ๋ฉด ํด๋นํ๋ value๊ฐ
5. CSSStyleDeclaration.item(index) : ์ ์ฉ๋ ์คํ์ผ์ index๋ฅผ ๋ฃ์ผ๋ฉด ํด๋นํ๋ key๊ฐ์ด ๋ฐํ
6. CSSStyleDeclaration.removeProperty('style name') : ํ๋ผ๋ฏธํฐ๋ก ์คํ์ผ์ ์ด๋ฆ(key)๋ฅผ ๋ฃ์ผ๋ฉด ํด๋น ์คํ์ผ์ด ์ ๊ฑฐ
7. CSSStyleDeclaration.setProperty('์ ์ฉํ ์คํ์ผ ์ด๋ฆ', '์ ์ฉํ ๊ฐ', '์ต์
( important/undefined/"" ') : ์คํ์ผ์ ์์ ํ ๋ ์ฌ์ฉ. ํ์ฌ ์ ์ฉ๋์ด ์์ง ์๋ ์คํ์ผ์ ์์ฑํ๋ฉด ์ถ๊ฐ. ์ต์
๊ฐ์ผ๋ก ์ค์๋๋ฅผ ์ค์ ํ ์ ์์ผ๋ ์๋ต ๊ฐ๋ฅ
ํด๋์ค ์ถ๊ฐ/์ ๊ฑฐ/ํ ๊ธํ๊ธฐ
ํ๊ทธ ๋ณ์์ ์ง์ ์ ์ผ๋ก ์ธ ์ ์๋ ์์ฑ๋ค
title.classList
title.className
title.classList.contains
classList ๋ฉ์๋
Element.classList.add : ํด๋์ค๋ฅผ ์ถ๊ฐํ ์ ์๋ค.
Element.classList.remove : ํด๋์ค๋ฅผ ์ ๊ฑฐํ ์ ์๋ค.
Element.classList.toggle(String [, force]) : ํด๋์ค๊ฐ ์๋ ๊ฒฝ์ฐ ์ญ์ ํ๊ณ ์๋ ๊ฒฝ์ฐ ์ถ๊ฐํ๋ค. ๋๋ฒ์งธ ์ธ์๋ฅผ ๋ฃ๋ ๊ฒฝ์ฐ ๋๋ฒ์งธ ์ธ์๊ฐ true์ผ ๋๋ง ๊ฐ์ ์ถ๊ฐํ๊ณ false๋ฉด ์ ๊ฑฐํ๋ค.
Element.classList.item(Number) : DOMTokenList์ ์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉํ์ฌ ํด๋์ค ๊ฐ์ ๋ฐํํ๋ค.
Element.classList.contains(String) : ํด๋น ํด๋์ค๊ฐ ์กด์ฌํ๋์ง ํ์ธํ๊ณ true๋๋ false๋ก ๋ฐํํ๋ค.
Element.classList.replace(old class, new class) : ํด๋์ค๋ฅผ ๋ณ๊ฒฝ(์ฌ์ค์ )ํ๋ค.
๊ฐ๋ น css ์ ์๋์ ๊ฐ์ ์คํ์ผ์ด ์๋ค๊ณ ์น์.
.title {
font-weight: bold;
}
.pink-border {
border: 2px solid rgb(255, 0, 200);
}
.small-title {
margin: 15px;
}
title.classList.remove('pink-border')
'๋งํฌ์ > Html & Css' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
16์ง์ ์ปฌ๋ฌ์ฝ๋ (0) | 2023.01.15 |
---|---|
[CSS] ๋ฐฑ๊ทธ๋ผ์ด๋ ์ด๋ฏธ์ง, margin-auto (0) | 2023.01.15 |
[HTML][Canvas] ํ๊ทธ Canvas ์์๋ณด๊ธฐ (0) | 2022.12.23 |
[Css][๋ฐ์ํ ์น] flex ์ด์ฉํ์ฌ div ๋ฐ์ค๋ฅผ ์ํ๋ ์์น๋ก ๋ด๋ฆฌ๊ธฐ (0) | 2022.12.08 |
[HTML ์ขํ] x, y, ox, oy, Height, width (0) | 2022.08.26 |