Markdown Styling
Customizing the look and feel of your Hugo Markdown content, including adding specific colors, can be achieved through several techniques. The best method depends on whether you want to style an entire element globally, a specific instance, or just inline text.
Here are the four primary ways to custom style your Hugo markdown posts.
1. Using CSS and Global Styling (Global/Structural)
This is the standard approach for styling the overall structure of your posts. You write CSS rules that target the standard HTML elements that Markdown renders to (e.g., h1
, p
, blockquote
, pre
).
How to Implement:
- Identify the HTML Tag: Look at your rendered Markdown. A paragraph is
<p>
, a heading is<h1>
, a blockquote is<blockquote>
, and so on. - Add Custom CSS: Add the styling to your main CSS file (usually in
assets/css/
orstatic/css/
).
Example CSS:
To make all blockquotes in your posts have a blue background and a yellow border:
/* Target the content wrapper, then the blockquote element */
.post-content blockquote {
background-color: #e6f7ff; /* Light blue background */
border-left: 5px solid #007bff; /* Blue left border */
color: #333; /* Dark text color */
padding: 10px 15px;
margin: 20px 0;
}
Where to Put the CSS:
Ensure your main page template (layouts/_default/single.html
) links to this CSS file, often using an approach like this in the <head>
:
2. Using Markdown Attributes (Block-Level Elements)
Hugo's default Markdown renderer, Goldmark, supports Markdown Attributes. This allows you to add custom class
or id
attributes (and inline style
attributes) to certain block-level elements directly in your Markdown content.
A. Configuration (One-Time Setup)
You must enable block-level attributes in your configuration file (config.toml
, config.yaml
, or config.json
).
In config.toml
:
[markup]
[markup.goldmark]
[markup.goldmark.parser]
[markup.goldmark.parser.attribute]
block = true # Crucial for paragraphs, blockquotes, etc.
B. Usage in Markdown
Once enabled, you can add custom styling to the preceding block.
Element | Markdown Code | Output |
---|---|---|
Paragraph | This is a styled paragraph. {.red-text} |
<p class="red-text">...</p> |
Heading | # My Red Heading {#main-title .red-text} |
<h1 id="main-title" class="red-text">...</h1> |
Blockquote | > This is a colorful alert! {.alert-box style="background: yellow;"} |
<blockquote class="alert-box" style="background: yellow;">...</blockquote> |
Example:
You still need to define the .red-text
class in your main CSS file.
3. Creating Custom Shortcodes (For Reusable Elements)
Shortcodes are the most powerful and reusable way to insert custom styled HTML. They are ideal for colored text snippets, custom buttons, or info boxes.
A. Create the Shortcode File
Create a file named, for example, layouts/shortcodes/color.html
:
<span style="color: {{ .Get "color" }}; background-color: {{ .Get "bg" }}; padding: 2px 4px; border-radius: 3px;">
{{ .Inner }}
</span>
B. Use in Markdown
You can then wrap text directly in your Markdown file using the shortcode, passing in the desired colors as parameters:
This is normal text, but I want to highlight the
{{< color color="#8B0000" bg="#FFFACD" >}}most important part{{< /color >}}
of the sentence in a custom color.
Note: Use the {{< shortcode >}}
syntax when the inner content is plain text or HTML. Use {{% shortcode %}}
if the inner content itself needs to be processed as Markdown (e.g., if you include bold text or lists inside the shortcode).
4. Customizing Default Rendering with Render Hooks
If you want to change the HTML structure or add a specific class to every instance of a Markdown element globally (like all tables, all headings, or all images), use a Render Hook.
For example, to wrap all Markdown-generated tables in a special div
for responsive design:
- Create the Hook File:
layouts/_default/_markup/render-table.html
- Add the Template:
This is how you can achieve sophisticated, content-specific styling without writing custom HTML in every post.