Learn CSS from the Best Tutors
Search in
In Angular 6 and later versions, you can dynamically add a CSS class to an element using Angular's `ngClass` directive or by manipulating the `classList` property in your component.
Method 1: Using `ngClass` directive:
The `ngClass` directive allows you to conditionally apply CSS classes based on expressions. Here's an example:
1. **HTML Template:**
html
<!-- your-component.component.html -->
<div [ngClass]="{'highlight': isHighlighted}">
<!-- Your component content -->
</div>
2. **Component TypeScript:**
typescript
// your-component.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-your-component',
templateUrl: './your-component.component.html',
styleUrls: ['./your-component.component.css']
})
export class YourComponent {
isHighlighted: boolean = true; // Set this based on your condition
}
In this example, the `highlight` class will be applied to the `div` element when `isHighlighted` is `true`. You can adjust the condition based on your specific requirements.
Method 2: Using `classList` property:
1. **HTML Template:**
html
<!-- your-component.component.html -->
<div #myDiv>
<!-- Your component content -->
</div>
2. **Component TypeScript:**
typescript
// your-component.component.ts
import { Component, ElementRef } from '@angular/core';
@Component({
selector: 'app-your-component',
templateUrl: './your-component.component.html',
styleUrls: ['./your-component.component.css']
})
export class YourComponent {
constructor(private el: ElementRef) {}
addHighlightClass() {
const divElement = this.el.nativeElement.querySelector('#myDiv');
divElement.classList.add('highlight');
}
}
- In this example, we use the `ElementRef` to get a reference to the native DOM element with the ID `myDiv`.
- The `addHighlightClass` method uses the `classList.add` method to add the `highlight` class to the element.
Remember to replace `'highlight'` with your actual class name, and adjust the logic based on your specific requirements. The `ngClass` approach is more Angular-specific and is often preferred for dynamic class binding.
Related Questions
Why is HTML is used in Web Application?
Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com
Ask a QuestionRecommended Articles
Why Should you Learn HTML
Since the world today is ruled by the Internet, everyone desires to build a website for either business or personal interests. HTML or Hyper-text Mark-up Language is a globally standardized language which used to format web pages. By using HTML, the appearance of text, links, images and almost every part of a web page could...
Skills Required to Become a Front End Web...
Today, no business can exist without having its own website to interact with its customer base. Having a website is no more restricted to the big MNCs. With the advancement in technology and global business, even the small enterprises are spending on having their own websites and development teams. The person works on web...
Why Should you Become an IT Consultant
Information technology consultancy or Information technology consulting is a specialized field in which one can set their focus on providing advisory services to business firms on finding ways to use innovations in information technology to further their business and meet the objectives of the business. Not only does...
Learn Microsoft Excel
Microsoft Excel is an electronic spreadsheet tool which is commonly used for financial and statistical data processing. It has been developed by Microsoft and forms a major component of the widely used Microsoft Office. From individual users to the top IT companies, Excel is used worldwide. Excel is one of the most important...
Looking for CSS Training?
Learn from the Best Tutors on UrbanPro
Are you a Tutor or Training Institute?
Join UrbanPro Today to find students near youThe best tutors for CSS Classes are on UrbanPro
The best Tutors for CSS Classes are on UrbanPro