Skip to content

Commit 5881194

Browse files
committed
docs(sphinx) Render demos at real size, not stretched
why: The aspect-ratio hook forced width:100%, so a narrow demo (a 299px gif) was blown up to the ~750px column and went soft. The real pixels should show at their real size. what: - Inject width:<W>px; max-width:100% (was width:100%) so each demo renders at its true size, shrinks on a narrow screen, and never upscales — still aspect-ratio-reserved, so there is no layout shift
1 parent aea1c61 commit 5881194

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

docs/_ext/aspect_ratio.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
33
Sphinx's HTML writer renders image ``:width:`` as an inline pixel style, which
44
overrides the theme's ``height: auto`` and distorts responsive images. Instead
5-
this stamps each demo ``<img>`` with ``width:100%; aspect-ratio:W/H`` (computed
6-
from the source GIF), so the browser reserves the correct box before the image
7-
loads while keeping it fluid.
5+
this stamps each demo ``<img>`` with ``width:<W>px; max-width:100%;
6+
aspect-ratio:W/H`` (computed from the source GIF): the gif shows at its real
7+
size, shrinks on a narrow screen, never scales up, and the browser reserves the
8+
correct box before it loads (no layout shift).
89
"""
910

1011
from __future__ import annotations
@@ -49,7 +50,7 @@ def repl(match: re.Match[str]) -> str:
4950
size = _sizes.get(name)
5051
if size is None or "aspect-ratio" in tag:
5152
return tag
52-
style = f"width:100%;aspect-ratio:{size[0]}/{size[1]}"
53+
style = f"width:{size[0]}px;max-width:100%;aspect-ratio:{size[0]}/{size[1]}"
5354
if 'style="' in tag:
5455
return _STYLE.sub(f'style="{style}"', tag)
5556
return tag[:-1].rstrip("/") + f' style="{style}" />'

0 commit comments

Comments
 (0)