This is not how you do it in Angular! This is how you would do it:

In your CarItemComponent add a vairable for the font-size for example:

fontSize: number;

In your addStyle() set this number to the font size you want to use and in your template add this to the element which's font-size you want to change:

[style.font-size.px]="{{ fontSize }}"
Answer from tommueller on Stack Overflow
🌐
Angular
v2.angular.io › docs › ts › latest › guide › component-styles.html
Component Styles - ts - GUIDE
For every Angular component you ... rules, and media queries that you need. One way to do this is to set the styles property in the component metadata....
🌐
Medium
medium.com › @shivam29feb › how-to-add-css-class-using-typescript-in-angular-project-477d5b42cf1b
How to add CSS class using Typescript in Angular project - Shivam Shukla - Medium
April 12, 2023 - How to add CSS class using Typescript in Angular project we need to save changes in three files. .html .css .ts Here’s how we do it with an example app.component.html
🌐
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 - it seems like optional chaining in typescript is returning error for assignment operator '=' on DOM elements. try this: let name = document.getElementById('name') as HTMLElement; name?.style.setProperty('background-color', 'green') ... YJIT Cache Invalidation Failure, Prepended Anonymous Modules on Frozen Singleton Classes..
🌐
Angular
v17.angular.io › guide › component-styles
Angular
Angular is a platform for building mobile and desktop web applications. Join the community of millions of developers who build compelling user interfaces with Angular.
🌐
Udemy
udemy.com › development › web development › web development
Learn Basic HTML, CSS & Javascript (Typescript) with Angular | Udemy
Once students become familiar with HTML and CSS, they will dive into Javascript and Typescript. They will learn the basics of creating dynamic HTML components, using math in Javascript, and creating interactive events. Once the student gains a basic grasp of Typescript, then they learn how to apply this to Angular by making interactive components and getting data into those components.
Rating: 3.9 ​ - ​ 95 votes
Find elsewhere
🌐
xjavascript
xjavascript.com › blog › angular-set-style-from-typescript
Angular Set Style from TypeScript: A Comprehensive Guide — xjavascript.com
It allows you to set styles directly from a TypeScript property in the component class. The NgStyle directive is used to set multiple inline styles on an HTML element. It takes an object where the keys are CSS property names and the values are ...
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);
    
🌐
Briantree
briantree.se › home › angular › angular component styling: every way to add styles (complete guide)
Angular Component Styling: Every Way to Add Styles (Complete Guide) | Brian Treese
March 6, 2025 - For this tutorial, we’ll be using ... open the TypeScript for this example component. Now, to add styles here, we can add a “styles” property. This property accepts a template literal also known as a template string. Within ...
🌐
Coderanch
coderanch.com › t › 675989 › languages › Angular-Development-TypeScript-put-CSS
Angular 2 Development with TypeScript: Where to put CSS (HTML Pages with CSS and JavaScript forum at Coderanch)
February 9, 2017 - The angular-cli command ng init creates a basic Angular 2 app. One of the files that it creates is src/styles.css, and this is referenced in the angular-cli.json file.
🌐
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.
🌐
HTML Goodies
htmlgoodies.com › home › css
Binding CSS Styles to Events in Angular Applications | HTMLGoodies.com
September 20, 2021 - Angular lets you conditionally set both classes and styles via the ngClass and ngStyle directives respectively. Overall, these work very well. However, there are some instances where they cannot be utilized, i.e. for: the dynamic styling of elements using TypeScript variables that are not known until runtime, or
🌐
DEV Community
dev.to › michaeljota › how-to-share-styles-between-angular-components-nhm
How to share styles between Angular Components - DEV Community
January 19, 2020 - Today I'm going to show you different ways to share styles between components in Angular, and I guess... Tagged with angular, css, typescript.