Skip to content

Dark mode support for HTML output - #11831

Open
clarfonthey wants to merge 1 commit into
jgm:mainfrom
clarfonthey:dark-mode
Open

Dark mode support for HTML output#11831
clarfonthey wants to merge 1 commit into
jgm:mainfrom
clarfonthey:dark-mode

Conversation

@clarfonthey

@clarfonthey clarfonthey commented Aug 26, 2026

Copy link
Copy Markdown

Originally jgm/djot#409, before I found out this was part of pandoc.

Essentially, the goal here is to add a "just works" dark mode support to pandoc's HTML generation while being minimally intrusive and not breaking custom schemes. It consists of the following changes to the styles.html template:

  • quotecolor and quotebordercolor are added as these are the only two unique colors in the stylesheet that weren't already configurable. They affect the styling of blockquote tags.
  • Each of the various *color variables for the HTML generation now has a *colordark friend, which controls the color in dark mode.
  • Padding is emitted for monobackgroundcolor if monobackgroundcolor or monobackgroundcolordark is provided, although the provided color will only work in the respective mode. (it uses inherit otherwise)
  • color-scheme is emitted to either allow both dark and light mode by default, or restrict to only light or dark mode depending on what variables the user defines. (see below)

Part of being minimally intrusive is only enabling light and dark mode in the following cases:

  1. No colors have been overridden
  2. The user has explicitly overridden colors for both schemes

However, because pandoc's template conditions can only check one variable at a time, only the fontcolor and backgroundcolor (and their *dark versions) variables is checked to determine whether the user has overrided the colors, and thus, which schemes they would like to be enabled. This means that, in general, if a user has changed backgroundcolor or fontcolor only, that means they will default to only light mode, and if a user has changed backgroundcolordark or fontcolordark only, it will default to only dark mode.

There are a few other caveats, but these have been added to MANUAL.txt and hopefully are clear.


Since the light-dark coloring is a "new" standard (around 2024, 88% coverage), it includes explicit fallbacks for light mode. However, as part of implementing this change, currentColor was added to a few color definitions as opposed to repeating the font color, since this has been supported since 2010 at 98% availability.

That said, I have no idea what pandoc wishes to support browser-wise, and am only taking a conservative approach out of caution. I don't mind removing the fallbacks if you'd prefer.

Comment thread MANUAL.txt
Comment on lines +2954 to +2957
: sets the CSS `font-family` property on `code` and `pre` elements.

`monobackgroundcolor`
: sets the CSS `background-color` property on `code` elements
`monobackgroundcolor`, `monobackgroundcolordark`
: sets the CSS `background-color` property on `code` and `pre` elements

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: affecting pre also is not a new change, just documenting existing behavior.

@jgm jgm left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made some suggestions on simplifying the CSS template. (Not exhaustive but you should get the idea.) As noted, leave the man pages unchanged for now. As for browser support, if the CSS can be simpler I'd accept 98% support for post-2010 browsers, I think.

Comment thread data/templates/styles.html Outdated
Comment thread data/templates/styles.html Outdated
Comment thread data/templates/styles.html Outdated
Comment thread data/templates/styles.html Outdated
Comment thread data/templates/styles.html
Comment thread pandoc-cli/man/pandoc.1 Outdated
@clarfonthey

clarfonthey commented Aug 28, 2026

Copy link
Copy Markdown
Author

Okay, removed the man page changes and updated the template per your suggestions. In some cases this is definitely clearer, in others it looks a bit messier to me, but it at least matches your suggestions. The only weird case is with the monobackgroundcolor and linkcolor cases, which I think is the style you'd prefer, but let me know if that's not the case.

color: light-dark($if(fontcolor)$$fontcolor$$else$#1a1a1a$endif$, $if(fontcolordark)$$fontcolordark$$else$#fdfdfd$endif$);
background-color: $if(backgroundcolor)$$backgroundcolor$$else$#1a1a1a$endif$;
background-color: light-dark($if(backgroundcolor)$$backgroundcolor$$else$#1a1a1a$endif$, $if(backgroundcolordark)$$backgroundcolordark$$else$#fdfdfd$endif$);
color-scheme:$if(fontcolor)$ light$elseif(backgroundcolor)$ light$elseif(fontcolordark)$$elseif(backgroundcolordark)$$else$ light$endif$$if(fontcolordark)$ dark$elseif(backgroundcolordark)$ dark$elseif(fontcolor)$$elseif(backgroundcolor)$$else$ dark$endif$;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way the logic works out, this should always output at least one of the two.

Comment on lines +64 to +68
$if(linkcolor)$
color: light-dark($linkcolor$, $if(linkcolordark)$$linkcolordark$$else$inherit$endif$);
$elseif(linkcolordark)$
color: light-dark(inherit, $linkcolordark$);
$endif$

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These avoid outputting a redundant light-dark(inherit, inherit).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants