Skip to content

Commit d092530

Browse files
committed
fix: Avoid layout shifts by pre-allocating space for pictures
So far, images don't set a fixed height for images. This leads to layout shifts while loading since CSS can only recognize the size of the image when loaded. By setting the aspact ratio on the img-element, the browser can already pre-allocate the necessary space. When the image is loaded, the layout no longer shifts.
1 parent 68f31b7 commit d092530

File tree

1 file changed

+36
-25
lines changed

1 file changed

+36
-25
lines changed

layouts/partials/image.html

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,41 @@
1313
{{- $name := $image.RelPermalink | strings.TrimSuffix (printf ".%s" $image.MediaType.SubType) -}}
1414
{{- $extension := $image.MediaType.SubType -}}
1515

16-
<picture>
17-
{{- $widths := slice -}}
18-
{{- if gt $image.Width 500 -}}
19-
{{- $widths = $widths | append 500 -}}
20-
{{- end -}}
21-
{{- if gt $image.Width 800 -}}
22-
{{- $widths = $widths | append 800 -}}
23-
{{- end -}}
24-
{{- if gt $image.Width 1200 -}}
25-
{{- $widths = $widths | append 1200 -}}
26-
{{- end -}}
27-
{{- if gt $image.Width 1500 -}}
28-
{{- $widths = $widths | append 1500 -}}
29-
{{- end -}}
30-
{{- if gt $image.Width 2200 -}}
31-
{{- $widths = $widths | append 2200 -}}
32-
{{- end -}}
16+
{{- $widths := slice -}}
17+
{{- if gt $image.Width 500 -}}
18+
{{- $widths = $widths | append 500 -}}
19+
{{- end -}}
20+
{{- if gt $image.Width 800 -}}
21+
{{- $widths = $widths | append 800 -}}
22+
{{- end -}}
23+
{{- if gt $image.Width 1200 -}}
24+
{{- $widths = $widths | append 1200 -}}
25+
{{- end -}}
26+
{{- if gt $image.Width 1500 -}}
27+
{{- $widths = $widths | append 1500 -}}
28+
{{- end -}}
29+
{{- if gt $image.Width 2200 -}}
30+
{{- $widths = $widths | append 2200 -}}
31+
{{- end -}}
3332

34-
{{- $srcset := slice -}}
35-
{{- range $i, $width := $widths -}}
36-
{{- with $image.Resize (printf "%dx" $width) | resources.Copy (print $name "_" $width "." $extension) -}}
37-
{{- $srcset = $srcset | append (printf "%s %dw" .RelPermalink $width) -}}
38-
{{- end -}}
33+
{{- $srcset := slice -}}
34+
{{- range $i, $width := $widths -}}
35+
{{- with $image.Resize (printf "%dx" $width) | resources.Copy (print $name "_" $width "." $extension) -}}
36+
{{- $srcset = $srcset | append (printf "%s %dw" .RelPermalink $width) -}}
3937
{{- end -}}
40-
{{- $srcset = $srcset | append (printf "%s %dw" $image.RelPermalink $image.Width) -}}
41-
{{- printf "<img src=\"%s\" alt=\"\" loading=\"%s\" srcset=\"%s\" sizes=\"(max-width: 576px) calc(100vw - 3.2rem), (max-width: 768px) 540px, (max-width: 992px) 720px, (max-width: 1200px) 960px, (max-width: 1400px) 1140px, 1320px\">" $image.RelPermalink $loading (delimit $srcset ", ") | safeHTML -}}
42-
</picture>
38+
{{- end -}}
39+
{{- $srcset = $srcset | append (printf "%s %dw" $image.RelPermalink $image.Width) -}}
40+
41+
{{- $styles := "" -}}
42+
{{- if and $image.Width -}}
43+
{{- $styles = printf "aspect-ratio: %d / %d" $image.Width $image.Height -}}
44+
{{- end -}}
45+
46+
<img
47+
src="{{ $image.RelPermalink }}"
48+
alt=""
49+
loading="{{ $loading }}"
50+
srcset="{{ delimit $srcset ", " }}"
51+
sizes="(max-width: 576px) calc(100vw - 3.2rem), (max-width: 768px) 540px, (max-width: 992px) 720px, (max-width: 1200px) 960px, (max-width: 1400px) 1140px, 1320px"
52+
style="{{ $styles | safeCSS }}"
53+
>

0 commit comments

Comments
 (0)