Creating a Hugo theme involves understanding the Hugo static site generator, writing templates using Go templating, and organizing your files correctly. Below is a step-by-step guide to help you create a Hugo theme from scratch.
First, make sure you have Hugo installed on your machine. You can download it from the Hugo releases page or use a package manager like brew
on macOS:
brew install hugo
Create a new Hugo site if you don’t already have one:
hugo new site my-hugo-site
cd my-hugo-site
Within your Hugo site directory, create a new theme:
hugo new theme my-theme
This will create a new theme directory structure inside themes/my-theme
.
Here’s an overview of the essential directories and files:
my-theme/
├── archetypes/
│ └── default.md
├── layouts/
│ ├── _default/
│ │ ├── baseof.html
│ │ ├── list.html
│ │ └── single.html
│ ├── index.html
│ ├── partials/
│ │ ├── footer.html
│ │ ├── header.html
│ │ └── head.html
│ └── shortcodes/
├── static/
│ ├── css/
│ ├── js/
│ └── images/
├── theme.toml
└── README.md
layouts/_default/baseof.html
This is the base template that other templates can extend.
<!DOCTYPE html>
<html lang="en">
<head>
{{ partial "head.html" . }}
</head>
<body>
{{ partial "header.html" . }}
<main>
{{ block "main" . }}{{ end }}
</main>
{{ partial "footer.html" . }}
</body>
</html>
layouts/_default/single.html
This template is used for single content pages.
{{ define "main" }}
<article>
<h1>{{ .Title }}</h1>
{{ .Content }}
</article>
{{ end }}
layouts/_default/list.html
This template is used for list pages, like your blog homepage or category pages.
{{ define "main" }}
<h1>{{ .Title }}</h1>
<ul>
{{ range .Pages }}
<li>
<a href="{{ .Permalink }}">{{ .Title }}</a>
</li>
{{ end }}
</ul>
{{ end }}
layouts/partials/head.html
Include common <head>
elements.
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ .Title }}</title>
<link rel="stylesheet" href="{{ "css/styles.css" | relURL }}">
layouts/partials/header.html
Add your site header.
<header>
<nav>
<ul>
<li><a href="{{ "/" | relLangURL }}">Home</a></li>
<!-- Add more links as needed -->
</ul>
</nav>
</header>
layouts/partials/footer.html
Add your site footer.
<footer>
<p>© {{ now.Format "2006" }} Your Site Name</p>
</footer>
Place your CSS, JavaScript, and image files in the static/
directory.
Edit the theme.toml
file to include metadata about your theme:
name = "my-theme"
license = "MIT"
licenselink = "https://opensource.org/licenses/MIT"
author = "Your Name"
homepage = "https://example.com"
In the root of your Hugo site, edit config.toml
to use your theme:
theme = "my-theme"
Run the Hugo server to test your theme:
hugo server
Open your browser and navigate to http://localhost:1313
to see your theme in action.
Continue adding styles, scripts, and content types as needed. Customize your templates and partials to match your design requirements.
Creating a Hugo theme involves setting up a directory structure, writing templates using Go templating, and organizing static assets. By following this guide, you can create a professional and customizable Hugo theme tailored to your needs.