The type is React.CSSProperties. You can find this in VSCode by writing <div
style={{}}> and pressing F12 when having your cursor in the style attribute.
The type is React.CSSProperties. You can find this in VSCode by writing <div
style={{}}> and pressing F12 when having your cursor in the style attribute.
The style prop on regular DOM elements should be in the form of an object where the keys are the css attributes.
Example:
<div style={{ width: "100%", backgroundColor: "black" }} />
Notice that in the case of attributes containing dashes, they become camel cased. For instance background-color becomes backgroundColor.
React style documentation
» npm install csstype
Typescript Type support for style objects
CSS Styling in Typescript
element.style.color = "red"; IS javascript. AFAIK typescript doesn't change anything about the dom API, it only adds things like static typing and enums.
Actually, I don't think this question is related to typescript at all.
Edit: I guess I misread what you said, but my point still stands, every line you posted here is valid javascript (and valid typescript).
More on reddit.comhow to set multiple CSS style properties in typescript for an element? - Stack Overflow
Is any[] the TypeScript equivalent of !important in CSS?
Videos
I'm fairly new to Typescript and I need to know how to style elements with Typescript. I already know that:
element.style.color = "red";
in Javascript is equivalent to
element.setAttribute("style", "color: red")in Typescript, but I'm having a few problems with converting a couple of other things, such as:
element.style.removeProperty("color");or
if (element.style.borderColor == "red")
Any help would be much appreciated. The lack of documentation and answers I can find using Google on these problems I encounter is disappointing to say the least.
element.style.color = "red"; IS javascript. AFAIK typescript doesn't change anything about the dom API, it only adds things like static typing and enums.
Actually, I don't think this question is related to typescript at all.
Edit: I guess I misread what you said, but my point still stands, every line you posted here is valid javascript (and valid typescript).
Typescript gets transpiled into vanilla js so that browsers can understand it. Typescript syntax is mostly the same.
Try using setAttribute. TypeScript does not have the style property on Element.
Copyelement.setAttribute("style", "color:red; border: 1px solid blue;");
Some related discussion in this GitHub issue: https://github.com/Microsoft/TypeScript/issues/3263
The API you were searching for is: https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration/setProperty
Copypublic static setStyleAttribute(element: HTMLElement, attrs: { [key: string]: string }): void {
for(const [key, value] of Object.entries(attrs)) {
element.style.setProperty(key, value);
}
}
And hyphen is not allowed in object keys, so use ' here, too:
Copylet elem: HTMLElement = document.getElementById('myDiv');
setStyleAttribute(elem, {'font-size':'12px', color: 'red', 'margin-top': '5px'});
» npm install ts-style