Accessibility mode without duplicate CSS
CSS blocks are copied by a script instead of by hand
In short
On tfagency.pl there is an “Accessibility mode” button: larger text, stronger contrast and no animations. The site already had 19 rules for people who turned off motion in their system settings. Instead of writing them a second time, I made a script copy them every time the site is built. Here is how it works and why a hand-made copy would sooner or later drift apart.
A setting few people know about
Phones and computers have an option in their accessibility settings to reduce motion. Among others, it is used by people who feel dizzy from animations. A website can read it and switch off things that slide, fly in or flicker.
A lot happens on tfagency.pl: smooth scrolling, a custom cursor, a video and an effect at the top of the page, headings that slide in line by line. That is why the stylesheets have 19 blocks that say what should stop when someone has reduced motion turned on.
Why a button as well
Not everyone knows about this setting, and not everyone wants to change it for the whole phone. So the header has a small icon of a person, and on phones there is a row in the menu. After a click, the site should look exactly as it does for someone with reduced motion, plus:
- Larger body text, at least 17 pixels. I do not enlarge the headings, because in the first attempt, when everything grew at once, the top of the page fell apart on phones.
- Stronger contrast. Grey text becomes black or nearly black.
- Underlined links, so you can tell them apart by more than colour.
- A very clear outline around the element chosen with the keyboard: yellow with a black ring, visible on any background.
- No video or effect in the header. A still image stays.
One caveat: a blind person uses a screen reader and does not need this button. What matters to them is how the code of the page is built. I write about that in A website accessible to blind people. The button is for people with low vision and people sensitive to motion.
The problem with a copy
The simplest option would be to write those 19 blocks a second time, just for the button. It would work until the first change. I add a new animation, write a rule to stop it for reduced motion and forget the second copy. A person with the system setting sees a calm page, and a person who clicked the button sees something moving. Nobody notices until it reaches someone it bothers.
A script instead of a copy
Instead, there is a short script that runs by itself before every build. It reads the stylesheets, finds all the reduced-motion blocks and writes a copy of them to a separate file. Each rule in the copy applies when accessibility mode is on. The copy is not edited by hand, and its first line says so.
@media (prefers-reduced-motion: reduce) {
.hero-film { animation: none; }
}
/* the script writes to a separate file: */
html[data-dostepnosc] .hero-film { animation: none; }There is also the opposite case: one block says “show this animation only when motion is allowed”. In accessibility mode, the script switches it off. It can only undo animations and transitions, though. If anything else ever appears in such a block, the script stops the build with a message saying which line needs handling by hand. I prefer a red error to a mode that quietly stopped matching.
Motion driven by code
Not everything can be stopped with styles. Smooth scrolling, the cursor, the header effect and the video that moves with the page run in JavaScript. Each of them asks the same question when it starts: is accessibility mode on, or reduced motion in the system? One function, one answer, so no element has its own slightly different version of the condition.
Because these elements start once, when the page opens, clicking the button reloads the page with the new setting. It is the simplest and most reliable solution.
So that nothing flashes
The choice is saved in the browser, so the mode stays on across pages. A tiny script at the very top of the page reads it before anything is drawn. If it did that later, every move to a new page would show the normal, animated look for a split second and then jump. The script also copes with browsers that block saving data, for example in private mode. The page then simply opens in its normal version, without an error.
What this means for your website
If your site has a lot of motion, check it with reduced motion turned on in your phone settings. Everything should still be there and readable, just calmer. If text disappears or something keeps moving, someone forgot about this option. If you would like me to check your site, get in touch.