Styling not applying to child component

前端 未结 3 1978
自闭症患者
自闭症患者 2020-12-15 03:53

I\'m trying to apply styling to a child component tag, but I can\'t do that.

I have child component with anchor tag.

Even though i have styling for anchor

相关标签:
3条回答
  • 2020-12-15 04:12

    It's because by default components have view encapsulation (shadow dom). To disable this behavior, you can leverage the encapsulation attribute, as described below:

    import {Component, ViewEncapsulation} from '@angular/core';
    import {TestApp} from 'testapp.component.ts';
    @Component({
      selector:'test-component',
      styleUrls: ['test.component.css'],
      templateUrl: './test.component.html',
      directives:[TestApp],
      encapsulation: ViewEncapsulation.None // <------
    })
    export class TestComponent{
    
    }
    

    See this plunkr: http://plnkr.co/edit/qkhkfxPjgKus4WM9j9qg?p=preview.

    0 讨论(0)
  • 2020-12-15 04:17

    EDITED: This should solve:

    In the css of child class write below code

    :host.parentclass childclass{
    
    }
    

    The parentclass is immediate parent of child. childclass is the class applied to child component.

    0 讨论(0)
  • 2020-12-15 04:23

    When using the styleUrls property, the styles are local to the one component, not to its children. So I made two changes: 1) Moved the styleUrls to the testapp component. 2) Moved the div to the testapp component.

    import {Component} from '@angular/core';
    @Component({
     selector:'testapp',
     styleUrls: ['./test.component.css'],
     template: `
     <div class="test">
      <a href="https://www.google.com">Google</a>
     </div>
    
     `
    
    })
    export class TestApp{
    
    }
    
    0 讨论(0)
提交回复
热议问题