|
| 1 | +# Angular Material typography |
| 2 | + |
| 3 | +### What is typography? |
| 4 | +Typography is a way of arranging type to make text legible, readable, and appealing when displayed. |
| 5 | +The Angular Material typography is based on the guidelines from the [Material Design spec][1] and is |
| 6 | +arranged into typography levels. Each level has a `font-size`, `line-height` and `font-weight`. The |
| 7 | +available levels are: |
| 8 | + |
| 9 | +* `display-4`, `display-3`, `display-2` and `display-1` - Used for large, one-off headers, usually |
| 10 | +at the top of the page (e.g. a hero header). |
| 11 | +* `headline` - Corresponds `<h1>` tag. |
| 12 | +* `title` - Corresponds `<h2>` tag. |
| 13 | +* `subheading-2` - Corresponds `<h3>` tag. |
| 14 | +* `subheading-1` - Corresponds `<h4>` tag. |
| 15 | +* `body-2` - Used for bolder body text. |
| 16 | +* `body-1` - Used as the base body text. |
| 17 | +* `caption` - Primarily for smaller body and hint text. |
| 18 | +* `button` - Used for styling buttons. |
| 19 | + |
| 20 | +The typography levels are collected into a typography config which is used to generate the CSS. |
| 21 | + |
| 22 | +[1]: https://material.io/guidelines/style/typography.html |
| 23 | + |
| 24 | +### Usage |
| 25 | +To use the Angular Material typography, you have to include the `Roboto` font with the 300, 400 and |
| 26 | +500 weights. You can host it yourself or include it from Google Fonts: |
| 27 | + |
| 28 | +```html |
| 29 | +<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500" rel="stylesheet"> |
| 30 | +``` |
| 31 | + |
| 32 | +Afterwards you have to add the appropriate CSS classes to the elements that you want to style: |
| 33 | + |
| 34 | +```html |
| 35 | +<h1 class="mat-display-1">Jackdaws love my big sphinx of quartz.</h1> |
| 36 | +<h2 class="mat-h2">The quick brown fox jumps over the lazy dog.</h2> |
| 37 | +``` |
| 38 | + |
| 39 | +By default, Angular Material doesn't apply any global CSS, however if you want to use the typography |
| 40 | +styles everywhere, you can apply the `mat-typography` class to an element or the `<body>` itself. |
| 41 | + |
| 42 | +```html |
| 43 | +<h1>This header is unstyled</h1> |
| 44 | + |
| 45 | +<section class="mat-typography"> |
| 46 | + <h1>This header will be styled</h1> |
| 47 | + <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nihil, molestias.</p> |
| 48 | +</section> |
| 49 | +``` |
| 50 | + |
| 51 | +### Customization |
| 52 | +If you want to override one or more of the typography levels, or to specify a different font, you |
| 53 | +can use the `mat-typography-level` and `mat-typography-config` functions to create your custom |
| 54 | +config. `mat-typography-level` takes in the desired `font-size`, `line-height` and `font-weight`, |
| 55 | +whereas `mat-typography-config` collects the levels into a configuration object. |
| 56 | + |
| 57 | +Afterwards you can pass the config to the typography mixins. Material uses a few mixins to generate |
| 58 | +the typography styles: |
| 59 | + |
| 60 | +* `angular-material-typography` - Includes the typography styles for all components, as well as the |
| 61 | +base typography. |
| 62 | +* `mat-base-typography` - Includes the typography for body text, headers etc. |
| 63 | +* `mat-{{component}}-typography` - Every Material component has a corresponding typography mixin, |
| 64 | +which allows for its styles to be overwritten (e.g. `mat-checkbox-typography`). |
| 65 | + |
| 66 | +For more details about the typography functions and default config, see the |
| 67 | +[source](https://github.com/angular/material2/blob/master/src/lib/core/typography/_typography.scss). |
| 68 | + |
| 69 | + |
| 70 | +```scss |
| 71 | +// Import the requied mixins. |
| 72 | +@import '~@angular/material/theming'; |
| 73 | + |
| 74 | +// Define a custom typography config that overrides the `headlines` and `body-1` levels. |
| 75 | +$custom-typography: mat-typography-config( |
| 76 | + $headline: mat-typography-level(32px, 48px, 700), |
| 77 | + $body-1: mat-typography-level(16px, 24px, 500) |
| 78 | +); |
| 79 | + |
| 80 | +// Override the base typography |
| 81 | +@include mat-base-typography($config); |
| 82 | + |
| 83 | +// Override the typography for a specific component. |
| 84 | +@include mat-checkbox-typography($config); |
| 85 | + |
| 86 | +// Override the typography for all components. Note that if you do this, |
| 87 | +// you don't have to call `mat-base-typography` explicitly. |
| 88 | +@include angular-material-typography($config); |
| 89 | +``` |
| 90 | + |
| 91 | +### Material typography in your custom CSS |
| 92 | +Material includes a few typography utility mixins and functions that you can use to customize your |
| 93 | +own components: |
| 94 | + |
| 95 | +* `mat-font-size($config, $level)` - Returns the font size, based on the provided config and level. |
| 96 | +* `mat-font-family($config)` - Returns the font family, based on the provided config. |
| 97 | +* `mat-line-height($config, $level)` - Returns the line, based on the provided config and level. |
| 98 | +* `mat-font-weight($config, $level)` - Returns the font weight, based on the provided config and |
| 99 | +level. |
| 100 | +* `mat-typography-level-to-styles($config, $level)` - Mixin that takes in a configuration object |
| 101 | +and a typography level, and outputs a short-hand CSS `font` declaration. |
| 102 | + |
| 103 | +```scss |
| 104 | +@import '~@angular/material/theming'; |
| 105 | + |
| 106 | +// Create a config with the default typography levels. |
| 107 | +$config: mat-typography-config(); |
| 108 | + |
| 109 | +// Custom header that only uses the Material `font-size` and `font-family`. |
| 110 | +.custom-header { |
| 111 | + font-size: mat-font-size($config, headline); |
| 112 | + font-family: mat-font-family($config); |
| 113 | +} |
| 114 | + |
| 115 | +// Custom title that uses all of the typography styles from the `title` level. |
| 116 | +.custom-title { |
| 117 | + @include mat-typography-level-to-styles($config, title); |
| 118 | +} |
| 119 | +``` |
| 120 | + |
0 commit comments