Dynamic Font Scaling
Dynamic Font Scaling is a feature that allows users to choose the size of the text displayed on the screen. This helps users who need larger text for better readability, and it also accommodates users who can read smaller text.
Try It Out
Be sure to try this on an Android, iOS, or iPadOS device.
If you are testing on Chrome for Android, make sure"Accessibility Page Zoom" is enabled.
Follow theChanging the Font Size on a Device guide to set your preferred font size, and watch the text in the demo below grow or shrink according to your preferences.
Using Dynamic Font Scaling
Enabling in an Application
Dynamic Font Scaling is enabled by default as long as thetypography.css file is imported. Importing this file will define the--ion-dynamic-font variable which will activate Dynamic Font Scaling. While not recommended, developers can opt-out of Dynamic Font Scaling by setting this variable toinitial in their application code.
Integrating Custom Components
Developers can configure their custom components to take advantage of Dynamic Font Scaling by converting anyfont-size declarations that usepx units to userem units instead. An easy way to convert frompx torem is to divide the pixel font size by the default browser font size, which is typically16px. For example, if a component has a font size of14px, then this could be converted torem by doing14px / 16px = 0.875rem. Also note that any Ionic components that have had their font sizes overridden should also be updated to userem units.
One thing to keep in mind is that the dimensions of your components may need to change to accommodate the larger font sizes. For example,width andheight properties may need to change tomin-width andmin-height, respectively. Developers should audit their applications for any CSS properties that uselength values and make any applicable conversions frompx torem. We also recommend having long text wrap to the next line instead of truncating to keep large text readable.
Custom Font Family
We recommend using the default fonts in Ionic as they are designed to look good at any size and ensure consistency with other mobile apps. However, developers can use a custom font family with Dynamic Font Scaling via CSS:
html{
--ion-dynamic-font:var(--ion-default-dynamic-font);
--ion-font-family:'Comic Sans MS';
}
em units versusrem units
Developers have two options for relative font sizes:em andrem.
em units set the font size of an element relative to the font size of its parent.
In the following example, the computed font size of.child is40px because it is a child of.parent (2em * 20px = 40px).
.parent{
font-size:20px;
}
.child{
font-size:2em;
}
However, theem unit has a compounding effect which can cause issues. In the following example, the second.child element has a computed font size of80px since the font sizes compound.
<divclass="parent">
Parent element with 20px
<divclass="child">
Child element with 40px
<divclass="child">Child element with 80px</div>
</div>
</div>
Parent element with 20px
Child element with 40px
Due to this compounding effect, we strongly recommend usingrem units instead ofem units when working with Dynamic Font Scaling.rem units set the font size of an element relative to the font size of the root element, which is typically<html>. The default font size of the root element is typically16px.
In the following example, the computed font size of.child is32px because the font size is being computed relative tohtml, not.parent.
.parent{
font-size:20px;
}
.child{
font-size:2rem;
}
How Dynamic Font Scaling works in Ionic
Ionic components that define font sizes and participate in Dynamic Font Scaling typically userem units. This sizes the text in each component relative to the font size of the root element, which is usually thehtml element. This means that as the root element's font size changes, the text in all Ionic components scale in a consistent manner. This avoids the need to manually override each component's font size. Some elements inside of these components, such as icons, useem units instead so the elements are sized relative to the text, though the text itself is sized usingrem units.
iOS
Dynamic Font Scaling in Ionic builds on top of an iOS feature calledDynamic Type. To do this, Ionic sets thefont of the root element to an Apple-defined text style. For consistency, Ionic uses thebody text style.
Using the Apple-defined text style enables Dynamic Type, allowing all text in Ionic components to scale according to the system-level preference. Note that these Apple-defined fonts only work on Apple devices. As a result, these fonts will not work on Android devices even if your app is usingios mode.
Ionic followsApple's Human Interface Guidelines for Typography when an app is inios mode. As a result, important content is prioritized when the text size changes. This means a few things:
- Content in an
ion-headeror anion-footerwill have maximum font sizes to prioritize content inion-contentwhich is deemed more important than content in theion-headerandion-footer. - Components such as
ion-badgeandion-back-buttonwill have minimum font sizes so they remain readable. - Text in components such as
ion-tab-barandion-pickerdo not participate in Dynamic Font Scaling according to Apple's Human Interface Guidelines.
Android Web View
The Android Web View's font scaling mechanism is always enabled in web content and will automatically scale font sizes defined using thepx unit. This means that any maximum or minimum font sizes specified usingpx will still be scaled even if the final font size does not align with the maximum or minimum font sizes specified.
In the following example we are using themin() function to indicate that the font size of.foo should be no larger than14px.
.foo{
font-size:min(1rem,14px);
}
If the root element's default font size is16px, and the system-level font scale is1.5 (i.e text sizes should be increased by 50%), then1rem will evaluate to24px because16 * 1.5 = 24.
This is larger than our defined maximum of14px, so one might assume that the evaluated font size of.foo is14px. However, since the Android Web View scales any font sizes defined using thepx unit, this means the14px used in ourmin() function will also be scaled by 1.5.
As a result, this means that the maximum computed font size is actually21px since14 * 1.5 = 21 and therefore the overall computed font size of.foo is21px.
Chrome for Android
The Chrome Web Browser on Android behaves differently than the Android Web View. By default, Chrome for Android does not respect the system-level font scale setting. However, the Chromium team is working on a new feature to allow for this. When enabled, this feature will change thezoom level of thehtml element which will cause the layout to increase in size in addition to the text.
Developers can test this behavior by enabling the experimental "Accessibility Page Zoom" feature inchrome://flags.
Seehttps://bugs.chromium.org/p/chromium/issues/detail?id=645717 for more information.
Using Modes on Different Platforms
Each platform has slightly different font scaling behaviors, and theios andmd modes have been implemented to take advantage of the scaling behaviors on their respective platforms.
For example,ios mode makes use of maximum and minimum font sizes to followApple's Human Interface Guidelines for Typography.md mode does not implement this same behavior because Material Design does not have that same guidance. This means that usingmd mode on an iOS device may allow for very large font sizes in headers and footers.
As a result, we strongly recommend usingios mode on iOS devices andmd mode on Android devices when using Dynamic Font Scaling.
Changing the Font Size on a Device
Font scaling preferences are configured on a per-device basis by the user. This allows the user to scale the font for all applications that support this behavior. This guide shows how to enable font scaling for each platform.
iOS
Font scaling on iOS can be configured in the Settings app.
SeeApple Support for more information.
Android
Where users access the font scaling configuration varies across devices, but it is typically found in the "Accessibility" page in the Settings app.
The Chrome Web Browser on Android has some limitations with respecting system-level font scales. SeeChrome for Android for more information.
Troubleshooting
Dynamic Font Scaling is not working
There are a number of reasons why Dynamic Font Scaling may not have any effect on an app. The following list, while not exhaustive, provides some things to check to debug why Dynamic Font Scaling is not working.
- Verify that your version of Ionic supports Dynamic Font Scaling. Dynamic Font Scaling was added starting in Ionic v7.5.
- Verify that thetypography.css file has been imported. This file is required for Dynamic Font Scaling to work.
- Verify that your code does not override the root element's default font size. Manually setting a font size on the root element will prevent Dynamic Font Scaling from working as intended.
- Verify that your code does not override font sizes on Ionic components. Ionic components that set
font-sizerules will useremunits. However, if your app overrides that to usepx, then that custom rule will need to be converted to userem. SeeIntegrating Custom Components for more information. - Verify "Accessibility Page Zoom" is enabled if using Chrome for Android. SeeChrome for Android for more information.
Maximum and minimum font sizes are not being respected on Android
The Android Web View scales any font sizes defined using thepx unit by the system-level font scale preference. This means that actual font sizes may be larger or smaller than the font sizes defined inmin(),max(), orclamp().
Seehow font scaling works on Android for more information.
Font sizes are larger/smaller even with Dynamic Font Scaling disabled
Ionic components define font sizes usingrem units even when Dynamic Font Scaling is disabled. This sizes the text in each component relative to the font size of the root element, which is usually thehtml element. As a result, if the font size ofhtml changes, the computed font size of all Ionic components will change too.
Scaled Ionic iOS component font sizes do not exactly match native iOS equivalents
Certain native iOS components such as the Action Sheet make use of private font scales that Ionic does not have access to. While we try to stay as close as possible to the native behavior, text in some components may render slightly larger or smaller than their native counterparts.
The text size in my Ionic app on iOS changed when enabling Dynamic Font Scaling
The root element's default font size is typically16px. However, Dynamic Font Scaling on iOS devices make use of the"Body" text style which has a default font size of17px. Since the text in Ionic components is scaled relative to the root element's font size, some text may get larger or smaller when Dynamic Font Scaling is enabled, even if the system-level text scale did not change.
iOS provides a "Callout" text style which has a default font size of16px. However, this font style is currently not exposed to web content. Seethe supported text styles in WebKit for more information.
Contents
- Try It Out
- Using Dynamic Font Scaling
- How Dynamic Font Scaling works in Ionic
- Changing the Font Size on a Device
- Troubleshooting
- Dynamic Font Scaling is not working
- Maximum and minimum font sizes are not being respected on Android
- Font sizes are larger/smaller even with Dynamic Font Scaling disabled
- Scaled Ionic iOS component font sizes do not exactly match native iOS equivalents
- The text size in my Ionic app on iOS changed when enabling Dynamic Font Scaling