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

Answer from theUtherSide on Stack Overflow
🌐
Oida
oida.dev › typescript-react › styles
TypeScript and React: Styles and CSS
TypeScript support comes through DefinitelyTyped: ... import styled from "styled-components"; export const Heading = styled.h1` font-weight: normal; font-style: italic; `; You get typings directly out of the box. You can constraint CSS properties to certain values if you like, or even pass custom properties to regular CSS properties.
🌐
CSS-Tricks
css-tricks.com › css-in-typescript-with-vanilla-extract
CSS in TypeScript with vanilla-extract | CSS-Tricks
October 7, 2021 - So, instead of injecting styles at runtime, vanilla-extract takes after Linaria and astroturf. These libraries let you author styles using JavaScript functions that get ripped out at build time and used to construct a CSS file. Although you write vanilla-extract in TypeScript, it doesn’t affect the overall size of your production JavaScript bundle.
Discussions

how to set multiple CSS style properties in typescript for an element? - Stack Overflow
This should be the accepted answer. TypeScript's CSSStyleDeclaration has number-based indexing (referenced in comments on other answers here) so string keys don't work. setProperty accepts string keys and has the same effect as style[key] = val without resorting to unsafe typing. More on stackoverflow.com
🌐 stackoverflow.com
html - Applying CSS to Element created in TypeScript - Stack Overflow
I would like to apply the CSS to the element created in TypeScript. Is this possible without adding the style in TypeScript (test.style.color = 'red';)? My HTML: More on stackoverflow.com
🌐 stackoverflow.com
CSS in TS
Afaik, CSS-in-js evolved mostly from a developer standpoint in response to react. It helps prevent context switching to another file and another language. It can also help with weird CSS inheritance issues since it'll compile the CSS used into a unique class if they aren't completely identical. I'd say if you feel confident in CSS, you're probably good putting it off learning it until a job requires it. Bias warning: I've only really used css-in-js for a contracting role I had once More on reddit.com
🌐 r/webdev
22
9
April 28, 2021
reactjs - How to import CSS modules with Typescript, React and Webpack - Stack Overflow
How to import CSS modules in Typescript with Webpack? Generate (or auto-generate) .d.ts files for CSS? And use classic Typescript import statement? With ./styles.css.d.ts: import * as styles from './ More on stackoverflow.com
🌐 stackoverflow.com
🌐
vanilla-extract
vanilla-extract.style
vanilla-extract — Zero-runtime Stylesheets-in-TypeScript.
Use TypeScript as your preprocessor. Write type‑safe, locally scoped classes, variables and themes, then generate static CSS files at build time.
🌐
npm
npmjs.com › package › typescript-plugin-css-modules
typescript-plugin-css-modules - npm
July 23, 2025 - A TypeScript language service plugin for CSS Modules.
      » npm install typescript-plugin-css-modules
    
Published   Jul 23, 2025
Version   5.2.0
Author   Brody McKee
🌐
Medium
medium.com › @dimi_2011 › setting-up-css-modules-in-typescript-project-52596526d19
Setting up CSS Modules in Typescript project | by Ivan Dimitrijevic | Medium
March 21, 2023 - Step 1 We need to define typings.d.ts file in root of our project in which we will describe how css module should be threated. ... Step 2 We need to install typescript-plugin-css-modules with npm.
🌐
GitHub
github.com › frenic › csstype
GitHub - frenic/csstype: Strict TypeScript and Flow types for style based on MDN data
TypeScript and Flow definitions for CSS, generated by data from MDN.
Starred by 1.8K users
Forked by 75 users
Languages   TypeScript 98.4% | JavaScript 1.6% | TypeScript 98.4% | JavaScript 1.6%
Find elsewhere
🌐
DEV Community
dev.to › domnikl › scoped-css-for-react-components-with-typescript-31a4
Scoped CSS for React Components with TypeScript - DEV Community
May 25, 2021 - I came to React, looking for Single File Components all over the place and as it turns out, it's not that easy 😔 What I did find though, is CSS Modules 🥳 It works like this: you import the CSS as a JavaScript module , which have been mapped from your CSS class names and assign those as className properties in the JSX. I used the npm package typescript-plugin-css-modules for this.
Top answer
1 of 2
2

You can directly add class to the element, created in the TypeScript file.

TypeScript Code

test.textContent = "Created in TS";
test.setAttribute("class","MyClass");
container.append(test);

CSS

