Skip to content
Bright Themes

Image zoom for Ghost

Readers can open the images in your posts and galleries full screen, with a mouse, a keyboard or a tap, and close them with Esc. No library to load.

  • Works with any Ghost theme

New to code injection?

It adds your own CSS and scripts to every page of your site, from Ghost Admin, without editing the theme.

Demo

Choose an image, or Tab to it and press Enter, to see it full screen. Choose anywhere or press Esc to close it.

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;
  }
}

Add it to your site

  1. 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.
  2. 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.
  3. 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 IMAGES line 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 IMAGES line.

For a lightbox with next and previous buttons for galleries, see How to add an image lightbox in Ghost.

Customize

VariableDefaultWhat it changes
--bt-zoom-backdroprgb(0 0 0 / 0.9)The screen behind the zoomed image.
--bt-zoom-gap24pxThe space kept around the zoomed image.
--bt-zoom-focusvar(--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.
Runs on every post, with no changes to your theme. Something not right? Ask us.

All widgets

Launch a better Ghost site

From a solo newsletter to a busy newsroom, there is a theme that fits. No code needed.