Difference between global scss file and scss file for component in Angular











up vote
-1
down vote

favorite
1












In a recent Angular 7 project, I have a component (defined in the file file-list.component.ts) in which there is a mat-paginator (a component from Angular material component library). When I want to change the background color of the mat-paginator, I first tried to put



.mat-paginator-container {
background-color: yellow;
}


in film-list.component.scss (the stylesheet for associated with this component), the background color of the paginator did not change. When I put this in app.component.scss, it did not work either. But when I put it in the src/styles.css, the background color is correctly changed.



So my questions are:




  • What is the difference between src/styles.scss, app.component.scss and film-list.component.scss?

  • What is the scope of each of these files?

  • What is the influence of body selector used in these stylesheet files?










share|improve this question


















  • 1




    Component stylesheets are scoped to the component. The global stylesheet isn't.
    – jonrsharpe
    Nov 10 at 18:03















up vote
-1
down vote

favorite
1












In a recent Angular 7 project, I have a component (defined in the file file-list.component.ts) in which there is a mat-paginator (a component from Angular material component library). When I want to change the background color of the mat-paginator, I first tried to put



.mat-paginator-container {
background-color: yellow;
}


in film-list.component.scss (the stylesheet for associated with this component), the background color of the paginator did not change. When I put this in app.component.scss, it did not work either. But when I put it in the src/styles.css, the background color is correctly changed.



So my questions are:




  • What is the difference between src/styles.scss, app.component.scss and film-list.component.scss?

  • What is the scope of each of these files?

  • What is the influence of body selector used in these stylesheet files?










share|improve this question


















  • 1




    Component stylesheets are scoped to the component. The global stylesheet isn't.
    – jonrsharpe
    Nov 10 at 18:03













up vote
-1
down vote

favorite
1









up vote
-1
down vote

favorite
1






1





In a recent Angular 7 project, I have a component (defined in the file file-list.component.ts) in which there is a mat-paginator (a component from Angular material component library). When I want to change the background color of the mat-paginator, I first tried to put



.mat-paginator-container {
background-color: yellow;
}


in film-list.component.scss (the stylesheet for associated with this component), the background color of the paginator did not change. When I put this in app.component.scss, it did not work either. But when I put it in the src/styles.css, the background color is correctly changed.



So my questions are:




  • What is the difference between src/styles.scss, app.component.scss and film-list.component.scss?

  • What is the scope of each of these files?

  • What is the influence of body selector used in these stylesheet files?










share|improve this question













In a recent Angular 7 project, I have a component (defined in the file file-list.component.ts) in which there is a mat-paginator (a component from Angular material component library). When I want to change the background color of the mat-paginator, I first tried to put



.mat-paginator-container {
background-color: yellow;
}


in film-list.component.scss (the stylesheet for associated with this component), the background color of the paginator did not change. When I put this in app.component.scss, it did not work either. But when I put it in the src/styles.css, the background color is correctly changed.



So my questions are:




  • What is the difference between src/styles.scss, app.component.scss and film-list.component.scss?

  • What is the scope of each of these files?

  • What is the influence of body selector used in these stylesheet files?







angular stylesheet






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 17:43









Bohao LI

12819




12819








  • 1




    Component stylesheets are scoped to the component. The global stylesheet isn't.
    – jonrsharpe
    Nov 10 at 18:03














  • 1




    Component stylesheets are scoped to the component. The global stylesheet isn't.
    – jonrsharpe
    Nov 10 at 18:03








1




1




Component stylesheets are scoped to the component. The global stylesheet isn't.
– jonrsharpe
Nov 10 at 18:03




Component stylesheets are scoped to the component. The global stylesheet isn't.
– jonrsharpe
Nov 10 at 18:03












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










src/styles.scss



Is for global CSS that will be applied across all the application and all the components. Here you can apply styling to body without problem.



example.component.scss



Is the CSS that will be scoped and applied to this specific component only. Here you won't be able to apply styling to body element.



You can still pierce the scoped boundaries...



When using components like mat-paginator inside, let say example.component.ts for example, the CSS from mat-paginator is in fact "outside" the example.component.ts component scope because mat-paginator has its own scope. Therefore you can pierce through the shadow-dom using ::ng-deep to apply CSS.



Working example with the code below: https://stackblitz.com/edit/angular-stackoverflow-53241725



/* not working because the class is not directly inside this component */
.mat-paginator-container {
background: yellow;
}

/* working because `ng-deep` pierce through the shadow-dom */
::ng-deep .mat-paginator-container {
background: red;
}


Suggested Documentation



Official Angular documentation about styling:
https://angular.io/guide/component-styles



Great blog that explains CSS encapsulation: https://blog.angular.io/the-state-of-css-in-angular-4a52d4bd2700






