[Rx] Hugo | Image Path from Absolute to Relative

quick-icon Quick Fix


Tested on Roadster Theme.

This is the bare bones version to get up and running:

Example: /pages/about/_images/thumb01.png to _images/thumb01.png.

  1. Download/Copy render-image.html.txt into layouts/_default/_markup/ folder (.txt removed).
  2. Download/Copy post_thumbnail.html.txt into layouts/_default/_markup/ folder (.txt removed).
  3. Test hugo server -D. Verify with hugo --minify.

Gotchas:

  1. Did you rename rx-image-path-absolute-to-relative.md to index.md?
  2. Did you leave .html files in the folder? Add .txt.
  3. Clicked the links. Where are .txt files? Typically, in Downloads folder.

Rx-32x32 Notes


If the quick fix didn’t do it for you or you want the gritty details, these notes explain how to convert hardcoding full/absolute image paths in Hugo content and front matter to short, portable, relative paths.

For example, write _images/thumb01.png instead of /pages/about/_images/thumb01.png.

It covers both inline markdown images and front-matter thumbnail fields, since Hugo resolves these two cases differently and each needs its own fix.

Prerequisites:

  • Advanced copy-and-paste skills.
  • Content needs to be organized as leaf bundles. Each post/page is a folder containing an index.md plus its own images.
  • Theme you can override at project level. This means your project has its own layouts/ folder can contain a file at the same relative path as one in the theme, and Hugo will use your copy instead of the theme’s without you ever touching the theme’s files directly. This is how all standard Hugo themes work, so you’re good here. Maybe. Probably.

This is mostly copy and paste if you’re using the Roadster theme. Otherwise, you need to be comfortable enough reading someone else’s template code to adapt it, rather than just pasting a finished snippet.


1. Why Absolute Paths Are a Problem

If your markdown or front matter references images with a full path like:

![thumbnail](/posts/rx-my-post-slug/_images/thumb01.png)

that path breaks the moment you rename the post’s folder, move it to a different section, or fix a typo in the URL structure (e.g. /post/ vs /posts/) . Hugo won’t warn you. The image just silently 404s. It also means you can’t simply copy-paste an image into a post’s bundle folder and reference it by filename. You have to know and type its full eventual URL. Who wants to do that?

The fix is to treat images as Hugo “page resources” – files that live inside the same bundle folder as the content referencing them. Then, Hugo can resolve the actual path internally via .Resources.GetMatch, rather than hardcoding a URL string yourself.


2. Precondition: Your Content Must Be a Leaf Bundle

Page resources only work if the content file is a leaf bundle — meaning the post/page is its own folder, and the markdown file inside it is named exactly index.md:

content/posts/my-post-slug/
├── index.md
└── _images/
    └── thumb01.png

This will not work – a plain file, or a same-named file inside a folder:

content/posts/my-post-slug.md  ← not a bundle, no folder
content/posts/my-post-slug/my-post-slug.md   ← wrong filename, no leaf bundle

This will work – index.md file with images in _images folder:

content/posts/my-post-slug/index.md  ← works, _images folder needed for images

Check any post that isn’t behaving before assuming the template is broken:

find content/posts -iname "*my-post-slug*"

If you see a single .md file rather than a folder, convert it to a bundle first (create the folder, move the file in as index.md, move any images alongside it) before continuing.


3. Case A: Inline Markdown Images (![alt](path))

This is the simplest case – a Hugo render hook can intercept every markdown image site-wide and resolve it automatically, with zero change to how you actually write markdown. The is just copy-and-paste the block below to a new file (render-image.html) in a text editor and save to the layouts/_default/_markup/ folder. If _markup folder doesn’t exist, create it.

Create the render hook

layouts/_default/_markup/render-image.html:

{{- $image := .Page.Resources.GetMatch .Destination -}}
{{- if $image -}}
<img src="{{ $image.RelPermalink }}" alt="{{ .Text }}"{{ with .Title }} title="{{ . }}"{{ end }}>
{{- else -}}
<img src="{{ .Destination | safeURL }}" alt="{{ .Text }}"{{ with .Title }} title="{{ . }}"{{ end }}>
{{- end -}}

How it works:

  • Hugo automatically routes every markdown image through this template once it exists at this exact path — no registration or config needed.
  • .Destination is the path you wrote in the markdown (e.g. _images/thumb01.png).
  • .Page.Resources.GetMatch .Destination looks for a file at that relative path inside the current page’s own bundle folder. If found, .RelPermalink gives Hugo’s own correctly-computed root-relative URL.
  • If not found as a page resource (e.g. the image actually lives in static/ instead), the else branch falls back to rendering the path exactly as written — so nothing breaks for images that aren’t page resources.

What you write in markdown – no change needed

![thumbnail](_images/thumb01.png)

Just the relative filename. Paste an image into the post’s bundle folder, reference it by relative path, done — no absolute URLs, no remembering the post’s eventual full route.

Rebuild and test

hugo --minify

Check the actual generated HTML resolved to a real, working path:

grep -n "thumb01" public/posts/my-post-slug/index.html

4. Case B: Front-Matter Thumbnail Fields

