Fix dark mode code block

How to get code to render in dark mode using docsy theme.

If you’re using Docsy as your Hugo theme, your code blocks might not render correctly in dark mode.

To fix this quickly, create a CSS rule that sets the background-color for <pre> elements that are children of the .highlight class.

Here’s one way to do this:

  1. In your assets/scss folder, create (or edit) _style_project.scss.

  2. Declare the following rule:

    /* Fix dark mode code rendering */
    .highlight pre {
      background-color: var(--td-pre-bg) !important;
    }
    

    Here, --td-pre-bg evaluates to the background color value defined by your theme.

  3. Save your changes and rebuild your project.

Your code blocks should now be more readable in dark mode.

To learn more about _styles_project.scss, see Project style files (Docsy)

Vital statistics

  • Last tested with:

    • Hugo v0.0125.5+extended
    • Docsy v0.10.0
  • 16 May 2024: First post