2

I have two components, I want inner component extends outside component styles

@Component({
    selector: 'parent',
    style: [ '.panel{width:100px;height:100px;border:1px solid #ccc}' ],
    template: 'parent'
})

class ParentComponent 
{
}

@Component({
    selector: 'child',
    template: '<div class=".panel"></div>'
})

class ChildComponent 
{
}

using like this I want show a square

<parent>
    <child></child>
</parent>
Moon
  • 259
  • 1
  • 2
  • 12

3 Answers3

3

Have a look ViewEncapsulation on for your child component. You could maybe set the view encapsulation to none for the child component so it will inherit the parent styles:

import {ViewEncapsulation} from "@angular/core"
@Component({
    encapsulation: ViewEncapsulation.None,
    selector: 'child',
    template: '<div class=".panel"></div>'
}) 

I have never really tested it for this use-case but you might be able to do something with it.

rtn
  • 2,012
  • 4
  • 21
  • 39
2

You have the power to give child's css from parent component using >>> selector as shown below,

@Component({
    selector: 'parent',
    style: [ `
           :host >>> child .myCssclass
             {
               width:100px;height:100px;border:1px solid #ccc
             } 
           `],
    template: 'parent'
})

class ParentComponent 
{
}

@Component({
    selector: 'child',
    template: '<div class="myCssclass"></div>'
})

class ChildComponent 
{
}

How to style child components from parent component's css file?

Community
  • 1
  • 1
micronyks
  • 54,797
  • 15
  • 112
  • 146
0

The template of the child component should be like this:

template: '<div class="panel"></div>'

The class name should be without the dot ;)

Anyway this is not the Angular question. It belongs to CSS.

Next - the template of the parent should be:

template: '<child></child>'

And in the final template code you may leave just the

<parent></parent>
Roberc
  • 1,816
  • 3
  • 9
  • 12