.MyClass {
    color: green !important;
 }
2 of 2
0

Why not work:

The way Angular manage the .css (to isolate the .css of each element) is adding a prefix in .css. So if you check the .html using F12 in your navigator

You see a .css like

p[_ngcontent-ng-c474887881] {
  color: red;
}

And a .html like

<div _ngcontent-ng-c474887881="" class="testContainer">
   <p _ngcontent-ng-c474887881="">Created in HTML</p>
   <p>Created in TS</p>
</div>

(the _ng-content-ng-c474887881] can be another one, it's only to explain me.

So, when you create using simple javascript your "divs" has not the attribute _ngcontent-ng-..., and not get the .css

Solutions:

  1. You can use a global .css (adding in styles.css or using ViewEncapsulation.None), but in this case the .css is applied to all the application, and to make the .css it's only to the element created you need add a class to your element to be more specific the .css

  2. The another way is use Renderer2

      const container = document.querySelector('.testContainer') //(*)
    
      const div = this.renderer.createElement('p');
      const text = this.renderer.createText('Created in TS by renderer2');
    
      this.renderer.appendChild(div, text);
      this.renderer.appendChild(container, div);
    

    (*) I prefer use a template reference variable and ViewChild,

    In your .html

    <div #container>
      <p>Created in HTML</p>
      <!--  Created in TS-->
    </div>
    

    In .ts

    //I use static true because the div is NOT under a *ngIf
    @ViewChild('container',{static:true}) container!:ElementRef
    
    //and use
     this.renderer.appendChild(this.container.nativeElement, div);
    
🌐
Flowbite
flowbite.com › docs › getting-started › typescript
Tailwind CSS TypeScript - Flowbite
TypeScript is a free and open-source programming language that helps improve the scalability, maintainability, and readability of code. It does this by adding optional static typing to JavaScript.
🌐
GitHub
github.com › mrmckeb › typescript-plugin-css-modules
GitHub - mrmckeb/typescript-plugin-css-modules: A TypeScript language service plugin providing support for CSS Modules. · GitHub
A TypeScript language service plugin for CSS Modules.
Starred by 1.4K users
Forked by 77 users
Languages   TypeScript 93.3% | SCSS 2.0% | CSS 1.2% | JavaScript 1.0% | Sass 1.0% | Stylus 0.7%
🌐
Reddit
reddit.com › r/webdev › css in ts
r/webdev on Reddit: CSS in TS
April 28, 2021 -

Honest question: why is this a thing? I've been working with CSS for years and haven't tried anything related to CSS in TS or JS. If you are able to manage your styles with utility classes or classes for components in CSS directly, why would you use something like this?

I'm in no way criticizing the technology, I just want to know what problems tools like this one solve. If I'm able to style all my views with CSS with no problem, should I learn something like this and move to this approach?

Sprinkles - Atomic CSS in TS
Top answer
1 of 5
13
Afaik, CSS-in-js evolved mostly from a developer standpoint in response to react. It helps prevent context switching to another file and another language. It can also help with weird CSS inheritance issues since it'll compile the CSS used into a unique class if they aren't completely identical. I'd say if you feel confident in CSS, you're probably good putting it off learning it until a job requires it. Bias warning: I've only really used css-in-js for a contracting role I had once
2 of 5
9
If you are able to manage your styles with utility classes or classes for components in CSS directly, why would you use something like this? Good question, OP! Another commenter mentioned context switching; it's true that CSS in JS is a convenience because it keeps your styles with your component, rather than in a separate file. But CSS in JS doesn't just prevent context switching—you can also (very intelligently) interpolate values from things like objects or modify values using libraries like polished . You can write (and compose!) entire functions that generate dynamic CSS at run time based on various factors; this can be useful for creating a theme editor in an app, for example (having worked in this area at my current job, I can't imagine how we could do any of that with static stylesheets—create a class name for each possibility and dump it in the stylesheet?). Another benefit of using CSS in JS is that you can strictly enforce the types for values that you use in your CSS at compile time, whereas CSS alone barely has any checks in place. For example, if you use CSS custom properties for theming but accidentally misspell a variable's name, you won't get any warnings or errors. It just won't render properly. With CSS in JS, you'll get an error if you attempt to use something that's not defined. (Although, you can still generate invalid CSS with them, like interpolating an object). I used to not like CSS in JS until I worked with style-jsx , which imo is a lot better than styled-components and a lot of other CSS in JS libraries. If you know CSS, you can literally pick this up in just a few minutes and start using it.
Top answer
1 of 12
111

Now in the year 2021, all you need to do is add a src/Globals.d.ts to your project with these lines:

declare module "*.module.css";
declare module "*.module.scss";
// and so on for whatever flavor of css you're using

Then install and add

{
  "compilerOptions": {
    "plugins": [{ "name": "typescript-plugin-css-modules" }]
  }
}

to your tsconfig.

Example of this correctly functioning in VS code after making that simple change (root is a class defined in my stylesheet):

Webpack and tsc also compile correctly on the command line.

2 of 12
62

A) As you are saying, there is one simplest (not best) option to use require:

