Skip to main content

Carbon theme for MkDocs

Project description

mkdocs-carbon

PyPI - Version PyPI - Python Version PyPI - Downloads

Carbon Design System theme for mkdocs.

What's New

v2.0 - Modernization 🎉

Phase 1 & 2: Layout & Navigation

  • Three-column responsive layout - Navigation, content, and table of contents
  • Sticky TOC sidebar - Always visible with automatic scroll highlighting
  • Navigation state persistence - Remembers expanded/collapsed menu items
  • Previous/Next navigation - Footer links for easy page navigation
  • Improved mobile experience - Touch-optimized with slide-out drawer
  • Optimized content width - Maximum 900px for comfortable reading

Phase 3: Interactive Features

  • Back to top button - Floating button appears after scrolling
  • Copy to clipboard - One-click code copying with visual feedback
  • Enhanced admonitions - Icons for all admonition types
  • Keyboard shortcuts - Press / to focus search
  • Smooth scrolling - Enhanced anchor navigation

Phase 4: Polish & Optimization

  • Carbon type scale - Complete IBM typography system
  • 100+ CSS variables - Full theming control
  • Reduced motion support - Respects accessibility preferences
  • Enhanced code blocks - Better styling and syntax highlighting
  • Performance optimized - Efficient CSS and JavaScript

Previous Releases

  • v1.3 Support for Accordion component & edit link in header
  • v1.2 Support for Header Navigation Menu
  • v1.1 Support for Search
  • v1.0 Initial Release

Examples

Installation

python -m pip install mkdocs-carbon

Usage

theme:
  name: carbon
  prefix: Durera
  theme_toggle: true
  header_nav_items:
    - title: View on Github
      url: https://github.com/durera/mkdocs-carbon
      active: true
    - title: View on PyPi
      url: https://pypi.org/project/mkdocs-carbon/
      target: _new

markdown_extensions:
  - toc:
      permalink: true

Theme Switcher

The theme now includes a built-in theme switcher in the header that allows users to toggle between Carbon's four theme zones:

  • Light (white) - Pure white background
  • Light Gray (g10) - Light gray background
  • Dark Gray (g90) - Dark gray background (default)
  • Dark (g100) - Pure dark background

The default theme is Dark Gray (g90), which provides excellent readability in most lighting conditions. The selected theme is automatically saved to localStorage and persists across page loads. Users can click the moon icon in the header to access the theme menu.

To disable the theme switcher, set theme_toggle: false in your mkdocs.yml.

Features

Modern Three-Column Layout

  • Left Sidebar: Collapsible navigation with state persistence
  • Center Content: Optimized reading width (max 900px) with proper spacing
  • Right Sidebar: Sticky table of contents with automatic scroll highlighting

Responsive Design

  • Desktop (≥1280px): Full three-column layout
  • Tablet (768px-1279px): Two-column layout (nav + content, TOC hidden)
  • Mobile (<768px): Single column with slide-out navigation drawer

Navigation Features

  • State Persistence: Navigation menu remembers expanded/collapsed items using localStorage
  • Active Page Highlighting: Current page is clearly marked in navigation
  • Smooth Transitions: Elegant expand/collapse animations
  • Mobile-Friendly: Touch-optimized with swipe-to-close drawer

Table of Contents

  • Sticky Positioning: Always visible while scrolling on desktop
  • Scroll Spy: Automatically highlights current section
  • Nested Headings: Supports H1, H2, H3, and H4 levels
  • Smooth Scrolling: Click any heading for smooth navigation

Navigation Footer

  • Previous/Next Links: Easy navigation between pages
  • Automatic Generation: Built from your nav structure
  • Responsive Layout: Stacks vertically on mobile

Enhanced User Experience

  • Keyboard Shortcuts: Press / to focus search
  • Smooth Scrolling: All anchor links scroll smoothly
  • Print-Friendly: Optimized print stylesheet
  • Accessibility: ARIA labels and semantic HTML

Carbon Design Integration

  • Design Tokens: Uses Carbon spacing, typography, and color scales
  • IBM Plex Fonts: IBM Plex Sans and Mono included
  • Theme Zones: Support for white, g10, g90, and g100 themes
  • Carbon Components: Full integration with Carbon web components

