Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styling Angular 2 component tag

Tags:

angular

Inside my app-root component, I've app-container component. How would I style the app-container component?

HTML:

<app-container></app-container>

CSS:

app-container {
  background: red;
}

^ The above css doesn't work.

I want to style and position the app-container component. I tried googling but couldn't find a proper solution. Please Help.

Thanks in advance!

like image 873
Aravind Vasudevan Avatar asked Apr 25 '17 14:04

Aravind Vasudevan


1 Answers

Yes there is. If you have enabled shadow-dom or the emulation of shadow-dom you can style the tag via the :host property. (Emulation is enabled per default)

E.G.

:host{
   background-color:red;
}

I highly suggest you this article: https://angular.io/guide/component-styles

Your css code above should work if it is in the global css file not in the component css file.

Don't forget to check the styleUrl path. Here an example of styleUrls.

@Component({
  selector: 'hero-details',
  template: `
    <h2>{{hero.name}}</h2>
    <hero-team [hero]=hero></hero-team>
    <ng-content></ng-content>
  `,
  styleUrls: ['app/hero-details.component.css']
})
export class HeroDetailsComponent {
/* . . . */
}

Here more details on emulated / native -shadow dom: https://angular.io/guide/component-styles#view-encapsulation

like image 113
DaniS Avatar answered Oct 14 '22 07:10

DaniS