Skip to content

CSS theming

A theme-aware SVG export has no fixed colors on its shapes. Every bond, label, charge, and annotation carries one or more .mk-* classes, and one scoped stylesheet inside the file supplies the light palette, the dark palette, and the rules that switch between them. You can restyle the whole drawing from your page with ordinary CSS.

Structural classes mark the parts of the drawing. Their rules have no !important, so any host selector can override them.

Class Targets
mk-bond Bond line strokes, including projection bonds and Newman circles
mk-dash Hashed wedge lines; the dash pattern stays as an attribute
mk-wedge Solid wedge fills
mk-wedge-outline Hollow wedge outlines
mk-double-x Crossed double-bond marks
mk-cap Rounded bond end caps
mk-vertex Vertex dots where bonds meet at implicit carbons
mk-aromatic Aromatic ring circles
mk-deloc-arc Delocalization arcs
mk-lp Lone-pair dots and bars
mk-radical Radical dots
mk-charge-label Charge text and the plus and minus strokes inside circled charges
mk-charge-circle The ring around circled charges
mk-halo The page-colored halo behind atom labels
mk-underlay Page-colored masks that break crossing bonds
mk-fo-text HTML annotation text inside foreignObject blocks (uses color, not fill)
mk-bg The background rectangle, when the background is exported
mk-newman-bg The page-colored circle fill in Newman projections
mk-dflt-fill, mk-dflt-stroke Fallback classes for shapes drawn in the default color

Color classes sit on top of the structural classes:

  • Element classes. Atom label text gets a class named after its element symbol (mk-C, mk-O, mk-N, and so on) as well as mk-atom. The atom’s charges, lone pairs, and radicals inherit the element class. Rules are written only for elements present in the drawing.
  • Element symbols inside rich labels. In animated exports, element-colored symbols in rich labels (spans in a foreignObject) also get the mk-<Element> class, so one .mk-O override recolors both. See Control animated SVGs.
  • Custom colors. A color that matches no color-scheme entry gets a class with a unique suffix, such as mk-c0-k3v9, and a dark counterpart that Molkit derives from it. The suffix keeps the classes of several inlined SVGs on one page from colliding.

!important policy. Element and custom color rules carry !important; structural rules do not. Several inlined SVGs share one page cascade, and without the flag a later file’s generic text rule could override an earlier file’s element colors. To override an element color, your rule needs !important too; see override examples.

Every rule is scoped to the export’s random root id, but the id is wrapped in :where(), which adds zero specificity. An exported rule for .mk-bond therefore has the specificity of a bare .mk-bond, and any host selector with higher specificity wins. An excerpt:

inside the exported <style> block
/* light */
:where(#molkit-a1b2c) .mk-bond { stroke: #000000; }
:where(#molkit-a1b2c) .mk-halo { fill: var(--mk-page-bg, #ffffff); }
:where(#molkit-a1b2c) .mk-O, :where(#molkit-a1b2c) text.mk-O { fill: #ff0000 !important; color: #ff0000 !important; stroke: none; }
/* dark, inside @media (prefers-color-scheme: dark) */
:where(#molkit-a1b2c:not(.mk-light, .mk-light *)) .mk-bond { stroke: #e0e0e0; }
:where(#molkit-a1b2c:not(.mk-light, .mk-light *)) .mk-O, :where(#molkit-a1b2c:not(.mk-light, .mk-light *)) text.mk-O { fill: #ef4444 !important; color: #ef4444 !important; stroke: none; }

The light values come from the color scheme that was active at export time (see color schemes). Gradient bonds keep their gradients; in dark mode, attribute selectors remap the stop colors.

Dark mode in a theme-aware export has three states.

State How to get it How it works
Follow the operating system (default) Nothing; this also works in an img tag The dark palette is inside a prefers-color-scheme: dark media query
Force dark Add the class mk-dark to the inlined SVG or any ancestor The dark rules are written a second time under an .mk-dark ancestor scope, after the media query, so they win at equal specificity
Force light Add the class mk-light to the SVG or any ancestor The media query’s scope selector excludes .mk-light ancestors, so the export stays light on a dark system

To make embedded drawings follow your site’s own theme toggle instead of the operating system, set these classes from the toggle:

site-theme-toggle.html
<button id="theme-toggle">Toggle theme</button>
<div class="content">
<!-- inlined theme-aware Molkit SVGs anywhere below documentElement -->
</div>
<script>
const root = document.documentElement;
let dark = window.matchMedia('(prefers-color-scheme: dark)').matches;
function applyTheme() {
root.classList.toggle('mk-dark', dark);
root.classList.toggle('mk-light', !dark);
// ...flip your site's own theme class here too
}
document.getElementById('theme-toggle').addEventListener('click', () => {
dark = !dark;
applyTheme();
});
applyTheme();
</script>

Animated exports have the same three hooks. SVGs exported before Molkit 0.19.0 may respond only to the media query; re-export them if you need the classes. For playback, see Control animated SVGs.

Recolor all bonds. Structural classes have no !important, so one wrapper class adds enough specificity. Include the bond classes that use fill:

brand-bonds.css
.figure .mk-bond, .figure .mk-dash, .figure .mk-wedge-outline { stroke: #1e3a5f; }
.figure .mk-wedge { fill: #1e3a5f; stroke: #1e3a5f; }
.figure .mk-cap, .figure .mk-vertex, .figure .mk-double-x { fill: #1e3a5f; }

Recolor one element across the site. Element rules use !important, so your rule must as well, and it must match both the text and the stroke variants:

orange-oxygen.css
svg .mk-O, svg text.mk-O { fill: #d97706 !important; color: #d97706 !important; }
svg line.mk-O, svg path.mk-O { stroke: #d97706 !important; }

The export’s dark rules also use !important. To keep the override in forced dark mode, add an .mk-dark variant of your rule.

Match the page background. Halos and underlays use --mk-page-bg, which defaults to white in light mode and near-black in dark mode. In dark mode the background rectangle is transparent. If your dark page uses another background color, set the variable so label halos do not show as off-color rectangles:

page-bg.css
.mk-dark svg { --mk-page-bg: #111827; }

Theme-aware CSS controls color. Text rendering is a separate choice with four options; see font embedding for details.

Option The host page must provide Text selectable
Default: a Google Fonts @import is added for the non-local web fonts used Network access to the font CDN Yes
Embed fonts for offline use: font files inlined as data URIs Nothing Yes
Mobile-safe rendering: subsets of Liberation Sans, Serif, and Mono embedded as fallbacks with the metrics of Arial, Times, and Courier Nothing Yes
Convert labels to paths: labels become vector outlines Nothing No

Every export ends with an id-scoped print block. It draws bonds in solid black with a heavier stroke and hides the background rectangle, so drawings print legibly from any page that embeds them.