Skip to content

Commit c86d13c

Browse files
willshowellkara
authored andcommitted
docs(styles): add guide for overriding styles (#6541)
1 parent 1179868 commit c86d13c

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Customizing Angular Material component styles
2+
3+
### Styling concepts
4+
5+
There are 3 questions to keep in mind while customizing the styles of Angular Material
6+
components:
7+
8+
1. Are your styles encapsulated?
9+
2. Are your styles more specific than the defaults?
10+
3. Is the component a child of your component, or does it exist elsewhere in the DOM?
11+
12+
##### View encapsulation
13+
14+
By default, Angular component styles are scoped to affect the component's view. This means that
15+
the styles you write will affect all the elements in your component template. They will *not*
16+
affect elements that are children of other components within your template. You can read more
17+
about view encapsulation in the
18+
[Angular documentation](https://angular.io/guide/component-styles#view-encapsulation). You may
19+
also wish to take a look at
20+
[_The State of CSS in Angular_](https://blog.angular.io/the-state-of-css-in-angular-4a52d4bd2700)
21+
on the Angular blog.
22+
23+
##### Selector specificity
24+
25+
Each CSS declaration has a level of *specificity* based on the type and number of selectors used.
26+
More specific styles will take precedence over less specific styles. Angular Material uses the
27+
least specific selectors possible for its components in order to make it easy to override them.
28+
You can read more about specificity and how it is calculated on the
29+
[MDN web docs](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity).
30+
31+
##### Component location
32+
33+
Some Angular Material components, specifically overlay-based ones like MdDialog, MdSnackbar, etc.,
34+
do not exist as children of your component. Often they are injected elsewhere in the DOM. This is
35+
important to keep in mind, since even using high specificity and shadow-piercing selectors will
36+
not target elements that are not direct children of your component. Global styles are recommended
37+
for targeting such components.
38+
39+
### Styling overlay components
40+
41+
Overlay-based components have a `panelClass` property (or similar) that can be used to target the
42+
overlay pane. For example, to remove the padding from a dialog:
43+
44+
```scss
45+
// Add this to your global stylesheet after your theme setup
46+
.myapp-no-padding-dialog .mat-dialog-container {
47+
padding: 0;
48+
}
49+
```
50+
51+
```ts
52+
this.dialog.open(MyDialogComponent, {panelClass: 'myapp-no-padding-dialog'})
53+
```
54+
55+
Since you are adding the styles to your global stylesheet, it is good practice to scope
56+
them appropriately. Try prefixing your selector with your app name or "custom". Also note that
57+
the `mat-dialog-container`'s padding is added by default via a selector with specificity of 1. The
58+
customizing styles have a specificity of 2, so they will always take precedence.
59+
60+
### Styling other components
61+
62+
If your component has view encapsulation turned on (default), your component styles will only
63+
affect the top level children in your template. HTML elements belonging to child components cannot
64+
be targeted by your component styles unless you do one of the following:
65+
66+
- Add the overriding style to you global stylesheet. Scope the selectors so that it only affects
67+
the specific elements you need it to.
68+
- Turn view encapsulation off on your component. If you do this, be sure to scope your styles
69+
appropriately, or else you may end up incidentally targeting other components elswhere in your
70+
application.
71+
- Use a deprecated shadow-piercing descendant combinator to force styles to apply to all the child
72+
elements. Read more about this deprecated solution in the
73+
[Angular documentation](https://angular.io/guide/component-styles#deprecated-deep--and-ng-deep).

0 commit comments

Comments
 (0)