const css = require('./component.css')
  • We need to have typings for require as it's not standard feature in typescript.
  • Simplest typing for this specific require may be:

    declare function require(name: string): string;
    
  • Webpack will then compile typescript and use modules properly - BUT without any IDE help and class names checks for build.

B) There is better solution to use standard import:

import * as css from './component.css'
  • enables full class names IntelliSense
  • requires types definition for each css file, otherwise tsc compiler will fail

For proper IntelliSense, Webpack needs to generate types definition for each css file:

  1. Use webpack typings-for-css-modules-loader

    webpackConfig.module.loaders: [
      { test: /\.css$/, loader: 'typings-for-css-modules?modules' }
      { test: /\.scss$/, loader: 'typings-for-css-modules?modules&sass' }
    ];
    
  2. Loader will generate *.css.d.ts files for each of css files in your codebase

  3. Typescript compiler will understand that css import will be module with properties (class names) of type string.

Mentioned typings-for-css-loader contains a bug and because of types file generation delay, it's best to declare global *.css type in case our *.css.d.ts file is not generated yet.

That little bug scenario:

  1. Create css file component.css
  2. Include it in component import * as css from './component.css'
  3. Run webpack
  4. Typescript compiler will try to compile code (ERROR)
  5. Loader will generate Css modules typings file (component.css.d.ts), but it's late for typescript compiler to find new typings file
  6. Run webpack again will fix build error.

Easy fix is to create global definition (eg. file called typings.d.ts in your source root) for importing CSS Modules:

declare module '*.css' {
  interface IClassNames {
    [className: string]: string
  }
  const classNames: IClassNames;
  export = classNames;
}

This definition will be used if there is no css file generated (eg. you have added new css file). Otherwise will be used generated specific (needs to be in same folder and named same as source file + .d.ts extension), eg. component.css.d.ts definition and IntelliSense will work perfectly.

Example of component.css.d.ts:

export const wrapper: string;
export const button: string;
export const link: string;

And if you don't want to see generated css typings you may setup filter in IDE to hide all files with extension .css.d.ts in your sources.

🌐
Sololearn
sololearn.com › en › Discuss › 3102247 › how-can-you-change-css-with-typescript
How can you change css with typescript? | Sololearn: Learn to code for FREE!
November 8, 2022 - I was working in javascript, and now I have switched to angular where typescript is used instead of javascript. I have problem with changing value of css properties with ts. I want for example to chage background color of div by pressing button.
🌐
GitHub
github.com › typestyle › typestyle
GitHub - typestyle/typestyle: Making CSS Typesafe 🌹
Use it like you would use CSS modules or CSS in general with webpack etc, but this time you get to use TypeScript / JavaScript!
Starred by 3.1K users
Forked by 86 users
Languages   TypeScript 96.9% | JavaScript 1.7% | HTML 1.4% | TypeScript 96.9% | JavaScript 1.7% | HTML 1.4%
🌐
DhiWise
dhiwise.com › post › how-to-elevate-ui-design-using-react-cssproperties-typescript
Implementing React CSSProperties Typescript for Styling
May 20, 2024 - We then use this object within the style prop of a div element. TypeScript helps us by ensuring that the properties we use are valid CSS keys and that their values are of the correct type.
🌐
Emotion
emotion.sh › docs › typescript
Emotion – TypeScript
Emotion includes TypeScript definitions for @emotion/react and @emotion/styled. These definitions infer types for css properties with the object syntax, HTML/SVG tag names, and prop types.
🌐
Bobby Hadz
bobbyhadz.com › blog › typescript-add-css-style-to-element
Set CSS styles on an Element using TypeScript | bobbyhadz
February 29, 2024 - A step-by-step guide on how to set CSS styles on an element in TypeScript.