Angular Directives Created: 16 Jan 2026 Updated: 16 Jan 2026

@HostBinding in Angular

In Angular, @HostBinding is a bridge that connects your directive's TypeScript logic to the properties of the HTML element it sits on. It can target four main categories: Classes, Styles, Properties, and Attributes.

Here is a comprehensive list of the most common and probable bindings you will use.

1. CSS Class Bindings

This is the most frequent use case. You can toggle a single class or manage all classes at once.

  1. Single Class (Boolean): Adds the class if the value is true.
@HostBinding('class.is-active') isActive = true;
  1. Multiple Classes (String): Replaces the entire class attribute.
@HostBinding('className') allClass = 'btn btn-primary shadow-lg';

2. CSS Style Bindings

You can bind to specific CSS properties. Angular also allows you to specify units (like px, %, em) directly in the binding.

  1. Standard Style:
@HostBinding('style.background-color') bgColor = 'red';
@HostBinding('style.display') display = 'block';
  1. Style with Units: (Very useful for dynamic layouts)
@HostBinding('style.width.px') width = 200;
@HostBinding('style.font-size.rem') size = 1.2;
@HostBinding('style.opacity') opacity = 0.5;

3. Native DOM Properties

These target the internal properties of the DOM element. Unlike attributes, these affect the "live" state of the element.

  1. Common Properties:
  2. TypeScript
@HostBinding('id') elementId = 'custom-id-01';
@HostBinding('disabled') isDisabled = true;
@HostBinding('tabIndex') focusOrder = 1;
@HostBinding('title') hoverText = 'Click to submit';
  1. Content Properties:
@HostBinding('innerText') text = 'Hello World';
@HostBinding('innerHTML') html = '<strong>Warning!</strong>';

4. HTML Attributes

Some values (like ARIA labels for accessibility or custom data attributes) are not properties but attributes. You must use the attr. prefix for these.

  1. Accessibility (ARIA):
@HostBinding('attr.role') role = 'alert';
@HostBinding('attr.aria-label') ariaLabel = 'Close modal';
@HostBinding('attr.aria-expanded') isExpanded = false;
  1. Custom Data Attributes:
@HostBinding('attr.data-cy') testId = 'login-button';
  1. Media Attributes:
@HostBinding('attr.src') imagePath = 'assets/logo.png';
@HostBinding('attr.alt') description = 'Company Logo';

Summary Table

Binding TypeSyntax ExampleCommon Use Case
Boolean Classclass.some-classToggling active, loading, or error states.
PropertydisabledEnabling/disabling buttons or inputs.
Unit Stylestyle.height.pxDynamic heights for progress bars or sidebars.
ARIA Attrattr.aria-hiddenManaging accessibility for screen readers.
ContentinnerTextDynamically changing labels (like your Copyright example).


Share this lesson: