Skip to content

Commit dd85e3b

Browse files
authored
[css-env-1] Explainer meta text scale (#12377)
* [css-env-1] meta text-scale explainer * [css-env-1] Corrections to env explainer * [css-env-1] Convert MD to HTML table for rowspan
1 parent 8b291a8 commit dd85e3b

6 files changed

+273
-3
lines changed

css-env-1/explainers/env-preferred-text-scale.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
- [Alternatives considered](#alternatives-considered)
4848
- [New meta viewport key for changing text-scale](#new-meta-viewport-key-for-changing-text-scale)
4949
- [Fold OS-level font scale into initial font size](#fold-os-level-font-scale-into-initial-font-size)
50-
- [Footnotes](#footnotes)
5150

5251
<!-- TOC end -->
5352

@@ -475,8 +474,6 @@ Cons
475474
- Sites are NOT built correctly and things that used to just look small would now be clipped
476475
- You can simulate the effect of this alternative in Chrome by setting the UA-level font at chrome://settings/fonts. Set "font-size" to the maximum. old.reddit.com, gmail, etc, all break. Sites today ineffectively mix `px` and `em` because there is no safeguard against doing so – few authors change the UA-level font-size at chrome://settings/fonts.
477476

478-
## Footnotes
479-
480477
[^1]: Note: It is not the same as increasing the display pixel density (i.e. the `devicePixelRatio`).
481478
[^2]: According to research by Appt. [https://appt.org/en/stats/font-size](https://appt.org/en/stats/font-size)
482479
[^3]: There are other questions from authors on how to detect the OS-level font scale with no real answers, including on [StackOverflow](https://stackoverflow.com/questions/73725168/how-to-detect-if-the-user-have-a-bigger-font-setting-in-the-browser-or-in-the-mo) and [Reddit](https://www.reddit.com/r/Wordpress/comments/15n3xid/is_there_any_way_to_ensure_website_displays/).
Loading
Loading
Loading
Loading
Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
# Explainer: meta tag for text scaling behavior
2+
3+
> [!NOTE]
4+
> This explainer is to be merged into the [`env(preferred-text-scale)` Explainer](./env-preferred-text-scale.md) once the proposed behaviors in both have been finalized and implemented. Currently, these proposals remain separate to highlight the new additions presented herein.
5+
6+
## Authors
7+
8+
- Josh Tumath (@JoshTumath), BBC
9+
- David Grogan (@davidsgrogan), Google
10+
- Philip Rogers (@progers), Google
11+
12+
## Table of contents
13+
14+
<!-- TOC start (generated with https://github.com/derlin/bitdowntoc) -->
15+
16+
- [Introduction](#introduction)
17+
- [User-Facing Problem](#user-facing-problem)
18+
- [Goals](#goals)
19+
- [Non-goals (for now)](#non-goals-for-now)
20+
- [Proposal](#proposal)
21+
- [`legacy` option](#legacy-option)
22+
- [`scale` option](#scale-option)
23+
- [Comparison of `legacy` and `scale`](#comparison-of-legacy-and-scale)
24+
- [Alternatives Considered](#alternatives-considered)
25+
- [The `pem` unit](#the-pem-unit)
26+
27+
<!-- TOC end -->
28+
29+
## Introduction
30+
31+
Operating systems and web browsers provide global accessibility settings for the user to increase their system text scale. For authors to conform to the [WCAG 2.2 Resize Text success criterion](https://www.w3.org/TR/WCAG22/#resize-text), they must ensure that the user can increase the text size by up to 200%. Users can increase their text size globally in their operating system settings. However, browsers do not currently scale all text in a document based on the _OS-level_ text scale setting.
32+
33+
The new CSS `env(preferred-text-scale)` variable provides a mechanism for authors to respect the user’s text scale setting that they’ve set either in their operating system or web browser settings. Authors can use it to scale the `font-size` and alter the layout accordingly.
34+
35+
> [!NOTE]
36+
> See the [`env(preferred-text-scale)` Explainer](./env-preferred-text-scale.md) for a comparison of the various ways users can scale content and examples of how to use the environment variable.
37+
38+
However, if authors have already used font-relative units like `rem` and `em` to conform to the Resize Text guideline, the browser could automatically incorporate the OS-level text scale setting into those font-relative units. This would allow authors to avoid having to determine the precise elements to apply the env() variable to.
39+
40+
We propose a new HTML meta tag that tells the user agent to apply the scaling factor from `env(preferred-text-scale)` to the entire page. We expect it will become best practice for authors to use this meta tag, just as they would use the viewport meta tag. The environment variable would be reserved for atypical use cases.
41+
42+
## User-Facing Problem
43+
44+
The [`env(preferred-text-scale)` Explainer](./env-preferred-text-scale.md) explains:
45+
46+
> Research has shown that around 37% of Android users and 34% of iOS users have changed their system-level font scale factor from the default.
47+
48+
So over a third of mobile device users have expressed a preference for a different text scale to the default, but nothing happens on the web when they do.
49+
50+
Authors are taught to use font-relative units like `em` and `rem` to size content[^1]. It is a well-established best practice and authors have expressed to us that they would not like to migrate to a new best practice.
51+
52+
The Explainer explains [how authors would use the environment variable](https://github.com/w3c/csswg-drafts/blob/main/css-env-1/explainers/env-preferred-text-scale.md#how-authors-will-use-envpreferred-text-scale), and they would need to use it in `calc()` functions to set the root font-size and some media queries. They would need a lot of guidance to ensure it gets used correctly and there would be little variation in how they would use it.
53+
54+
Therefore, it would be much easier for authors if they could continue to use font-relative units as they do now and the UA initial font-size was redefined to incorporate the OS-level text scale setting. However, we can’t simply change the UA initial font size OS-level, as the Explainer notes:
55+
56+
> Browsers can’t simply apply the user’s system-level font scale to all web pages, because – if they did – **many** existing page layouts would break, causing content to be invisible or to lose interactivity.
57+
58+
So for authors to continue just using font-relative units, they would need a way to _opt-in_ to a UA initial font size that incorporates the OS-level text scale.
59+
60+
Authors would need to test their websites before opting in to see if they need to adapt them to protect from overflowing text or content that is too squashed to be able to read or view easily. They would need to pay special attention to scaled up text on mobile devices, where the viewport is so small that content is very likely to be squashed.
61+
62+
![Screenshot of ](images/background-bbc-text-scaled-squashed.png)
63+
64+
_Firefox simulating an iPhone SE 2 with a viewport of 375px on [bbc.co.uk](http://bbc.co.uk), where the text scale is set to 200%. Because the content is in two columns, the boxes can only fit one or two words per line. When the text is scaled up this much on a small viewport, a one column layout would improve readability._
65+
66+
> [!NOTE]
67+
> You could make the argument that users on desktop browsers can already change the UA’s initial font size in their browser settings, so there’s already a risk that websites could break.
68+
>
69+
> That is true, but the vast majority of users don’t do that[^2] (compared to approximately 36% of users on mobile OSs), so authors are much less likely to receive complaints about broken content, and therefore are less likely to be aware of it.
70+
>
71+
> Also, desktop browsers tend to have large viewports, so content is unlikely to get squashed. Authors are not used to dealing with scaled up text on small mobile-device-sized viewports.
72+
73+
### Goals
74+
75+
- Give authors an easy way to respect the OS-level text scale setting
76+
- Allow authors to continue to follow best-practices around using font-relative units
77+
- Don't break existing websites
78+
- Make sure font-relative units in media queries are affected by the OS-level and UA-level text scaling
79+
80+
### Non-goals (for now)
81+
82+
UA-supplied non-linear scaling (i.e. scaling larger text at a lower rate). For example, where the 16px body doubles to 32px, but a 32px heading only increases to 48px rather than doubling. This automatic non-linear scaling could be supported via a future `text-scale` value of `progressive`.
83+
84+
> [!NOTE]
85+
> Authors can already do non-linear scaling at the _application_ level via CSS today, using a formula such as:
86+
>
87+
> $$fontSize + (fontSize \cdot (preferredTextScale − 1)) \cdot rateOfIncrease$$
88+
>
89+
> For example:
90+
>
91+
> ```css
92+
> h1 {
93+
> --heading-size: 2rem;
94+
> --rate-of-increase: 0.5;
95+
>
96+
> font-size: calc(var(--heading-size) + (var(--heading-size) * (env(preferred-text-scale) − 1)) * var(--rate-of-increase);
97+
> }
98+
> ```
99+
100+
## Proposal
101+
102+
We propose a new HTML `meta` element extension with the keyword `text-scale`. The content value would be an enumerated type with the following options:
103+
104+
- `legacy`
105+
- `scale`
106+
107+
If the `text-scale` metadata is not included in the document, the `legacy` option would be the _missing default value_.
108+
109+
### `legacy` option
110+
111+
```html
112+
<meta name="text-scale" content="legacy" />
113+
```
114+
115+
When the author specifies `legacy`, or when `text-scale` is omitted, all affected features behave as they do today.
116+
117+
That means:
118+
119+
- The UA’s initial font-size (i.e. the computed value of `font-size: medium`) continues to be 16px by default. Desktop browser users continue to be able to change the initial font-size.
120+
- `env(preferred-text-scale)` only provides the scale factor on mobile browsers. On desktop browsers, the variable always has the value `1`.
121+
122+
`env(preferred-text-scale)` is always `1` on desktop because:
123+
124+
- Desktop browsers incorporate the UA-level font size setting into the initial font-size. If the environment variable, which also incorporates the UA-level font size setting, applied as well, content would be scaled twice.
125+
- On Windows, all major browsers do a full browser zoom of the web page in response to OS-level text scaling. If the environment variable applied as well, content would be scaled twice.
126+
- On macOS, no major browsers do anything in response to OS-level text scaling (nor do most of the built-in macOS apps).
127+
128+
### `scale` option
129+
130+
```html
131+
<meta name="text-scale" content="scale" />
132+
```
133+
134+
When the author specifies `scale`, the UA’s initial font size is affected by the OS-level text scale setting.
135+
136+
That means:
137+
138+
- The UA’s initial font-size (i.e. the computed value of `font-size: medium`) changes to 16px multiplied by `env(preferred-text-scale)`.
139+
- `env(preferred-text-scale)` provides the scale factor on both mobile and desktop browsers. It is derived from the OS-level font scale and UA-level font size setting. Desktop browser users continue to be able to change the initial font-size.
140+
141+
We expect `scale` to become best practice for authors to use on all new website designs, just as they use the viewport meta tag. It allows authors to continue to use font-relative units like `rem` and `em` like they normally would and mostly avoid using `env(preferred-text-scale)`.
142+
143+
### Comparison of `legacy` and `scale`
144+
145+
This comparison table summarises our proposal. **`legacy`** describes current behavior. **`scale`** represents a simple way for sites to obey the OS-level text settings.
146+
147+
<table>
148+
<thead>
149+
<tr>
150+
<th>Affected feature</th>
151+
<th><code>legacy</code></th>
152+
<th><code>scale</code></th>
153+
</tr>
154+
</thead>
155+
<tbody>
156+
<tr>
157+
<th><code>font-size: medium</code> on mobile</th>
158+
<td rowspan="2">16px</td>
159+
<td rowspan="4">16px × OS-level scale × UA-level scale</td>
160+
</tr>
161+
<tr>
162+
<th><code>1rem</code> or <code>1em</code> in Media Queries on mobile</th>
163+
</tr>
164+
<tr>
165+
<th><code>font-size: medium</code> on desktop</th>
166+
<td rowspan="2">16px × UA-level scale</td>
167+
</tr>
168+
<tr>
169+
<th><code>1rem</code> or <code>1em</code> in Media Queries on desktop</th>
170+
</tr>
171+
<tr>
172+
<th><code>env(preferred-text-scale)</code> on mobile</th>
173+
<td>OS-level scale</td>
174+
<td rowspan="2">OS-level scale × UA-level scale</td>
175+
</tr>
176+
<tr>
177+
<th><code>env(preferred-text-scale)</code> on desktop</th>
178+
<td>1</td>
179+
</tr>
180+
<tr>
181+
<th>Heuristic-based text autosizer (i.e. <code>text-size-adjust: auto</code>) on mobile</th>
182+
<td>Enabled</td>
183+
<td>Disabled</td>
184+
</tr>
185+
<tr>
186+
<th>Full-page zoom on Windows</th>
187+
<td>Enabled</td>
188+
<td>Disabled</td>
189+
</tr>
190+
</tbody>
191+
</table>
192+
193+
## Alternatives Considered
194+
195+
### The `pem` unit
196+
197+
`pem` would be a new CSS unit as a syntactic sugar for `env(preferred-text-scale)`. It would be defined as either:
198+
199+
1. Relative to the element font-size
200+
2. Relative to the UA initial font size.
201+
202+
We would expect authors to use `pem` units in place of the environment variable in most use cases.
203+
204+
After gathering feedback from authors, we don’t believe `pem` units are useful. Authors we spoke to described that they are already ‘doing the right thing’ by using existing font-relative units like `rem` and `em`, so they would not like to change their pages to use `pem` everywhere; they would like to continue to use `rem` and `em` and are surprised to learn that browsers do not already use OS-level text scales for the UA’s initial font size.
205+
206+
> [!NOTE]
207+
> We originally proposed `pem` units to the CSSWG along with the `env(preferred-text-scale)` proposal, but we no longer think it has merit due to the feedback from authors.
208+
209+
#### Option 1: Relative to the element font-size.
210+
211+
Equivalent to: `calc(1em * env(preferred-text-scale))`
212+
213+
```css
214+
:root {
215+
/* This line causes rem to include the OS-level text scale. */
216+
font-size: 1pem;
217+
}
218+
219+
.box {
220+
/* Authors can continue to use rem units. This rem is now relative to the modified
221+
* root font-size. */
222+
width: 10rem;
223+
}
224+
225+
.sidebar {
226+
display: grid;
227+
/* Authors commonly want text to scale, but not spacing between text, hence px here. */
228+
gap: 16px;
229+
230+
/* Units like rem and em aren't affected by changes to the stylesheet's root
231+
* font-size when used in media queries; they are instead relative to the UA's
232+
* initial font-size. For the same reason, even though pem is based on the
233+
* element font-size (just like em), we can use pem in a media query. */
234+
@media (width > 30pem) {
235+
grid-template-columns: 1fr 10rem;
236+
}
237+
}
238+
```
239+
240+
For this option, we don’t know if there would be a reason to use it within an element in the body of the document. If the author sets the font-size to `1pem`, they can still use `rem` units to size content and would likely only need `pem` for media queries.
241+
242+
#### Option 2: If pem is fixed to the browser’s initial font size
243+
244+
Equivalent to: `calc(the-ua-initial-font-size * env(preferred-text-scale))`
245+
246+
```css
247+
:root {
248+
/* This line causes rem to include the OS-level text scale. */
249+
font-size: 1pem;
250+
}
251+
252+
.box {
253+
/* Authors can use pem units everywhere, and they will work the same way
254+
* on all elements. They could still continue to use rem. */
255+
width: 10pem;
256+
}
257+
258+
.sidebar {
259+
display: grid;
260+
gap: 16px;
261+
262+
/* Authors could alternatively set:
263+
* @media (width > calc(30rem * env(preferred-text-scale))) */
264+
@media (width > 30pem) {
265+
grid-template-columns: 1fr 10pem;
266+
}
267+
}
268+
```
269+
270+
For this option, we envision authors using `pem` instead of `rem` everywhere for greenfield sites.
271+
272+
[^1]: It is mentioned in [WCAG 2.2](https://www.w3.org/WAI/WCAG22/Understanding/resize-text.html) and on websites like [CSS Tricks](https://css-tricks.com/accessible-font-sizing-explained/) and [Josh Comeau](https://www.joshwcomeau.com/css/surprising-truth-about-pixels-and-accessibility/).
273+
[^2]: Evan Minto from the Internet Archive found in 2018 that approximately 3% of their visitors on desktop browsers changed the default UA font size from 16px. ‘[Pixels vs. Ems: Users DO Change Font Size’.](https://medium.com/@vamptvo/pixels-vs-ems-users-do-change-font-size-5cfb20831773)

0 commit comments

Comments
 (0)