Skip to content

Commit b8b3ad5

Browse files
committed
images: wrap images in a lightbox
Wrap images in a lightbox, increasing them to full-screen size when clicked. There are JavaScript solutions available (e.g. lightbox2)[^0], though I opted for a purely CSS-based approach.[^1] [^0]: https://lokeshdhakar.com/projects/lightbox2/ [^1]: https://codesalad.dev/blog/how-to-create-an-image-lightbox-in-pure-css-25 Signed-off-by: Luca Zeuch <[email protected]>
1 parent 4da89cb commit b8b3ad5

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

assets/scss/components/_images.scss

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,40 @@ figcaption {
2525
margin-top: 0.5rem;
2626
font-style: italic;
2727
}
28+
29+
/* CSS-only lightbox. See https://codesalad.dev/blog/how-to-create-an-image-lightbox-in-pure-css-25 */
30+
.lightbox {
31+
/* Default to hidden */
32+
display: none;
33+
34+
/* Overlay entire screen */
35+
position: fixed;
36+
z-index: 99999;
37+
top: 0;
38+
left: 0;
39+
right: 0;
40+
bottom: 0;
41+
42+
/* A bit of padding around image */
43+
padding: 1em;
44+
45+
/* Translucent background */
46+
background: rgba(0, 0, 0, 1);
47+
}
48+
49+
/* Unhide the lightbox when it's the target */
50+
.lightbox:target {
51+
display: block;
52+
}
53+
54+
.lightbox span {
55+
/* Full width and height */
56+
display: block;
57+
width: 100%;
58+
height: 100%;
59+
60+
/* Size and position background image */
61+
background-position: center;
62+
background-repeat: no-repeat;
63+
background-size: contain;
64+
}

layouts/_default/_markup/render-image.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,28 @@
108108
{{- end }}
109109
{{- end }}
110110

111+
{{- $lightbox := true }}
112+
111113
{{- /* Render image element. */ -}}
114+
{{- if $lightbox -}}
115+
<a href="#{{ print $id "-lightbox" }}">
116+
{{- end }}
112117
<img
113118
{{- range $k, $v := $attrs }}
114119
{{- if or $v (eq $k "alt") }}
115120
{{- printf " %s=%q" $k $v | safeHTMLAttr }}
116121
{{- end }}
117122
{{- end -}}
118123
>
124+
{{- if $lightbox -}}
125+
</a>
126+
<a href="javascript:history.back();" class="lightbox" id="{{ print $id "-lightbox" }}">
127+
<img
128+
{{- range $k, $v := $attrs }}
129+
{{- if or $v (eq $k "alt") }}
130+
{{- printf " %s=%q" $k $v | safeHTMLAttr }}
131+
{{- end }}
132+
{{- end -}}
133+
>
134+
</a>
135+
{{- end }}

0 commit comments

Comments
 (0)