Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Custom Styling

You do not need to replace the entire mdBook theme to tweak the look. A small custom CSS file loaded via additional-css is often all you need.

Setup

Create a theme/ directory next to your src/ folder and add a CSS file:

mkdir theme
touch theme/custom.css

Then reference it in book.toml:

[output.html]
additional-css = ["./theme/custom.css"]

Example customisations

Here is a small theme/custom.css that makes a few tasteful tweaks without fighting the default theme:

/* Slightly wider content area */
:root {
    --content-max-width: 850px;
}

/* Softer sidebar font size */
.sidebar {
    font-size: 0.9em;
}

/* Accent underline on h1 headings */
.content main h1 {
    border-bottom: 2px solid var(--sidebar-active);
    padding-bottom: 0.3em;
}

/* Rounded code blocks */
.content main pre {
    border-radius: 6px;
}

/* Make inline code stand out */
.content main code {
    padding: 0.1em 0.35em;
    border-radius: 3px;
}

Useful CSS variables

mdBook exposes CSS custom properties you can override. A few handy ones:

VariableDescription
--content-max-widthMaximum width of the main content area
--sidebar-widthWidth of the navigation sidebar
--sidebar-bgSidebar background colour
--sidebar-activeColour of the active sidebar link
--linksColour of hyperlinks
--inline-code-colorColour of inline code spans

These variables change per theme (Light, Rust, Coal, Navy, Ayu), so your overrides automatically adapt to the reader’s chosen theme.

Going further

If CSS overrides are not enough, you can replace individual template files. Copy them from the mdBook source into your theme/ directory:

mdbook init --theme   # dumps all default theme files into theme/

You can then edit theme/index.hbs (the page template), theme/header.hbs, or any of the CSS files. mdBook will use your local copies instead of the built-in ones.

Tip: Only copy the files you actually want to change. Keeping the rest at their defaults means you automatically benefit from upstream improvements when you upgrade mdBook.