share|improve this answer























    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%2f53241725%2fdifference-between-global-scss-file-and-scss-file-for-component-in-angular%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
    1
    down vote



    accepted










    src/styles.scss



    Is for global CSS that will be applied across all the application and all the components. Here you can apply styling to body without problem.



    example.component.scss



    Is the CSS that will be scoped and applied to this specific component only. Here you won't be able to apply styling to body element.



    You can still pierce the scoped boundaries...



    When using components like mat-paginator inside, let say example.component.ts for example, the CSS from mat-paginator is in fact "outside" the example.component.ts component scope because mat-paginator has its own scope. Therefore you can pierce through the shadow-dom using ::ng-deep to apply CSS.



    Working example with the code below: https://stackblitz.com/edit/angular-stackoverflow-53241725



    /* not working because the class is not directly inside this component */
    .mat-paginator-container {
    background: yellow;
    }

    /* working because `ng-deep` pierce through the shadow-dom */
    ::ng-deep .mat-paginator-container {
    background: red;
    }


    Suggested Documentation



    Official Angular documentation about styling:
    https://angular.io/guide/component-styles



    Great blog that explains CSS encapsulation: https://blog.angular.io/the-state-of-css-in-angular-4a52d4bd2700






    share|improve this answer



























      up vote
      1
      down vote



      accepted










      src/styles.scss



      Is for global CSS that will be applied across all the application and all the components. Here you can apply styling to body without problem.



      example.component.scss



      Is the CSS that will be scoped and applied to this specific component only. Here you won't be able to apply styling to body element.



      You can still pierce the scoped boundaries...



      When using components like mat-paginator inside, let say example.component.ts for example, the CSS from mat-paginator is in fact "outside" the example.component.ts component scope because mat-paginator has its own scope. Therefore you can pierce through the shadow-dom using ::ng-deep to apply CSS.



      Working example with the code below: https://stackblitz.com/edit/angular-stackoverflow-53241725



      /* not working because the class is not directly inside this component */
      .mat-paginator-container {
      background: yellow;
      }

      /* working because `ng-deep` pierce through the shadow-dom */
      ::ng-deep .mat-paginator-container {
      background: red;
      }


      Suggested Documentation



      Official Angular documentation about styling:
      https://angular.io/guide/component-styles



      Great blog that explains CSS encapsulation: https://blog.angular.io/the-state-of-css-in-angular-4a52d4bd2700






      share|improve this answer

























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        src/styles.scss



        Is for global CSS that will be applied across all the application and all the components. Here you can apply styling to body without problem.



        example.component.scss



        Is the CSS that will be scoped and applied to this specific component only. Here you won't be able to apply styling to body element.



        You can still pierce the scoped boundaries...



        When using components like mat-paginator inside, let say example.component.ts for example, the CSS from mat-paginator is in fact "outside" the example.component.ts component scope because mat-paginator has its own scope. Therefore you can pierce through the shadow-dom using ::ng-deep to apply CSS.



        Working example with the code below: https://stackblitz.com/edit/angular-stackoverflow-53241725



        /* not working because the class is not directly inside this component */
        .mat-paginator-container {
        background: yellow;
        }

        /* working because `ng-deep` pierce through the shadow-dom */
        ::ng-deep .mat-paginator-container {
        background: red;
        }


        Suggested Documentation



        Official Angular documentation about styling:
        https://angular.io/guide/component-styles



        Great blog that explains CSS encapsulation: https://blog.angular.io/the-state-of-css-in-angular-4a52d4bd2700






        share|improve this answer














        src/styles.scss



        Is for global CSS that will be applied across all the application and all the components. Here you can apply styling to body without problem.



        example.component.scss



        Is the CSS that will be scoped and applied to this specific component only. Here you won't be able to apply styling to body element.



        You can still pierce the scoped boundaries...



        When using components like mat-paginator inside, let say example.component.ts for example, the CSS from mat-paginator is in fact "outside" the example.component.ts component scope because mat-paginator has its own scope. Therefore you can pierce through the shadow-dom using ::ng-deep to apply CSS.



        Working example with the code below: https://stackblitz.com/edit/angular-stackoverflow-53241725



        /* not working because the class is not directly inside this component */
        .mat-paginator-container {
        background: yellow;
        }

        /* working because `ng-deep` pierce through the shadow-dom */
        ::ng-deep .mat-paginator-container {
        background: red;
        }


        Suggested Documentation



        Official Angular documentation about styling:
        https://angular.io/guide/component-styles



        Great blog that explains CSS encapsulation: https://blog.angular.io/the-state-of-css-in-angular-4a52d4bd2700







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 10 at 19:30

























        answered Nov 10 at 19:12









        j3ff

        1,74811025




        1,74811025






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241725%2fdifference-between-global-scss-file-and-scss-file-for-component-in-angular%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

            Bressuire

            Vorschmack

            Quarantine