Concept in Performance
Font and image optimization
Published Updated
Font and image loading feed into Largest Contentful Paint and Cumulative Layout Shift. A web font that blocks text or swaps late can hide text or shift it. Lazy-loading an image
above the fold, or serving a heavier JPEG where AVIF would do, can delay the largest paint. Shape font
loading with font-display and match fallback metrics with size-adjust in @font-face; for images, use loading,
fetchpriority, decoding, width, height, and <picture>. These controls are declarative.
How it works
Section titled “How it works”Fonts and images use different loading mechanisms.
font-display runs one timer per @font-face, which starts when the browser first tries to use the font and divides into three periods: a block period, during which text is drawn
invisible while the font loads, a swap period, during which a late-arriving font may still
replace the fallback, and a failure period. swap gives an extremely small block period and an infinite swap period, so text appears in the fallback
at once and changes when the font arrives; optional gives an extremely small block period and no swap period, so a font that arrives late does not replace the fallback on that page view; fallback gives an extremely small block period and a short swap period; block gives a short block period and an infinite swap period; Firefox exposes the “short” and “extremely small” durations as preferences
(font-display,
developer.mozilla.org). The descriptor is in Chrome 60, Firefox 58, and Safari 11.1 (BCD
css.at-rules.font-face.font-display).
The shift that swap causes shrinks with a size-matched fallback: a second @font-face
that names a local system font and uses size-adjust (Chrome 92, Firefox 92, Safari 17), ascent-override, and descent-override
(Chrome 87, Firefox 89, Safari still preview in BCD) so its lines occupy nearly the same height as the web font. The closer
the metrics match, the less the swap moves; exact line breaks still depend on glyph widths.
Images
Section titled “Images”loading="lazy" defers an image’s fetch until it approaches the viewport (Chrome 77, Firefox 75,
Safari 15.4) and should not be used on the LCP image, which the browser would then fetch late.
fetchpriority="high" raises an image’s relative fetch priority above the default (Low, or Medium for the first five large images since Chrome 117), which is what the LCP image needs when it competes with scripts (Chrome 101,
Firefox 132, Safari 17.2). decoding="async" lets other content be presented without waiting for the decode.
width and height give the image a layout box before it loads; without them a lazy image has
no size, and the browser may decide it is never near the viewport.
Format decides transfer size. AVIF is around half the size of a JPEG at comparable quality and
WebP about a quarter smaller; AVIF decodes in Chrome 85, Firefox 93, and Safari 16.1, WebP in every engine since Safari 14 (on macOS that also needs Big Sur or later) (Image file type and format
guide,
developer.mozilla.org). <picture> with <source type="image/avif"> lets the browser pick the
first format it can decode without a script.
Examples
Section titled “Examples”Font loading uses CSS, and image loading uses HTML. Detecting a format for script-generated images takes JavaScript.
A web font that cannot shift layout
Section titled “A web font that cannot shift layout”The first rule loads the web font with swap; the second declares a metric-matched fallback
and the font-family list names both. The override values come from the font’s metrics (tools
such as Fontaine or Capsize compute them); the numbers below are placeholders, not Brand Sans’s real metrics.
@font-face { font-family: "Brand Sans"; src: url("/fonts/brand-sans.woff2") format("woff2"); font-weight: 400; font-display: swap;}
@font-face { font-family: "Brand Sans Fallback"; src: local("Arial"); size-adjust: 104%; ascent-override: 92%; descent-override: 22%; line-gap-override: 0%;}
body { font-family: "Brand Sans", "Brand Sans Fallback", sans-serif;}On a slow connection the page renders in Arial scaled toward the same metrics; when brand-sans.woff2
arrives the glyphs change and, with real values in place, little should move. When the brand font is only decorative, font-display: optional skips the swap entirely on slow visits.
The hero image and a gallery
Section titled “The hero image and a gallery”The hero is eager and high priority with an AVIF source and a JPEG fallback; gallery images are lazy with explicit dimensions so they reserve space before they load.
<picture> <source srcset="/images/hero.avif" type="image/avif" /> <source srcset="/images/hero.webp" type="image/webp" /> <img src="/images/hero.jpg" width="1200" height="630" fetchpriority="high" decoding="async" alt="Product hero shot" /></picture>
<img src="/images/gallery-3.avif" width="400" height="300" loading="lazy" decoding="async" alt="Gallery photo 3" />The <img> inside <picture> should still carry width and height; the <source> elements
only choose the URL.
Detecting AVIF support for script-generated images
Section titled “Detecting AVIF support for script-generated images”Markup needs no detection because <picture> negotiates. Script that builds URLs (for
example from an API that returns one format) can probe once with a one-pixel AVIF and cache the
answer.
let avifSupport;function supportsAvif() { if (avifSupport) return avifSupport; if (!('Image' in window) || !('decode' in HTMLImageElement.prototype)) { avifSupport = Promise.resolve(false); // no decode(): request JPEG return avifSupport; } const probe = new Image(); probe.src = 'data:image/avif;base64,AAAAIGZ0eXBhdmlmAAAAAGF2aWZtaWYxbWlhZk1BMUIAAADybWV0YQAAAAAAAAAoaGRscgAAAAAAAAAAcGljdAAAAAAAAAAAAAAAAGxpYmF2aWYAAAAADnBpdG0AAAAAAAEAAAAeaWxvYwAAAABEAAABAAEAAAABAAABGgAAAB0AAAAoaWluZgAAAAAAAQAAABppbmZlAgAAAAABAABhdjAxQ29sb3IAAAAAamlwcnAAAABLaXBjbwAAABRpc3BlAAAAAAAAAAEAAAABAAAAEHBpeGkAAAAAAwgICAAAAAxhdjFDgQAMAAAAABNjb2xybmNseAACAAIABoAAAAAXaXBtYQAAAAAAAAABAAEEAQKDBAAAAB9tZGF0EgAKCBgABogQEDQgMgkxIABJSElBQkJB'; avifSupport = probe.decode().then(() => true).catch(() => false); // decode failure: fall back to JPEG return avifSupport;}decode() rejects with EncodingError when the format is unknown, which the catch maps to
false; the same rejection also covers a corrupt file, so the probe is only run on the known
sample.
See also
Section titled “See also”- font-display (developer.mozilla.org)
- Optimize resource loading with the Fetch Priority API (web.dev)
- Image file type and format guide (developer.mozilla.org)
- Resource hints (preload and preconnect)
- Core Web Vitals (LCP, INP, and CLS)
- Precaching strategies
Specifications
| Specification | Status |
|---|---|
| None. | |