Styling [innerHTML] Content with Angular

Ole Ersoy
1 min readDec 22, 2019

--

Image by Hans Braxmeier from Pixabay

Scenario

We are including content like this:

<div class="content" [innerHTML]="topic.html"></div>

And styling the paragraphs in topic.html like this:

p {
color: red;
}

However the paragraph text is not turning red.

Approach

First import ViewEncapsulation :

import { ViewEncapsulation } from '@angular/core'

Then use encapsulation: ViewEncapsulation.None :

@Component({
selector: 'app-topic',
templateUrl: './topic.component.html',
styleUrls: ['./topic.component.scss'],
encapsulation: ViewEncapsulation.None
})
export class TopicComponent ..

On the component in order to switch off View Encapsulation for the components content.

Bonus

The same technique can be used to style content embedded with ng-content .

--

--

Ole Ersoy
Ole Ersoy

Written by Ole Ersoy

Founder of Firefly Semantics Corporation

Responses (3)