But wait, there’s more! Render hooks only apply to markdown body content. They do not touch front-matter fields (like a thumbnail: value read by a post_thumbnail.html partial to build a list-page preview image). This needs a separate fix, directly in the partial that reads that field. In other words, you need to edit a file.

4a. Copy Theme post_thumbnail to Project Folder

Never edit inside themes/ directly:

Make a copy of the theme’s post_thumbnail.html and put it into the layouts/_partials project folder.

mkdir -p layouts/_partials     
cp themes/<your-theme>/layouts/_partials/post_thumbnail.html layouts/_partials/post_thumbnail.html

Then, edit the copy (layouts/_partials projects folder) with the corrected “after” version below.

4b. Replace Before with After

BEFORE: theme’s original html might look like this (actual Roaster html):

{{- if $thumbnail := .page.Params.thumbnail }}
	{{- $class := .class }}
	{{- $visibility := .page.Site.Params.thumbnail.visibility | default (slice "list" "post") }}

	{{- $valueType := printf "%T" $thumbnail -}}
	{{- $isMap := in $valueType "map" -}}
	{{- if $isMap }}
		{{ $visibility = default (slice "list" "post") (default .page.Site.Params.thumbnail.visibility $thumbnail.visibility) }}
		{{ $thumbnail = $thumbnail.src }}
	{{- end }}

	{{- if in $visibility $class }}
	<figure class="{{ with $class }}{{ . }}__thumbnail {{ end }}thumbnail">
		{{ if eq $class "list" }}<a class="thumbnail__link" href="{{ .page.RelPermalink }}">{{ end }}
		<img class="thumbnail__image" src="{{ $thumbnail | relURL }}" alt="{{ .page.Title }}">
		{{ if eq $class "list" }}</a>{{ end }}
	</figure>
	{{- end }}
{{- end }}

(19 lines)

AFTER: The modified version has the same structure, same classes (so existing CSS sizing still applies), only the src resolution logic changed:

{{- if $thumbnail := .page.Params.thumbnail }}
	{{- $class := .class }}
	{{- $page := .page }}
	{{- $visibility := .page.Site.Params.thumbnail.visibility | default (slice "list" "post") }}
	{{- $valueType := printf "%T" $thumbnail -}}
	{{- $isMap := in $valueType "map" -}}
	{{- if $isMap }}
		{{ $visibility = default (slice "list" "post") (default .page.Site.Params.thumbnail.visibility $thumbnail.visibility) }}
		{{ $thumbnail = $thumbnail.src }}
	{{- end }}
	{{- if in $visibility $class }}
	{{- $thumb := $page.Resources.GetMatch $thumbnail }}
	<figure class="{{ with $class }}{{ . }}__thumbnail {{ end }}thumbnail">
		{{ if eq $class "list" }}<a class="thumbnail__link" href="{{ .page.RelPermalink }}">{{ end }}
		{{ if $thumb }}
			<img class="thumbnail__image" src="{{ $thumb.RelPermalink }}" alt="{{ .page.Title }}">
		{{ else }}
			<img class="thumbnail__image" src="{{ $thumbnail | relURL }}" alt="{{ .page.Title }}">
		{{ end }}
		{{ if eq $class "list" }}</a>{{ end }}
	</figure>
	{{- end }}
{{- end }}

(23 lines)

5. Rebuild and Verify Everything

hugo --minify

Important: verify with a real build, not hugo server. If you’re running hugo server -D (Hugo’s live-reload dev server) and you add a brand-new image file to an existing post’s bundle folder while the server is already running, Hugo’s file watcher does not always detect it. In other words, if something looks broke, don’t panic (yet).

If a thumbnail appears broken only under hugo server -D right after you added the image, fully stop and restart the server before concluding anything is wrong:

# Ctrl+C to stop, then:
hugo server -D

Better yet, confirm against a real production build, which always re-scans everything from disk with no caching ambiguity:

rm -rf public
hugo --minify

If any post has a front-matter shape your partial doesn’t yet account for, Hugo’s build will fail immediately with a specific template error naming the exact file and line — e.g.:

execute of template failed: ... executing "post_thumbnail.html" ... can't evaluate field src in type string

That specific error means: some posts have thumbnail: as a plain string, but the partial assumed every post used the nested-object shape (or vice versa).

Once the build succeeds, scan for any other front-matter shape you might have missed across your whole content folder before considering this done:

grep -B1 -A2 "^thumbnail:" content/posts/*/index.md content/posts/*.md 2>/dev/null | grep -A2 "^thumbnail:$"

Empty output confirms there’s no third variant lurking that would break later.

Finally, visually confirm on both a list page and a single-post page that images render at the correct size (not native/oversized). If they suddenly render huge, it usually means a wrapping <figure>/class was accidentally dropped during the edit, and the CSS that sizes .thumbnail__image (or similar) no longer has anything to target.


6. If Cut-and-Paste Didn’t Work

I’m not an expert on Hugo’s templating system or even Hugo in general. I’m more of a learn-as-I-need-it-and-then-forget-it kind of guy (purpose of this blog).

Someone who just installed a random theme (not Roadster) and writes markdown posts, with no template-editing experience, would likely get stuck and need 2-3 days to figure it out (my story).

So, if this is your situation, google “Hugo Templates” or fire up Claude as a shortcut.

Good luck!

Square3_thumb