get/set CSS props
โญ๏ธ ๆณจๆ๏ผ
ไธ่ฆๅฐ Element.getAttribute()ใCSSStyleDeclaration.getPropertyValue() ่ Element.setAttribute()ใCSSStyleDeclaration.setProperty()ๆๆททไบโ๏ธ
// get:
elem.style.color // ไฝฟ็จ <elem style="...">
getComputedStyle(elem).color // โญ๏ธ computed style โ elem.style.colorโ๏ธ
// set:
elem.style.color = 'pink'; // ่จญๅฎๅ
งๅปบๆจฃๅผ
elem.styleProp('--css-var', '4px'); // ่จญๅฎ่ช่จ css var// get
// โญ๏ธ ๆณจๆ๏ผ`elem.style` ๅชๅญๅไพ่ช <... style="..."> ็ๆจฃๅผ, ้ๅธธ็บ ''โ๏ธ
elem.style.backgroundColor
elem.style.getPropertyValue('margin-top') // โญ๏ธ = elem.style.marginTop
// โญ๏ธ computed style (read-onlyโ๏ธ)
// ๆญค่็บใๅฅ็จๆๆๆจฃๅผใๅพๆๅพๅฐ็็ตๆ๏ผๆไปฅไธๅฎๆๅผใ
getComputedStyle(elem).color
// set
elem.style.display = "none"
style.setProperty('margin-top', '4px') // โญ๏ธ style.marginTop = '4px'Element โฉ .styleProp()
MDN โฉ
CSSStyleDeclaration โฉ .setProperty(), .getPropertyValue()
Window โฉ .getComputedStyle()
W3School โฉ CSS Default Values Reference
JS.info โฉ Styles and classes
W3C โฉ Full Property List
Element โฉ .styleProp()
CSS vs. DOM style prop names
'background-color'
elem.style.backgroundColor
'z-index'
elem.style.zIndex
'border-left-width'
elem.style.borderLeftWidth
code example
The style property operates only on the value of the "style" attribute, without any CSS cascade. ๐๐ป See "elem.style" tab below.
const {log} = console;
const p1 = $('p:nth-of-type(1)');
const p2 = $('p:nth-of-type(2)');
// css properties (kebab case)
const cssProps = [
'background', 'color', 'background-color', 'border',
'border-radius', 'padding', 'padding-left',
];
// DOM style properties (camel case, e.g. 'paddingLeft')
const styleProps = cssProps.map(prop => prop.kebabToCamelCase());
/*
โญ๏ธ The `style` property operates only on the value of
the "style" attribute, WITHOUT any CSS cascadeโ๏ธ
*/
// โญโโelem.styleโโโฎ โญโโโโโโโโโโโโโ style.getPropertyValue() โโโโโโโโโโโโโฎ
// css prop | p1 p2 | p1 | p2
// -----------------------------------------------------------------------------------
// background | yellow '' | rgb(255, 255, 0) none ... | rgba(0, 0, 0, 0) none ...
// color | red '' | rgb(255, 0, 0) | rgb(0, 0, 0)
// bg-color | yellow โญ๏ธ '' | rgb(255, 255, 0) | rgba(0, 0, 0, 0)
// border | '' '' | 1px solid rgb(0, 0, 0) | same
// border-rad | '' '' | 4px | same
// padding | '' '' | 0px 16px | same
// padding-l | '' '' | 16px | same
// show elem.style
function showStyles2(elem){
styleProps // color, border, ...
.map(prop => elem.style[prop]) // style.color, style.border, ...
.forEach(x => log(x));
}
// get computed styles
function showStyles3(elem){
cssProps // border-radius, padding-left
.map(prop => elem.styleProp(prop)) // getPropertyValue('border-radius'), ...
.forEach(x => log(x));
}
showStyles2(p1);
showStyles2(p2);
showStyles3(p1);
showStyles3(p2);
// before set
log(p1.styleProp('padding-left')); // 16px
log(p1.styleProp('paddingTop')); // 0px
log(p1.style.paddingLeft); // '' โญ๏ธ
log(p1.style.paddingTop); // '' โญ๏ธ
// set
p1.style.paddingLeft = '100px';
p1.styleProp('padding-top', '30px');
// after set
log(p1.styleProp('padding-left')); // 100px
log(p1.styleProp('paddingTop')); // 30px
log(p1.style.paddingLeft); // 100px
log(p1.style.paddingTop); // 30pxbody {
background: #aaa;
}
p {
border: 1px solid black;
border-radius: 4px;
padding: 0 1em;
}<p style="background: yellow; color: red;">this is a paragraph</p>
<p>this is another paragraph.</p>String โฉ .capitalize(), .kebabToCamelCase()
Element โฉ .styleProp()
codepen โฉ elem.style
replit โฉ style.setProperty() - set border, backgroundColor, color randomly.
Last updated
Was this helpful?