Style child component differently when it is in different parent components











up vote
0
down vote

favorite
1












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.










share|improve this question


























    up vote
    0
    down vote

    favorite
    1












    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.










    share|improve this question
























      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      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.










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 at 3:31









      Kwinten

      134




      134
























          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



          Demo





          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 {}





          share|improve this answer























          • 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











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          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

























          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



          Demo





          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 {}





          share|improve this answer























          • 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















          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



          Demo





          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 {}





          share|improve this answer























          • 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













          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



          Demo





          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 {}





          share|improve this answer














          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



          Demo





          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 {}






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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


















          • 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


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Trompette piccolo

          Slow SSRS Report in dynamic grouping and multiple parameters

          Simon Yates (cyclisme)