0

I am making a simple hello world app (currently migrating from angular 1) I used angular-cli to generate my components and it created a stylesheet and linked it in my component via styleUrls. None of my styles apply the way I think they are supposed to unless I do "viewEncapsulation.none." Is there something I'm missing? Or is there a better way to write out css for this?

If I use the default encapsulation ( ViewEncapsulation.Emulated or even native) my styles don't show up at all.

import { Component, OnInit } from '@angular/core';
import { ViewEncapsulation } from '@angular/core';  

@Component({
  selector: 'app-header',
  templateUrl: './header.component.html',
  encapsulation: ViewEncapsulation.None,
  styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {

constructor() { }

ngOnInit() {
}
}

My CSS looks like this:

.app-header{
    display: block;
    opacity:0.2;
}
1
  • I edited my response. Have you resolved the issue? Commented Dec 28, 2016 at 16:37

3 Answers 3

1

You need to use this css:

:host() {
    display: block;
    opacity:0.2;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Don't manipulate the component tag, this tag scope belongs to the parent component who uses it. manipulate only tags inside your html template component.

Comments

0

I think you need to set module:module.id in the component declaration for angular to search for the file in the right location. Have you verified that the CSS gets loaded in the non working case?

I am assuming you are using the app-header css somewhere in header.component.html? Try the below code.

import { Component, OnInit } from '@angular/core';
import { ViewEncapsulation } from '@angular/core';  

@Component({
  module: module.id,
  selector: 'app-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {

  constructor() { }

  ngOnInit() { }
}

3 Comments

Thank you so do put this in my decorator? moduleId: 'app-header',
angular-cli does not use the module Id.
Can you put the content of your component HTML file in the question as well so that I can give a more detailed and complete example.

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.