

Adapting typography to user preferences with CSS
source link: https://web.dev/adapting-typography-to-user-preferences-with-css/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Adapting typography to user preferences with CSS
A method to adapt a font to your users' preferences, so they're maximally comfortable reading your content.
Bringing the user into the design process has been an exciting time for users, designers and developers. Users can land on your experience and seamlessly begin consuming content, their preferences richly integrated into the design result.
This blog post explores using CSS media queries with a variable font to tailor the reading experience even further. Font grade and weight can be customized with font-variation-settings
, allowing microtuning given various preferences and contexts, like a preference for dark mode or high contrast. We can take these preferences and tailor a variable font for that user experience.
- Dark mode gets a slightly reduced gradation.
- High contrast gets a bolder font.
- Low contrast gets a thinner font.
https://codepen.io/argyleink/pen/mdQrqvj
Follow along to understand each portion of the CSS and variable font that enable this meaningful moment!
Getting setup #
To help focus on the CSS and font variation setting values, but also give us something to read and see, here's the markup you can use to preview the work:
<h1>Variable font weight based on contrast preference</h1>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit.
Officia, quod? Quidem, nemo aliquam, magnam rerum distinctio
itaque nisi nam, cupiditate dolorum ad sint. Soluta nemo
labore aliquid ex, dicta corporis.
</p>
Without adding any CSS, the font size is already adaptive to user preferences. Here's a video from another demo showing how setting font-size
in pixels will squash any user preferences, and why you should set your font size in rems:
Lastly, to center and support the demo, a little CSS:
@layer demo.support {
body {
display: grid;
place-content: center;
padding: var(--size-5);
gap: var(--size-5);
}
h1 {
text-wrap: balance;
}
}

This demo setup lets you begin testing and implementing this neat typography UX feature.
Loading the Roboto Flex variable font #
The adaptive strategy depends on a variable font with meaningful axes for customization, specifically you need GRAD
and wght
. The target adaptive user preferences in this article are for color scheme and contrast, both of which will tailor these axes to match the user's desired preference.
Load the variable font using the @font-face
API of CSS:
@font-face {
font-family: "Roboto Flex";
src: url('https://assets.codepen.io/2585/RobotoFlex') format('truetype');
}
Next, apply the font to some content. The following CSS applies it to everything:
@layer demo.support {
body {
font-family: Roboto Flex;
}
}

CSS custom properties and media queries for the win #
With the font loaded, you can query for user preferences and adapt the variable font settings to match.
Settings when there's no preferences (default) #
The following initial styles will be the default styles, or another way to look at it, the styles for users without any preferences.
@layer demo {
body {
--base-weight: 400;
--base-grade: 0;
font-variation-settings:
"wght" var(--base-weight),
"GRAD" var(--base-grade)
;
}
}
Settings when the preference is for high contrast #
For users who have indicated a preference for high contrast in their system settings, increase the --base-weight
value from 400
to 700
:
@layer demo {
@media (prefers-contrast: more) {
body {
--base-weight: 700;
}
}
}
Now there's more contrast while reading.
Settings when the preference is for low contrast #
For users who have indicated a preference for low contrast in their system settings, decrease the --base-weight
value from 400
to 200
:
@layer demo {
@media (prefers-contrast: less) {
body {
--base-weight: 200;
}
}
}
Now there's less contrast while reading.
Settings when the preference is for dark mode #
@layer demo {
@media (prefers-color-scheme: dark) {
body {
--base-grade: -25;
}
}
}
Now the perceptual differences of light on dark vs dark on light have been accounted for.
All together now #
@layer demo {
body {
--base-weight: 400;
--base-grade: 0;
font-variation-settings:
"wght" var(--base-weight),
"GRAD" var(--base-grade)
;
}
@media (prefers-contrast: more) {
body {
--base-weight: 700;
}
}
@media (prefers-contrast: less) {
body {
--base-weight: 200;
}
}
@media (prefers-color-scheme: dark) {
body {
--base-grade: -25;
}
}
}
Or, for fun, all together with CSS nesting:
@layer demo {
body {
--base-weight: 400;
--base-grade: 0;
font-variation-settings:
"wght" var(--base-weight),
"GRAD" var(--base-grade)
;
@media (prefers-contrast: more) { --base-weight: 700 }
@media (prefers-contrast: less) { --base-weight: 200 }
@media (prefers-color-scheme: dark) { --base-grade: -25 }
}
}
The result is a reading experience that adapts the font to match the user's preferences. Full source code is available below in the Codepen.
Last updated
Jul 27, 2023 — Improve articleRecommend
-
58
When Apple announced Dark Mode at WWDC 2018, one of my first thoughts was: “Aha! So that’s why Apple has us all switching our icons to template (monochrome) style images.” These images do tend to adapt pretty well to Dark...
-
32
Key Takeaways Nullable reference types need to be enabled on a per-project basis Nullable reference types may need to be disabled when working with generics Many w...
-
7
The case for passive preferencesIn Pace.dev users customise their experience passively as they interact with the UI. We achieve this by remembering their settings for next time in the brow...
-
11
Textretty A simple typography stylesheet used on HTML page. Introduction Textretty is a simple typography stylesheet which can be used on any html-based document. It's usually for article content...
-
7
Adapting to User Accessibility Settings in SwiftUIGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupShapeGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroupGroup...
-
6
Jan 18th, 2021Hasty Treat - CSS Typography and Systems👇 Download Show✏️ Edit Show Notes
-
8
Modern Fluid Typography Using CSS Clamp ...
-
8
In CSS, there are many different units that can be used to size elements on a page—px, vw, ch, em, rem, and
-
7
Fluid Typography: How To Predict A Problem With Your User’s Zoom-InIn this article, Ruslan Yevych will show you an easy way how to predict the appearance of a problem known as WCAG...
-
7
New Bing Errors, User Preferences and Company Reputations, Section 230 and LLMs Posted onTuesday, February 14, 2023Tuesday, February 14, 2023 Microsoft's demo...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK