Demo
When to use it
- Photography and travel posts, where readers want a photo larger than the post column allows.
- Tutorials with screenshots of settings, where the small text is hard to read at column width.
- Charts, maps and infographics whose labels are too small to read in the post.
When not to: For galleries readers page through, a lightbox with next and previous buttons serves better: see How to add an image lightbox in Ghost.
The code
.bt-zoom,
.bt-zoom-dialog {
--bt-zoom-backdrop: rgb(0 0 0 / 0.9);
--bt-zoom-gap: 24px;
--bt-zoom-focus: var(--ghost-accent-color, #4433ff);
}
.bt-zoom {
position: relative;
display: block;
width: fit-content;
max-width: 100%;
margin: 0 auto;
padding: 0;
border: 0;
border-radius: 0;
background: none;
color: inherit;
font: inherit;
cursor: zoom-in;
}
.kg-gallery-image > .bt-zoom {
width: 100%;
height: 100%;
}
.bt-zoom > img {
display: block;
}
.bt-zoom-badge {
position: absolute;
top: 8px;
right: 8px;
display: grid;
place-items: center;
width: 36px;
height: 36px;
border-radius: 50%;
background: rgb(0 0 0 / 0.65);
color: #fff;
opacity: 0;
transition: opacity 0.2s ease;
}
.bt-zoom:hover .bt-zoom-badge,
.bt-zoom:focus-visible .bt-zoom-badge {
opacity: 1;
}
@media (hover: none) {
.bt-zoom-badge {
opacity: 1;
}
}
.bt-zoom:focus-visible {
outline: 3px solid var(--bt-zoom-focus);
outline-offset: 3px;
}
.bt-zoom-dialog {
box-sizing: border-box;
width: 100vw;
max-width: none;
height: 100dvh;
max-height: none;
margin: 0;
padding: var(--bt-zoom-gap);
border: 0;
background: var(--bt-zoom-backdrop);
overflow: hidden;
}
.bt-zoom-dialog[open] {
display: grid;
place-items: center;
animation: bt-zoom-fade 0.2s ease-out;
}
.bt-zoom-dialog::backdrop {
background: transparent;
}
.bt-zoom-image {
display: block;
width: auto;
max-width: 100%;
height: auto;
max-height: 100%;
object-fit: contain;
cursor: zoom-out;
animation: bt-zoom-grow 0.2s ease-out;
}
.bt-zoom-close {
position: fixed;
top: 16px;
right: 16px;
display: grid;
place-items: center;
width: 44px;
height: 44px;
margin: 0;
padding: 0;
border: 0;
border-radius: 50%;
background: rgb(255 255 255 / 0.15);
color: #fff;
cursor: pointer;
}
.bt-zoom-close:focus-visible {
outline: 3px solid #fff;
outline-offset: 2px;
}
:root:has(.bt-zoom-dialog[open]) {
overflow: hidden;
}
@keyframes bt-zoom-fade {
from { opacity: 0; }
}
@keyframes bt-zoom-grow {
from { transform: scale(0.94); }
}
@media (prefers-reduced-motion: reduce) {
.bt-zoom-badge {
transition: none;
}
.bt-zoom-dialog[open],
.bt-zoom-image {
animation: none;
}
}(() => {
// Ghost's image cards and gallery cards. Linked images are left as links.
const IMAGES = '.kg-image-card img, .kg-gallery-image img';
const ICON_ZOOM = '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true" focusable="false"><circle cx="10" cy="10" r="7"/><path d="m21 21-6-6M10 7v6M7 10h6"/></svg>';
const ICON_CLOSE = '<svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" aria-hidden="true" focusable="false"><path d="M18 6 6 18M6 6l12 12"/></svg>';
function init() {
const images = [...document.querySelectorAll(IMAGES)].filter((img) => !img.closest('a, button'));
if (!images.length) return;
const dialog = document.createElement('dialog');
dialog.className = 'bt-zoom-dialog';
dialog.setAttribute('aria-label', 'Zoomed image');
dialog.innerHTML = `<img class="bt-zoom-image" alt=""><button class="bt-zoom-close" type="button" aria-label="Close">${ICON_CLOSE}</button>`;
const big = dialog.querySelector('.bt-zoom-image');
let opener = null;
images.forEach((img) => {
const button = document.createElement('button');
button.type = 'button';
button.className = 'bt-zoom';
button.setAttribute('aria-label', img.alt ? `Zoom image: ${img.alt}` : 'Zoom image');
img.replaceWith(button);
button.append(img);
button.insertAdjacentHTML('beforeend', `<span class="bt-zoom-badge">${ICON_ZOOM}</span>`);
button.addEventListener('click', () => {
opener = button;
// Let the browser pick the largest size Ghost made of the image.
big.sizes = '100vw';
big.srcset = img.srcset;
big.src = img.currentSrc || img.src;
big.alt = img.alt;
dialog.showModal();
});
});
// Any click closes it: on the image, around it or on the close button. Esc closes it too.
dialog.addEventListener('click', () => dialog.close());
dialog.addEventListener('close', () => {
if (opener) opener.focus({ preventScroll: true });
});
document.body.append(dialog);
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
else init();
})();Add it to your site
- Copy the CSS and paste it in Settings › Advanced › Code injection › Site headerSettings › Advanced › Code injection › Site header (opens your Ghost Admin in a new tab), between
<style>and</style>. Once per site. - Copy the JS and paste it in Settings › Advanced › Code injection › Site footerSettings › Advanced › Code injection › Site footer (opens your Ghost Admin in a new tab), between
<script>and</script>. Save. - Open any post on your site to see it: it works on every post, with nothing to add to them.
How it works
The script finds the images in your posts’ image cards and gallery cards and makes each one a button, with a small magnifier that shows on hover, on keyboard focus and always on touch screens. The zoomed image opens in the browser’s own dialog element, so focus moves into it, and goes back to the image the reader came from when it closes.
Ghost makes several sizes of every image you upload. The zoomed view lets the browser pick the size that fits the screen, so photos stay sharp; an image smaller than the screen opens at its own size rather than blown up. The fade is turned off for readers who prefer reduced motion.
Things to know
- Linked images: images you’ve given a link in the editor stay links and don’t zoom.
- Your theme may do this already: Ghost has no zoom or lightbox of its own, but many themes, several of ours among them, open gallery images in a lightbox. If yours does, change the
IMAGESline to'.kg-image-card img'so the two don’t clash. - Other images: to zoom images outside Ghost’s cards, add their selector to the
IMAGESline.
For a lightbox with next and previous buttons for galleries, see How to add an image lightbox in Ghost.
Customize
| Variable | Default | What it changes |
|---|---|---|
--bt-zoom-backdrop | rgb(0 0 0 / 0.9) | The screen behind the zoomed image. |
--bt-zoom-gap | 24px | The space kept around the zoomed image. |
--bt-zoom-focus | var(--ghost-accent-color, #4433ff) | The focus ring on an image when readers move to it with the keyboard. Defaults to your site's accent color. |