Difference between global scss file and scss file for component in Angular
up vote
-1
down vote
favorite
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
andfilm-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
add a comment |
up vote
-1
down vote
favorite
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
andfilm-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
1
Component stylesheets are scoped to the component. The global stylesheet isn't.
– jonrsharpe
Nov 10 at 18:03
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
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
andfilm-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
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
andfilm-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
angular stylesheet
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Nov 10 at 19:30
answered Nov 10 at 19:12
j3ff
1,74811025
1,74811025
add a comment |
add a comment |
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%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
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
1
Component stylesheets are scoped to the component. The global stylesheet isn't.
– jonrsharpe
Nov 10 at 18:03