Style child component differently when it is in different parent components
up vote
0
down vote
favorite
In an Angular 5 application, I want to style the child slight different when it is inside different parent component list below.
E.g red background for card under listing component
green background for card under listing detail component
I want to need if I can do it through scss in the child component? Because I think it would be easier to trace when doing it inside the child component itself.
<listing>
<card />
<listing/>
<listingDetail>
<card />
</listingDetail>
Thanks.
angular sass angular-components
add a comment |
up vote
0
down vote
favorite
In an Angular 5 application, I want to style the child slight different when it is inside different parent component list below.
E.g red background for card under listing component
green background for card under listing detail component
I want to need if I can do it through scss in the child component? Because I think it would be easier to trace when doing it inside the child component itself.
<listing>
<card />
<listing/>
<listingDetail>
<card />
</listingDetail>
Thanks.
angular sass angular-components
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
In an Angular 5 application, I want to style the child slight different when it is inside different parent component list below.
E.g red background for card under listing component
green background for card under listing detail component
I want to need if I can do it through scss in the child component? Because I think it would be easier to trace when doing it inside the child component itself.
<listing>
<card />
<listing/>
<listingDetail>
<card />
</listingDetail>
Thanks.
angular sass angular-components
In an Angular 5 application, I want to style the child slight different when it is inside different parent component list below.
E.g red background for card under listing component
green background for card under listing detail component
I want to need if I can do it through scss in the child component? Because I think it would be easier to trace when doing it inside the child component itself.
<listing>
<card />
<listing/>
<listingDetail>
<card />
</listingDetail>
Thanks.
angular sass angular-components
angular sass angular-components
asked Nov 21 at 3:31
Kwinten
134
134
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
You can use Angular's ng-deep if you want to affect the styles of its child components.
1.) On you ListingComponent, setup ng-deep and access the card container class
@Component({
selector: 'listing',
template: `<ng-content></ng-content>`,
styles: [
`
:host ::ng-deep .card-container { background-color: red; } // Keep in mind, to include :host which means this component as without it, this style will leak out affecting not only its child component class .card-container but also its siblings derived from another component
`
]
})
export class ListingComponent {}
2.) On you ListingDetailComponent, setup ng-deep and access the card container class
@Component({
selector: 'listing-detail',
template: `<ng-content></ng-content>`,
styles: [
`
:host ::ng-deep .card-container { background-color: green; }
`
]
})
export class ListingDetailComponent {}
3.) On you CardComponent, supposedly you have a card container class
@Component({
selector: 'card',
template: `<div class="card-container">Hi I'm Card</div>`,
})
export class CardComponent {}
4.) On your AppComponent, same with your structure
<listing>
<card></card>
</listing>
<listing-detail>
<card></card>
</listing-detail>
Here's the StackBlitz demo link for your reference
OR If you want to control the styling from the child component, you can do so by specifying :host-context and the parent's class.
Example:
1.) Specify the parent class that we will use to access from our child component (card)
<listing class="list-parent">
<card></card>
</listing>
<listing-detail class="list-detail-parent">
<card></card>
</listing-detail>
2.) On your child component (CardComponent), specify host-context on your styles. This way you can style your parent component in corresponds to their classes.
@Component({
selector: 'card',
template: `<div class="card-container">Hi I'm Card</div>`,
styles: [
`
:host-context(.list-parent) { background-color: red; }
:host-context(.list-detail-parent) { background-color: green; }
`
]
})
export class CardComponent {}
Thanks a lot for your thorough explanation, but I want to do it in the child component which is the <card/> component in this case so that developer can notice that when they make changes on the card component
– Kwinten
Nov 21 at 7:15
@Kwinten had updated my answer in corresponds to styling your parent component inside the child component, hope it helps :)
– KShewengger
Nov 22 at 3:54
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You can use Angular's ng-deep if you want to affect the styles of its child components.
1.) On you ListingComponent, setup ng-deep and access the card container class
@Component({
selector: 'listing',
template: `<ng-content></ng-content>`,
styles: [
`
:host ::ng-deep .card-container { background-color: red; } // Keep in mind, to include :host which means this component as without it, this style will leak out affecting not only its child component class .card-container but also its siblings derived from another component
`
]
})
export class ListingComponent {}
2.) On you ListingDetailComponent, setup ng-deep and access the card container class
@Component({
selector: 'listing-detail',
template: `<ng-content></ng-content>`,
styles: [
`
:host ::ng-deep .card-container { background-color: green; }
`
]
})
export class ListingDetailComponent {}
3.) On you CardComponent, supposedly you have a card container class
@Component({
selector: 'card',
template: `<div class="card-container">Hi I'm Card</div>`,
})
export class CardComponent {}
4.) On your AppComponent, same with your structure
<listing>
<card></card>
</listing>
<listing-detail>
<card></card>
</listing-detail>
Here's the StackBlitz demo link for your reference
OR If you want to control the styling from the child component, you can do so by specifying :host-context and the parent's class.
Example:
1.) Specify the parent class that we will use to access from our child component (card)
<listing class="list-parent">
<card></card>
</listing>
<listing-detail class="list-detail-parent">
<card></card>
</listing-detail>
2.) On your child component (CardComponent), specify host-context on your styles. This way you can style your parent component in corresponds to their classes.
@Component({
selector: 'card',
template: `<div class="card-container">Hi I'm Card</div>`,
styles: [
`
:host-context(.list-parent) { background-color: red; }
:host-context(.list-detail-parent) { background-color: green; }
`
]
})
export class CardComponent {}
Thanks a lot for your thorough explanation, but I want to do it in the child component which is the <card/> component in this case so that developer can notice that when they make changes on the card component
– Kwinten
Nov 21 at 7:15
@Kwinten had updated my answer in corresponds to styling your parent component inside the child component, hope it helps :)
– KShewengger
Nov 22 at 3:54
add a comment |
up vote
2
down vote
accepted
You can use Angular's ng-deep if you want to affect the styles of its child components.
1.) On you ListingComponent, setup ng-deep and access the card container class
@Component({
selector: 'listing',
template: `<ng-content></ng-content>`,
styles: [
`
:host ::ng-deep .card-container { background-color: red; } // Keep in mind, to include :host which means this component as without it, this style will leak out affecting not only its child component class .card-container but also its siblings derived from another component
`
]
})
export class ListingComponent {}
2.) On you ListingDetailComponent, setup ng-deep and access the card container class
@Component({
selector: 'listing-detail',
template: `<ng-content></ng-content>`,
styles: [
`
:host ::ng-deep .card-container { background-color: green; }
`
]
})
export class ListingDetailComponent {}
3.) On you CardComponent, supposedly you have a card container class
@Component({
selector: 'card',
template: `<div class="card-container">Hi I'm Card</div>`,
})
export class CardComponent {}
4.) On your AppComponent, same with your structure
<listing>
<card></card>
</listing>
<listing-detail>
<card></card>
</listing-detail>
Here's the StackBlitz demo link for your reference
OR If you want to control the styling from the child component, you can do so by specifying :host-context and the parent's class.
Example:
1.) Specify the parent class that we will use to access from our child component (card)
<listing class="list-parent">
<card></card>
</listing>
<listing-detail class="list-detail-parent">
<card></card>
</listing-detail>
2.) On your child component (CardComponent), specify host-context on your styles. This way you can style your parent component in corresponds to their classes.
@Component({
selector: 'card',
template: `<div class="card-container">Hi I'm Card</div>`,
styles: [
`
:host-context(.list-parent) { background-color: red; }
:host-context(.list-detail-parent) { background-color: green; }
`
]
})
export class CardComponent {}
Thanks a lot for your thorough explanation, but I want to do it in the child component which is the <card/> component in this case so that developer can notice that when they make changes on the card component
– Kwinten
Nov 21 at 7:15
@Kwinten had updated my answer in corresponds to styling your parent component inside the child component, hope it helps :)
– KShewengger
Nov 22 at 3:54
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You can use Angular's ng-deep if you want to affect the styles of its child components.
1.) On you ListingComponent, setup ng-deep and access the card container class
@Component({
selector: 'listing',
template: `<ng-content></ng-content>`,
styles: [
`
:host ::ng-deep .card-container { background-color: red; } // Keep in mind, to include :host which means this component as without it, this style will leak out affecting not only its child component class .card-container but also its siblings derived from another component
`
]
})
export class ListingComponent {}
2.) On you ListingDetailComponent, setup ng-deep and access the card container class
@Component({
selector: 'listing-detail',
template: `<ng-content></ng-content>`,
styles: [
`
:host ::ng-deep .card-container { background-color: green; }
`
]
})
export class ListingDetailComponent {}
3.) On you CardComponent, supposedly you have a card container class
@Component({
selector: 'card',
template: `<div class="card-container">Hi I'm Card</div>`,
})
export class CardComponent {}
4.) On your AppComponent, same with your structure
<listing>
<card></card>
</listing>
<listing-detail>
<card></card>
</listing-detail>
Here's the StackBlitz demo link for your reference
OR If you want to control the styling from the child component, you can do so by specifying :host-context and the parent's class.
Example:
1.) Specify the parent class that we will use to access from our child component (card)
<listing class="list-parent">
<card></card>
</listing>
<listing-detail class="list-detail-parent">
<card></card>
</listing-detail>
2.) On your child component (CardComponent), specify host-context on your styles. This way you can style your parent component in corresponds to their classes.
@Component({
selector: 'card',
template: `<div class="card-container">Hi I'm Card</div>`,
styles: [
`
:host-context(.list-parent) { background-color: red; }
:host-context(.list-detail-parent) { background-color: green; }
`
]
})
export class CardComponent {}
You can use Angular's ng-deep if you want to affect the styles of its child components.
1.) On you ListingComponent, setup ng-deep and access the card container class
@Component({
selector: 'listing',
template: `<ng-content></ng-content>`,
styles: [
`
:host ::ng-deep .card-container { background-color: red; } // Keep in mind, to include :host which means this component as without it, this style will leak out affecting not only its child component class .card-container but also its siblings derived from another component
`
]
})
export class ListingComponent {}
2.) On you ListingDetailComponent, setup ng-deep and access the card container class
@Component({
selector: 'listing-detail',
template: `<ng-content></ng-content>`,
styles: [
`
:host ::ng-deep .card-container { background-color: green; }
`
]
})
export class ListingDetailComponent {}
3.) On you CardComponent, supposedly you have a card container class
@Component({
selector: 'card',
template: `<div class="card-container">Hi I'm Card</div>`,
})
export class CardComponent {}
4.) On your AppComponent, same with your structure
<listing>
<card></card>
</listing>
<listing-detail>
<card></card>
</listing-detail>
Here's the StackBlitz demo link for your reference
OR If you want to control the styling from the child component, you can do so by specifying :host-context and the parent's class.
Example:
1.) Specify the parent class that we will use to access from our child component (card)
<listing class="list-parent">
<card></card>
</listing>
<listing-detail class="list-detail-parent">
<card></card>
</listing-detail>
2.) On your child component (CardComponent), specify host-context on your styles. This way you can style your parent component in corresponds to their classes.
@Component({
selector: 'card',
template: `<div class="card-container">Hi I'm Card</div>`,
styles: [
`
:host-context(.list-parent) { background-color: red; }
:host-context(.list-detail-parent) { background-color: green; }
`
]
})
export class CardComponent {}
edited Nov 24 at 13:54
answered Nov 21 at 3:55
KShewengger
71839
71839
Thanks a lot for your thorough explanation, but I want to do it in the child component which is the <card/> component in this case so that developer can notice that when they make changes on the card component
– Kwinten
Nov 21 at 7:15
@Kwinten had updated my answer in corresponds to styling your parent component inside the child component, hope it helps :)
– KShewengger
Nov 22 at 3:54
add a comment |
Thanks a lot for your thorough explanation, but I want to do it in the child component which is the <card/> component in this case so that developer can notice that when they make changes on the card component
– Kwinten
Nov 21 at 7:15
@Kwinten had updated my answer in corresponds to styling your parent component inside the child component, hope it helps :)
– KShewengger
Nov 22 at 3:54
Thanks a lot for your thorough explanation, but I want to do it in the child component which is the <card/> component in this case so that developer can notice that when they make changes on the card component
– Kwinten
Nov 21 at 7:15
Thanks a lot for your thorough explanation, but I want to do it in the child component which is the <card/> component in this case so that developer can notice that when they make changes on the card component
– Kwinten
Nov 21 at 7:15
@Kwinten had updated my answer in corresponds to styling your parent component inside the child component, hope it helps :)
– KShewengger
Nov 22 at 3:54
@Kwinten had updated my answer in corresponds to styling your parent component inside the child component, hope it helps :)
– KShewengger
Nov 22 at 3:54
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404870%2fstyle-child-component-differently-when-it-is-in-different-parent-components%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown