Lighthouse Part 2: Eliminating the Shifts
- Part 2 of 4 - Eliminating Cumulative Layout Shift (0.00 CLS) & Font Subsetting
Introduction
Building on the foundational build-time architectures established in Lighthouse Part 1: Parbaked Web , this analysis targets one of the most stubborn metrics in modern performance budgets: Cumulative Layout Shift (CLS). Unstable layouts trigger mis-clicks, disrupt reading flows, and incur severe algorithmic penalties.
Systematically eliminating these instabilities requires targeting the underlying DOM and CSS execution models through three distinct vectors:
- Platform Optimisation : This guide deconstructs four precise attack vectors that drove a Jekyll -based platform's CLS score from a failing 0.339 to a flawless 0.00.
- Rendering Mechanics : Dissecting font subsetting, critical CSS inlining, predictive aspect-ratio containers coupled with :has() pseudo-classes, and inert <template> tags exposes the mechanics of rendering stability.
- Structural Immutability : Readers gain a pragmatic roadmap for neutralising forced reflows , pre-allocating layout spaces, and offloading expensive geometric calculations to asynchronous APIs or compositor threads.
1. Font Subsetting & Inlined Critical CSS
External font libraries deploy monolithic bundles, triggering render-blocking chains and delivering thousands of unused Unicode glyphs. Resolving this requires pruning the network waterfall through three specific actions:
- Asset Removal : Completely removing the preloaded all.min.css from FontAwesome immediately eliminated 18 KiB of unused CSS.
- Glyph Subsetting : A manual Unicode glyph subsetting pass retained only the actively used Solid (fas) codepoints .
- Pipeline Automation : Automated build pipelines using pyftsubset mitigate the maintenance overhead required when introducing new icons.
These precise mappings were inlined directly into accessibility.css using the unicode-range descriptor. The heavy fa-regular-400.woff2 network preload was neutralised by migrating all icon references to Solid.
Injecting critical layout rules directly inside the HTML <head> dictates that structural styles parse synchronously before the first paint:
- Synchronous Parsing : This structural mandate prevents the generation of additional render-blocking requests.
- Metric Synchronisation : Replacing system fallbacks with subset fonts introduces structural risks if their font metrics differ.
- Hydration Locks : Strict CSS size-adjust properties or explicit font-display: block rules prevent structural shifting during the exact microsecond the custom font hydrates .

2. TOC Column, Aspect-Ratio Containers & Has-Selector Padding
Dynamically injected Table of Contents (TOC) columns project empty background boxes while JavaScript executes, violently shifting text downward upon population. Stabilising these regions requires explicit aspect-ratio containers (e.g., 32:9 for pictures) pre-configured at build time to hardcode reserved space for late-loading assets per the Optimise CLS guidelines.
Empty placeholder boxes are targeted via the CSS :has() pseudo-class:
.post-toc-column:has(nav) {
background: #f9f9f9;
padding: 15px;
border: 1px solid #eee;
}
This architectural safeguard ensures background borders and paddings paint exclusively when a child <nav> element is present. However, implementing this pattern requires managing three mechanical constraints:
- CSSOM Alteration : Dynamically painting padding or borders onto an in-flow container actively alters the CSSOM and forces a recalculation of the layout tree .
- Flow Isolation : This :has() -driven dynamic injection remains safe only if the TOC container is physically removed from the document flow using position: fixed or sticky sidebars.
- In-Flow Pre-allocation : If the element must remain in-flow, the space must be explicitly pre-allocated using min-height to absorb the dynamic injection without expanding the outer box boundaries.
The :has() selector requires Safari 15.4+, Chrome 105+, and Firefox 121+. In older environments, the column degrades safely to a transparent background.

3. Inert Template Tags for Overflow DOM Elements
Injecting dozens of overflow tags directly into the active document tree forces the browser into progressive reflows as elements cascade and wrap across lines. Neutralising this instability dictates that overflow nodes are isolated within inert <template> tags.

The browser parses and compiles these fragments at load time entirely outside the active rendering tree via HTMLTemplateElement . Maintaining elements in an inert state unlocks three specific performance benefits:
- Zero Overhead : They consume zero layout space and demand zero CSS computation overhead.
- On-Demand Hydration : A subsequent JavaScript routine clones and hydrates this pre-compiled fragment into the active DOM strictly on demand.
- Accessibility Preservation : This guarantees a minimal initial DOM footprint while retaining full accessibility , assuming JavaScript is available and enabled.
4. Asynchronous Offloading for Geometry Calculations
Extracting element geometry, such as scrollHeight or clientHeight—via synchronous JavaScript event listeners triggers immediate layout thrashing. The browser halts the main thread to recalculate the layout tree and return accurate geometric values prematurely.
Circumventing forced reflow and layout thrashing requires entirely discarding archaic techniques like binding height calculations to window load or scroll events. Modern standardisation dictates two specific architectural shifts:
- Compositor Thread Delegation : Reading progress bars and scroll-linked geometry relies natively on CSS Scroll-driven Animations (animation-timeline: scroll()).
- Thread Isolation : This paradigm offloads execution entirely to the compositor thread, eliminating the need for JavaScript, DOM event listeners, and layout thrashing.
Where JavaScript is strictly mandated for geometric calculations, reads must be offloaded using asynchronous API standards such as IntersectionObserver or ResizeObserver. These interfaces observe box-model mutations and trigger layout-dependent logic non-blockingly.

Conclusion & Strategic Advice
Orchestrating font subsetting, out-of-flow :has() -driven layout containers, inert DOM fragments, and asynchronous layout offloading transforms chaotic client-side rendering into mathematically deterministic paints. These strategies operate through three synergistic mechanisms:
- Geometric Bounding : CSS bounds guarantee initial geometric stability across dynamic load cycles.
- Overhead Elimination : Inert templates bypass DOM calculation overhead during the initial paint sequence.
- Thread Protection : Native compositor APIs ensure the main thread remains uninterrupted.
When architecting deployments, prioritise build-time bounds for static assets and enforce asynchronous offloading to protect visual integrity.
Continuing this architectural journey, our next exploration details client-side inclusivity. Discover how to engineer these systems without bloat in Lighthouse Part 3: Accessible Web Engineering .
Review your current performance traces, audit your layout shift bottlenecks, and begin enforcing zero-shift policies today.
💬 How is your engineering team tackling layout thrashing and protecting your performance budgets? Drop your strategies below and ♻️ share this blueprint with your front-end network!
Further Reading
- Cumulative Layout Shift (CLS) Definition & Scoring
- Practical Techniques to Optimise Cumulative Layout Shift
- MDN Web Docs: The :has() Pseudo-Class
More From Me
Original Article: Read on LinkedIn | Read on Substack