-
Notifications
You must be signed in to change notification settings - Fork 6.8k
docs(customizing): add guide for overriding styles #6541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
44446f6
docs(customizing): add guide for overriding styles
willshowell 0d47e88
Be explicit that this is about styles only
willshowell 8d149ab
Add link to The State of CSS in Angular
willshowell 13d97c1
Fix typos
willshowell cc8a3b9
Fix typo
willshowell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Customizing Angular Material component styles | ||
|
||
### Styling concepts | ||
|
||
There are 3 questions to keep in mind while customizing the styles of Angular Material | ||
components: | ||
|
||
1. Are your styles encapsulated? | ||
2. Are your styles more specific than the defaults? | ||
3. Is the component a child of your component, or does it exist elsewhere in the DOM? | ||
|
||
##### View encapsulation | ||
|
||
By default, Angular component styles are scoped to affect the component's view. This means that | ||
the styles you write will affect all the elements in your component template. They will *not* | ||
affect elements that are children of other components within your template. You can read more | ||
about view encapsulation in the | ||
[Angular documentation](https://angular.io/guide/component-styles#view-encapsulation). | ||
|
||
##### Selector specificity | ||
|
||
Each CSS declaration has a level of *specificity* based on the type and number of selectors used. | ||
More specific styles will take precedence of less specific styles. Angular Material uses the least | ||
specific selectors possible for its components in order to make it easy to override them. You can | ||
read more about specificity and how it is calculated on the | ||
[MDN web docs](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity). | ||
|
||
##### Component location | ||
|
||
Some Angular Material components, specifically overlay-based ones like MdDialog, MdSnakcbar, etc., | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: |
||
do not exist as children of your component. Often they are injected elswhere in the DOM. This is | ||
important to keep in mind, since even using high specificity and shadow-piercing selectors will | ||
not target elements that are not direct children of your component. Global styles are recommended | ||
for targeting such components. | ||
|
||
### Styling overlay components | ||
|
||
Overlay-based components have a `panelClass` property (or similar) that can be used to target the | ||
overlay pane. For example, to remove the padding from a dialog: | ||
|
||
```scss | ||
// Add this to your global stylesheet after your theme setup | ||
.myapp-no-padding-dialog .mat-dialog-container { | ||
padding: 0; | ||
} | ||
``` | ||
|
||
```ts | ||
this.dialog.open(MyDialogComponent, {panelClass: 'myapp-no-padding-dialog'}) | ||
``` | ||
|
||
Since you are adding the styles to your global stylesheet, it is good practice to scope | ||
them appropriately. Try prefixing your selector with your app name or "custom". Also note that | ||
the `mat-dialog-container`'s padding is added by default via a selector with specificity of 1. The | ||
customizing styles have a specificity of 2, so they will always take precedence. | ||
|
||
### Styling other components | ||
|
||
If your component has view encapsulation turned on (default), your component styles will only | ||
affect the top level children in your template. HTML elements belonging to child components cannot | ||
be targeted by your component styles unless you do one of the following: | ||
|
||
- Add the overriding style to you global stylesheet. Scope the selectors so that it only affects | ||
the specific elements you need it to. | ||
- Turn view encapsulation off on your component. If you do this, be sure to scope your styles | ||
appropriately, or else you may end up incidentally targeting other components elswhere in your | ||
application. | ||
- Use a deprecated shadow-piercing descendant combinator to force styles to apply to all the child | ||
elements. Read more about this deprecated solution in the | ||
[Angular documentation](https://angular.io/guide/component-styles#deprecated-deep--and-ng-deep). |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good to include a link to the recent blog post on this subject as well: https://blog.angular.io/the-state-of-css-in-angular-4a52d4bd2700
(which was partially ghostwritten by me)