Security News
NVD Backlog Tops 20,000 CVEs Awaiting Analysis as NIST Prepares System Updates
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
@material/typography
Advanced tools
Typography classes, mixins, and variables for Material Components for the web
@material/typography is a package from the Material Design library that provides a set of typography styles and utilities to help developers implement consistent and visually appealing text styles in their web applications. It follows the Material Design guidelines for typography, ensuring that text elements are readable, accessible, and aesthetically pleasing.
Applying Typography Styles
This feature allows you to apply predefined typography styles to HTML elements. By importing the CSS file and adding the appropriate class to an element, you can style text according to Material Design guidelines.
import '@material/typography/dist/mdc.typography.css';
const myElement = document.createElement('div');
myElement.className = 'mdc-typography--headline1';
myElement.textContent = 'Hello, World!';
document.body.appendChild(myElement);
Customizing Typography
This feature allows you to customize typography styles programmatically. By using the `typography` function, you can define custom styles and apply them to elements.
import { typography } from '@material/typography';
const customTypography = typography({
fontFamily: 'Roboto, sans-serif',
fontSize: '16px',
fontWeight: '400',
lineHeight: '1.5'
});
const myElement = document.createElement('div');
myElement.style.cssText = customTypography;
myElement.textContent = 'Custom Typography';
document.body.appendChild(myElement);
The 'typography' package is a powerful toolkit for building beautiful websites with consistent typography. It provides a wide range of customization options and integrates well with various CSS-in-JS libraries. Compared to @material/typography, it offers more flexibility and customization options but does not strictly follow Material Design guidelines.
The 'styled-components' package allows you to write actual CSS code to style your components. It supports theming and dynamic styling, making it a versatile choice for managing typography in React applications. While it is not specifically focused on typography, it provides the tools to create and manage text styles effectively. It offers more flexibility than @material/typography but requires more setup for consistent typography.
The 'emotion' package is a library designed for writing CSS styles with JavaScript. It offers powerful and flexible styling capabilities, including support for theming and dynamic styles. Like styled-components, it is not focused solely on typography but provides the tools to manage text styles effectively. It offers more flexibility and customization compared to @material/typography but requires more effort to adhere to Material Design guidelines.
Material Design's text sizes and styles were developed to balance content density and reading comfort under typical usage conditions.
MDC Typography is a foundational module that applies these styles to MDC Web components. The typographic styles in this module are derived from thirteen styles:
npm install @material/typography
We recommend using Roboto from Google Fonts:
<head>
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500" rel="stylesheet">
</head>
<body class="mdc-typography">
<h1 class="mdc-typography--headline1">Big header</h1>
</body>
@use "@material/typography/mdc-typography";
The typography styles (referred to as <STYLE>
below) used in the type system:
Scale | Description |
---|---|
headline1 | The largest text on the screen, reserved for short, important text or numerals |
headline2 | Headline variant 2 |
headline3 | Headline variant 3 |
headline4 | Headline variant 4 |
headline5 | Headline variant 5 |
headline6 | Headline variant 6 |
subtitle1 | Smaller than headline, reserved for medium-emphasis text that is shorter in length |
subtitle2 | Subtitle variant 2 |
body1 | Used for long-form writing |
body2 | Body variant 2 |
caption | Used sparingly to annotate imagery |
button | A call to action used by different types of buttons |
overline | Used sparingly to introduce a headline |
Some components have a set typographic style. For example, a raised MDC Card uses Body 1, Body 2, and Headline styles.
If you want to set the typographic style of an element, which is not a Material Design component, you can apply the following CSS classes.
CSS Class | Description |
---|---|
mdc-typography | Sets the font to Roboto |
mdc-typography--<STYLE> | Sets font properties as STYLE. Please see Typography styles section |
For example, the headline1
style as a CSS class would be mdc-typography--headline1
.
CSS Custom property | Description |
---|---|
--mdc-typography-font-family | The base font-family |
--mdc-typography-<STYLE>-font-family | The font-family for STYLE. Please see Typography styles section |
--mdc-typography-<STYLE>-font-size | The font-size for STYLE. Please see Typography styles section |
--mdc-typography-<STYLE>-line-height | The line-height for STYLE. Please see Typography styles section |
--mdc-typography-<STYLE>-font-weight | The font-weight for STYLE. Please see Typography styles section |
--mdc-typography-<STYLE>-letter-spacing | The letter-spacing for STYLE. Please see Typography styles section |
--mdc-typography-<STYLE>-text-decoration | The text-decoration for STYLE. Please see Typography styles section |
--mdc-typography-<STYLE>-text-transform | The text-transform for STYLE. Please see Typography styles section |
Mixin | Description |
---|---|
base | Sets the font to Roboto |
typography($style) | Applies one of the typography styles, including setting the font to Roboto |
smooth-font | Adds antialiasing for typography |
overflow-ellipsis | Truncates overflow text to one line with an ellipsis |
baseline($top, $bottom, $display) | Sets a container's baseline that text content will align to. |
zero-width-prefix | Adds an invisible, zero-width prefix to a container's text. This ensures that the baseline is always where the text would be, instead of defaulting to the container bottom when text is empty. Do not use this mixin if the baseline mixin is already applied. |
text-baseline($top, $bottom, $display) | Sets the baseline of flow text content. |
A note about
overflow-ellipsis
,overflow-ellipsis
should only be used if the element isdisplay: block
ordisplay: inline-block
.
$style
ValuesThese styles can be used as the $style
argument for the mdc-typography
mixin.
headline1
headline2
headline3
headline4
headline5
headline6
subtitle1
subtitle2
body1
body2
caption
button
overline
All styles can be overridden using CSS custom properties or Sass module/global variables.
When using Sass module variables, the module must be configured before any other @use
statements with a variable named $styles-{style}
. The variable should be assigned to a map
that contains all the properties you want to override for a particular style.
When using Sass global variables, they must be defined before the component is imported by setting a global
variable named $mdc-typography-styles-{style}
.
Example: Overriding the button font-size
and text-transform
properties.
CSS custom properties:
html {
--mdc-typography-button-font-size: 16px;
--mdc-typography-button-text-transform: none;
}
Sass module variables:
@use "@material/typography" with (
$styles-button: (
font-size: 16px,
text-transform: none,
)
);
@use "@material/button";
@include button.core-styles;
Sass global variables:
$mdc-typography-styles-button: (
font-size: 16px,
text-transform: none,
);
@import "@material/button/mdc-button";
Example: Overriding the global font-family
property.
CSS custom properties:
html {
--mdc-typography-font-family: Arial, Helvetica, sans-serif;
}
Sass module variables:
@use "@material/typography" with (
$font-family: unquote("Arial, Helvetica, sans-serif")
);
@use "@material/button";
@include button.core-styles;
Sass global variables:
$mdc-typography-font-family: unquote("Arial, Helvetica, sans-serif");
@import "@material/button/mdc-button";
Example: Overriding the font-family
property for headline1
and font-family
and font-size
for headline2
.
CSS custom properties:
html {
--mdc-typography-headline1-font-family: Arial, Helvetica, sans-serif;
--mdc-typography-headline2-font-family: Arial, Helvetica, sans-serif;
--mdc-typography-headline2-font-size: 3.25rem;
}
Sass module variables:
@use "@material/typography" with (
$styles-headline1: (
font-family: unquote("Arial, Helvetica, sans-serif")
),
$styles-headline2: (
font-family: unquote("Arial, Helvetica, sans-serif"),
font-size: 3.25rem
)
);
@use ...
Sass global variables:
$mdc-typography-styles-headline1: (
font-family: unquote("Arial, Helvetica, sans-serif")
);
$mdc-typography-styles-headline2: (
font-family: unquote("Arial, Helvetica, sans-serif"),
font-size: 3.25rem
);
@import ...
FAQs
Typography classes, mixins, and variables for Material Components for the web
We found that @material/typography demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 15 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.