1

I have a component in Angular2 and I want to set the padding on the host element.

@Component({
    selector: 'my-app',
    template: 'Hello world'
    styles: ['my-app {padding: 10px}']
})

in browser, my style looks like this:

my-app[_ngcontent-icu-3] {padding: 10px;}

while my host element looks like this:

<my-app _ngcontent-icu-1="" _nghost-icu-3="">
    <div class="content" _ngcontent-icu-3=""></div>
</my-app>

As I understand it, Angular2 adds these _ng attributes so CSS references specific components.

But how do I reference the host element from the CSS defined inside the component?

1 Answer 1

3
styles: [':host {padding: 10px}']

or

@Component({
    selector: 'my-app',
    template: 'Hello world'
    host: {'[style.padding.px]': '"10"'}
})

The added _ng... classes are for emulating scoped styles (default encapsulation: ViewEncapsulation.Emulated) similar to what shadow DOM does.

Sign up to request clarification or add additional context in comments.

1 Comment

The first one is what I was looking for. I can use that in the CSS file as well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.