6

[Edited to better describe the problem]

I'm having difficulty applying various styles to :host in Angular 2. The simple plunker below demonstrates the problem. At first I thought that certain styles such as margin/padding weren't getting applied, but it seems that the issue is that the host element isn't behaving like a normal element. Notice that I've added a border and padding. The border looks really odd and the padding doesn't move the content inside the element at all (although it does appear to affect the way the border looks. This is the style I'm applying:

:host {padding:10px; border:1px solid red;}

The rendered code looks like this:

<sample-component _nghost-cxm-2="">
  <div _ngcontent-cxm-2="">
    <h3 _ngcontent-cxm-2="">Sample Component</h3>
  </div>
</sample-component>

I can see in dev tools that the styles are being applied to the <sample-component> element, but this is what the page looks like rendered:

Weird border behavior

I would expect the border to wrap the content inside the component but it is behaving oddly. Here is a sample plunker: https://plnkr.co/edit/k7LJcmVlhJINmakBJAau?p=preview

What am I missing?

3
  • its working as expected. I don't think so any problem. plnkr.co/edit/Fs2Igm0N2eMTE1dyMsdF?p=preview change value(px) and it will react accordingly. Commented Oct 8, 2016 at 7:01
  • Strange even your plnkr isn't working for me. What browser are you using? I've tried Firefox 49.0.1 and chrome 53.0.2785.143. Changing the 100px to any other value doesn't change anything. Commented Oct 8, 2016 at 15:10
  • I think you are looking for margin property. Padding is just working fine in chrome(latest version). try to add border:1px solid then change padding value and you'll see according behavior. Commented Oct 8, 2016 at 15:44

2 Answers 2

21

I just realized what the problem is. Host elements are set to display:inline. Changing the element to display:block fixes the problem. Not sure how I missed this before.

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

1 Comment

I know the rules say not to do +1 comments, but they say nothing about +5 :heart:
2

I already showed you that your plunker is working. Other than that you can use host metaproperty to apply style to host element as shown below,

https://plnkr.co/edit/7JquAioc6lUx9bUrPCiZ?p=preview

@Component({
  selector: 'sample-component',
  template: `
    <div>
      <h3>Sample Component</h3>
    </div>
  `,
  host:{
        'style': 'color:red;padding:50px',
       }
})
export class SampleComponent {}

UPDATE after your edit:

I may not give you an exact answer but there is some problem with <sample-component> element/tag which gets created in DOM. Maybe I understand what you're trying to achieve. I have a workaround for the same.

look at here - https://plnkr.co/edit/yLLsZABJWCrvE0CWHgFy?p=preview

1 Comment

As of Angular 16, the use of the host property in the @Component decorator is seems discouraged: Use @HostBinding or @HostListener rather than the host metadata property (angular.io/styleguide#style-06-03)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.