Theme Configuration

Prefix

The default prefix is Carbon, this is what appears before the Site Title in the header

Carbon Theme Selection

Easily switch between Carbon themes using theme_sidenav and theme_header, they can be set to white, g10, g90, or g100, by default the header uses g100, and the side navigation g90.

alt text alt text

Header Navigation Menu

The header navigation menu can be enabled by defining header_nav_items as a list of objects with url and title. Optionally control where the links open using target, or set a navigation item as active by adding active set to true.

alt text

Layout Customization

The modern layout is automatically enabled and responsive. Customize the theme using CSS variables in your extra_css:

Layout Dimensions

:root {
    --sidebar-width: 256px;        /* Left navigation width */
    --content-max-width: 900px;    /* Maximum content width */
    --toc-width: 256px;            /* Right TOC width */
    --header-height: 48px;         /* Header height */
}

Colors

:root {
    --link-color: #0f62fe;         /* Primary link color */
    --link-hover-color: #0043ce;   /* Link hover color */
    --text-primary: #161616;       /* Primary text color */
    --text-secondary: #525252;     /* Secondary text color */
    --background: #ffffff;         /* Page background */
    --border-subtle: #e0e0e0;      /* Border color */
}

Typography

:root {
    --font-size-03: 1rem;          /* Body text size */
    --font-size-07: 2rem;          /* H2 heading size */
    --line-height-normal: 1.5;     /* Body line height */
    --font-weight-light: 300;      /* Light font weight */
}

Spacing

:root {
    --spacing-05: 1rem;            /* Standard spacing unit */
    --spacing-06: 1.5rem;          /* Medium spacing */
    --spacing-07: 2rem;            /* Large spacing */
}

Advanced Customization

Custom Theme Colors

Create a custom theme by overriding Carbon theme variables:

[data-carbon-theme="custom"] {
    --text-primary: #1a1a1a;
    --background: #fafafa;
    --layer-01: #f0f0f0;
    --link-color: #0066cc;
}

Disable Features

Hide specific features using CSS:

/* Hide back to top button */
.md-back-to-top {
    display: none !important;
}

/* Hide copy buttons on code blocks */
.md-code-copy {
    display: none !important;
}

/* Hide navigation footer */
.md-footer-nav {
    display: none !important;
}

Custom Breakpoints

Adjust responsive breakpoints:

/* Custom tablet breakpoint */
@media (max-width: 1024px) {
    .md-sidebar--secondary {
        display: none;
    }
}

Optional Page Metadata

Additional Breadcrumb Entries

The following metdata are supported, when set they will extend the breadcrumbs built from the nav structure by adding up to two extra entries before the final entry in the breadcrumb:

  • extra_breadcrumb_title_1
  • extra_breadcrumb_url_1
  • extra_breadcrumb_title_2
  • extra_breadcrumb_url_2

It's possible to only set the title for one or both of the entries if you don't want the breadcrumb element to take the user anywhere.

Associate Orphaned Page with Nav

An orphaned page can be connected to the navigation structure by setting the nav_title metadata to the title of the navigation item it should be connected to.

Fonts

Fonts are packaged in the theme itself:

Supported Carbon Components

These can be introduced as HTML inside markdown documents to bring Carbon components to your content.

Breadcrumb

<cds-breadcrumb no-trailing-slash>
  <cds-breadcrumb-item>
    <cds-breadcrumb-link href="{{ nav.homepage.url | url }}">Home</cds-breadcrumb-link>
  </cds-breadcrumb-item>
</cds-breadcrumb>

Select

<cds-select id="breadcrumbs-toc" class="cds-theme-zone-g10" inline placeholder="..." oninput="changeAnchor(this.value)">
  {% set h1 = page.toc | first %}
  {% for toc_item in h1.children %}
    <cds-select-item value="{{ toc_item.url }}">{{ toc_item.title }}</cds-select-item>
  {% endfor %}
</cds-select>

Tabs

