Code block in the wrong font: CSS cascade order and :where()
Introduction
On the Russian half of this site the Python code blocks had two problems at once. In some blocks the code rendered in Calibri, so the PEP 8 indentation fell apart. In others the lines wrapped in <code> used a different monospace font from the lines without it. Neither came from the page markup: both were decided by the CSS cascade.
This article shows the old rendering next to the fixed one, live on this page, explains which rule won and why, and shows the fix. The «before» blocks are drawn by frozen copies of the old rules, the legacy-* classes described at the end.
The symptom
The article on long lines in Python quotes the PEP 8 rules for wrapping arguments. The block has no <code> lines, the text sits directly in <p class="pycode p2">. Its Russian translation rendered it like this:
Before the fix, Russian pages (legacy-pycode legacy-p2):
# Good: # Wrapping by opening bracket. foo = long_function_name(var_one, var_two, var_three, var_four) # Alternative is a "Hanging" indent. foo = long_function_name( var_one, var_two, var_three, var_four)
After the fix (pycode p2):
# Good: # Wrapping by opening bracket. foo = long_function_name(var_one, var_two, var_three, var_four) # Alternative is a "Hanging" indent. foo = long_function_name( var_one, var_two, var_three, var_four)
Both blocks hold the same 25 spaces before var_three. In Calibri a space is much narrower than a letter, so the second line stops far short of the opening bracket it should line up with.
Cause 1: two rules of equal weight
Each served stylesheet of this site is two files joined together: the shared core first, then a small site file. The Russian sheet ends with source/sites/devhops/site_specific.css. Two rules set a font on the same element, <p class="pycode p2">, in this order:
/* core, source/_codeblocks.css: specificity 0,1,0 */
.pycode { font-family: sfmono-regular, menlo, monaco, consolas, liberation mono, "Courier New", monospace; }
/* later, source/sites/devhops/site_specific.css: specificity 0,1,0, wins */
.p1,.p10,.p100,.p12,.p15,.p2,.p20,.p3,.p4,.p5,.p7,.p8{box-sizing:border-box;font-family:calibri,sans-serif}
Each selector is one class, so both have the same specificity. With equal weight the cascade falls back to source order, and the later rule wins. The site file always comes after the core, so Calibri beat the code font on every element that had both a code class and a .p1–.p8 class.
The English site file has no font rule for these paragraph classes, which is why English pages kept the monospace block.
Cause 2: code lines use the browser font
Most code blocks wrap each line in <code>: a CSS counter on code draws the line numbers. No rule of the site set a font on code, so the browser’s own stylesheet applied: code { font-family: monospace }, the generic monospace font of the browser and the operating system. Text outside <code> uses the block’s own font stack. The same page therefore showed two monospace fonts.
Before the fix, English and Russian pages (legacy-pycode p2):
url = your_base_url + \
"/monitor-service/api/v1/components/744618a0-78c5-4e19-78f4-6d215bde64a5"
After the fix (pycode p2):
url = your_base_url + \
"/monitor-service/api/v1/components/744618a0-78c5-4e19-78f4-6d215bde64a5"
The fonts Chrome actually used on Linux, read from the live pages before the fix and from the same pages with the fixed stylesheet:
| Text | Before | After |
|---|---|---|
| block text without <code>, Russian page | Liberation Sans (Calibri is not installed, so sans-serif) | Liberation Mono |
| block text without <code>, English page | Liberation Mono | Liberation Mono |
| lines inside <code>, both pages | DejaVu Sans Mono | Liberation Mono |
On Windows the browser’s default monospace font is usually Consolas, which also leads the block’s stack, so the two blocks above may look the same there. On Linux and macOS they differ.
The fix
1. The paragraph font gets zero specificity. In the Russian and testsetup site files the selector list goes inside :where():
/* before */
.p1,.p10,.p100,.p12,.p15,.p2,.p20,.p3,.p4,.p5,.p7,.p8{box-sizing:border-box;font-family:calibri,sans-serif}
/* after */
:where(.p1,.p10,.p100,.p12,.p15,.p2,.p20,.p3,.p4,.p5,.p7,.p8){box-sizing:border-box;font-family:calibri,sans-serif}
:where() counts as 0,0,0 whatever it lists. The cascade compares specificity before source order, so any class that sets its own font on the same element now wins, wherever it sits in the sheet. A plain <p class="p2"> has no other font rule, so it keeps Calibri.
2. The lines of a code block inherit the block font. One rule in source/_codeblocks.css, right after the line counter:
:is(.somecode, .phpcode, .pycode, .shellscript, .jsoncode, .course_skeleton,
.rubycode, .asmcode, .cobolcode, .ccode, .cppcode, .dotnetcode, .elixircode,
.haskellcode, .erlangcode, .fortrancode, .gocode, .groovycode, .javacode,
.jscode, .rustcode, .typescriptcode, .htmlcode, .csscode, .perlcode,
.thriftcode, .csharpcode, .zplcode, .powercode, .robocode, .yamlfile,
.dockerfile, .markdown, .ini) code {
font-family: inherit;
}
The list holds the code-block classes only. An inline <code> inside a prose paragraph keeps the browser’s monospace on purpose: in Calibri it would no longer look like code. A test compares the list with the code-block classes defined in the file, so a new language class cannot be left out.
Then the four served sheets are rebuilt. From the container folder of the repository:
css_pipeline/copy-prod-css.sh
Why :where() and not another fix
| Option | Why not |
|---|---|
| Delete font-family from the .p1–.p8 rule | No ancestor of these paragraphs sets Calibri, so plain paragraphs fell back to Times New Roman when tried. |
| Repeat the code font for all 34 code classes after the paragraph rule | Works, but the list lives in two site files and has to follow every new code class. |
| Raise the specificity of the code classes, for example p.pycode | Touches 34 blocks of rules that all four sites share. |
| Remove p2 from the code blocks in the markup | About 3,470 files combine a code class with a paragraph class. |
| code { font-family: inherit } for every code element | 767 inline <code> elements in .p2 prose paragraphs would turn into Calibri. |
:where() and :is() work in every browser released since early 2021, and the minifier of the build, lightningcss, keeps both as written.
Side effect: callout boxes
Calibri used to beat every class on a .p1–.p8 paragraph of the Russian pages, not only code blocks. After the fix these boxes get their own fonts, the ones English pages had always shown:
| Class | Elements on Russian pages | Font after the fix |
|---|---|---|
| path | 842 | Palatino stack |
| action | 561 | code monospace stack |
| info | 365 | Consolas stack |
| 58 | Consolas stack | |
| answer, question | 29, 16 | Verdana |
| command, cmdout, quo | 9, 6, 4 | monospace stacks |
A box from the article on attaching files to an AI model. Before the fix, Russian pages (info legacy-p2):
An attached file goes to the model provider in full. On a free OpenCode model that may mean a contributor tier that trains on prompts. Never attach credentials, .env files, customer data or access logs.
After the fix (info p2):
An attached file goes to the model provider in full. On a free OpenCode model that may mean a contributor tier that trains on prompts. Never attach credentials, .env files, customer data or access logs.
How to check a page
The declared font and the font on screen are two different things: the first is a list, the second is the first font of the list that the system has. Chrome DevTools shows both:
- Right-click the block and choose Inspect.
- In the Computed tab, font-family shows the stack that won the cascade; expand it to see the rule and file it came from.
- At the bottom of the same tab, Rendered Fonts lists the fonts actually drawn, with the number of glyphs each.
- Select a <code> line inside the block and compare: before the fix its family was plain monospace.
The tables above came from headless Chrome: the DevTools protocol command CSS.getPlatformFontsForNode returns the same list as Rendered Fonts.
The legacy classes on this page
After the fix the normal classes no longer render the old way, so the «before» blocks on this page use frozen copies of the old rules. They live in source/_legacy_codeblocks.css, the last file of the shared core, and replay the old order: the core .p2, then .pycode, then the Calibri font of the site file.
| Class | Copy of | What it keeps |
|---|---|---|
| legacy-p2 | the core .p2 plus the Calibri font of the Russian site file | Calibri winning over the class next to it |
| legacy-pycode | .pycode as it was on 2026-11-18 | <code> lines in the browser’s monospace: the class is not in the :is() list |
<p class="legacy-pycode legacy-p2"> <!-- Russian pages before the fix -->
<p class="legacy-pycode p2"> <!-- English pages before the fix -->
<p class="info legacy-p2"> <!-- a callout box on Russian pages before the fix -->
They exist for this comparison only. New pages use the normal classes.
Article author: Klavdiya Kondakova