Skip to content

fix(emoji): reserve square box for img.emoji to avoid CLS#4685

Merged
imorland merged 1 commit into
2.xfrom
im/emoji-aspect-ratio-cls
Jun 2, 2026
Merged

fix(emoji): reserve square box for img.emoji to avoid CLS#4685
imorland merged 1 commit into
2.xfrom
im/emoji-aspect-ratio-cls

Conversation

@imorland

@imorland imorland commented Jun 2, 2026

Copy link
Copy Markdown
Member

Fixes #4681

Problem

img.emoji (the inline Twemoji <img> rendered by the formatter) is styled in extensions/emoji/less/forum.less with a height but no width and no aspect-ratio. The Twemoji SVGs are loaded from the jsDelivr CDN and the rendered <img> carries no intrinsic dimensions (the s9e formatter template emits <img alt="…" class="emoji" draggable="false" src="…"/> with no width/height).

With height: 1.5em but width: auto, the browser reserves zero width for each emoji until its SVG downloads, then reflows the surrounding text once each SVG arrives. On emoji-heavy posts this produces visible Cumulative Layout Shift (CLS), especially on first paint / cold CDN cache / throttled mobile connections.

Fix

Every Twemoji glyph is square (1:1), so declaring aspect-ratio: 1 lets the browser derive width: 1.5em immediately from the existing height and reserve the correct box before the SVG loads — eliminating the shift.

This lives in the extension's own stylesheet rather than the generated formatter bundle, so it survives bundle regeneration and is deployment-agnostic (correct for every install).

Testing

  1. Post a message containing several emoji (e.g. 😀🎹🔥👍🎉).
  2. Load the discussion with the Twemoji SVGs uncached (hard refresh, cache disabled, or network throttling).
  3. Before: text reflows horizontally as each SVG finishes loading. After: no layout shift.

Credit to @ekumanov for the report and the suggested fix.

Twemoji SVGs are loaded from a CDN and the rendered <img> carries no
intrinsic dimensions. With only `height: 1.5em` set and `width: auto`,
the browser reserves zero width per emoji until each SVG downloads,
reflowing surrounding text on load and producing visible Cumulative
Layout Shift.

All Twemoji glyphs are square, so declaring `aspect-ratio: 1` lets the
browser derive the width immediately and reserve the correct box before
the SVG arrives.

Fixes #4681
@imorland imorland requested a review from a team as a code owner June 2, 2026 11:48
@imorland imorland added this to the 2.0.0-rc.3 milestone Jun 2, 2026
@imorland imorland merged commit 37017bf into 2.x Jun 2, 2026
25 of 36 checks passed
@imorland imorland deleted the im/emoji-aspect-ratio-cls branch June 2, 2026 12:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(emoji) img.emoji lacks width/aspect-ratio, causing CLS while Twemoji SVGs load

1 participant