<cds-tabs trigger-content="Select an item" value="2024">
  <cds-tab id="tab-2024" target="panel-2024" value="2024">2024 Catalogs</cds-tab>
  <cds-tab id="tab-2023" target="panel-2023" value="2023">2023 Catalogs</cds-tab>
  <cds-tab id="tab-2022" target="panel-2022" value="2022">2022 Catalogs</cds-tab>
</cds-tabs>

<div class="tab-panel">
  <div id="panel-2024" role="tabpanel" aria-labelledby="tab-2024" hidden>
    Tab 1 content here
  </div>
  <div id="panel-2023" role="tabpanel" aria-labelledby="tab-2023" hidden>
    Tab 2 content here
  </div>
  <div id="panel-2022" role="tabpanel" aria-labelledby="tab-2022" hidden>
    Tab 3c ontent here
  </div>
</div>

Search

<cds-search id="header-search" label-text="Search" cds-search-input="search-event" expandable></cds-search>

Accordion

When using the accordion component, make sure to enclose the accordion object inside a div, otherwise mkdocs will mess up the generated HTML.

<div>
<cds-accordion>
  <cds-accordion-item title="Section 1 title">
    <p>Lorem ipsum odor amet, consectetuer adipiscing elit. Torquent sapien natoque volutpat lobortis mollis diam. Dictumst nibh tristique aliquet blandit suspendisse maecenas commodo class. Maecenas tincidunt ultrices elementum etiam ipsum at. Blandit habitasse ultricies dapibus volutpat eu porttitor pharetra? Posuere velit maecenas blandit praesent semper donec tristique natoque. Sapien sapien lobortis neque praesent morbi hendrerit. Diam arcu adipiscing himenaeos accumsan cras. Viverra pulvinar sodales torquent habitasse amet penatibus gravida.</p>
  </cds-accordion-item>
  <cds-accordion-item title="Section 2 title">
    <p>Lectus dui ridiculus mauris tempus; vivamus dignissim accumsan montes. Donec taciti vitae tincidunt faucibus hac mattis ante pretium. Taciti eros metus sapien urna eleifend ridiculus sagittis. Ridiculus conubia ligula parturient ullamcorper condimentum posuere porttitor. Dignissim urna laoreet conubia cubilia scelerisque cubilia aliquet inceptos aliquam. Senectus ultricies posuere eu facilisis pulvinar dignissim.</p>
  </cds-accordion-item>
  <cds-accordion-item title="Section 3 title">
    <p>Integer interdum at praesent congue semper maecenas platea. Bibendum facilisis eros potenti et egestas potenti curabitur. Mi blandit lacus aptent nullam, eros sagittis rhoncus vestibulum. Litora sapien ultricies vivamus facilisi varius erat ut. Luctus pretium massa dis cursus fusce purus montes molestie facilisi. Cras non mi suspendisse lobortis habitant sem malesuada feugiat est. Blandit natoque commodo sem eget curae porta facilisis sociosqu.</p>
  </cds-accordion-item>
</cds-accordion>
</div>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mkdocs_carbon-2.0.0.tar.gz (176.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mkdocs_carbon-2.0.0-py2.py3-none-any.whl (180.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file mkdocs_carbon-2.0.0.tar.gz.

File metadata

  • Download URL: mkdocs_carbon-2.0.0.tar.gz
  • Upload date:
  • Size: 176.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for mkdocs_carbon-2.0.0.tar.gz
Algorithm Hash digest
SHA256 5a8a87d3bc6085fa171c5758e370640d7137671521c7d782c97881d60ed244d1
MD5 dc33f56e47b83f30b4c78a5fed4828fa
BLAKE2b-256 ad82a7513d2257f1dae1f89286ef2804500bc71b6a1571a881abc376cb177c4f

See more details on using hashes here.

File details

Details for the file mkdocs_carbon-2.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: mkdocs_carbon-2.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 180.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for mkdocs_carbon-2.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1662a61a00fb8431a8e025f1da81e1942f5420f2b2955555994d2756bd86f728
MD5 5b02604d52e1f12630de9cf08131d4a5
BLAKE2b-256 0b3fea66388baa2aea88767c63d5fa712b167ee0d8a68dd5fd66262753fc22fb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page