v2.0.11

Theming

Last updated


All NanoMD styles are scoped to the .nanomd container; this page covers how the styles load, the dark mode attributes and the CSS variables you can override.

Loading


CaseStyle source
<script src=".../parser.js">parser.css from the same directory is inserted into <head> automatically
ESMparser.css is loaded from the same directory via import.meta.url
A bundler moves the filesimport "@pardnchiu/nanomd/parser.css" yourself
<script src=".../parser.js" data-nanomd-css="off">Nothing is inserted; use it when the page already inlines or links parser.css

Skip the Automatic Stylesheet


A second copy of parser.js with data-nanomd-css="off" adds no stylesheet; the same copy without it adds one.
  1. <script>
  2. const count = () => document.querySelectorAll('link[rel="stylesheet"][href$="parser.css"]').length;
  3. const load = (off) => new Promise((resolve) => {
  4. const s = document.createElement("script");
  5. s.src = document.querySelector('script[src$="parser.js"]').src;
  6. if (off) s.dataset.nanomdCss = "off";
  7. s.onload = resolve;
  8. document.head.appendChild(s);
  9. });
  10. console.log("loaded once:", count());
  11. load(true)
  12. .then(() => console.log("with data-nanomd-css=off:", count()))
  13. .then(() => load(false))
  14. .then(() => console.log("without it:", count()));
  15. </script>
Live preview · @pardnchiu/nanomd@2.0.11

Container Attributes


AttributeEffect
class="nanomd"Applies all styles; put the output of html() inside this container
data-mode="dark"Forces dark
data-mode="light"Forces light; without it the system prefers-color-scheme applies
data-fill="1"Positions the container absolutely to fill the nearest positioned ancestor

CSS Variables


.nanomd defines these variables on itself, so values set on an ancestor are replaced; override them on the .nanomd element, with an inline style or a selector at least as specific as .nanomd[data-mode="dark"] so the override also holds in dark mode:
VariableUsed for
--text, --text-color, --inkBody text, alert text, bold and table text
--accentLinks, blockquote border, footnote references
--gray, --gray-2, --gray-3Striped table rows and code span background, borders, secondary text
--code-bg, --code-hoverCode block background, hover background
--code-success, --code-errorCode background after a successful or failed copy
--copy-bgCopy button background
--mark==mark== background
--alert-note, --alert-tip, --alert-important, --alert-warning, --alert-cautionColors of the five alerts

  1. <div id="out" class="nanomd" data-mode="dark" style="--accent:#e11d48;--mark:#f59e0b;padding:16px">
  2. </div>
  3. <script>
  4. const out = document.getElementById("out");
  5. out.innerHTML = MDParser.Parse("> [!TIP]\n> Dark mode with a custom [accent](https://nanomd.pardn.io) and ==mark==.").html();
  6. addEventListener("load", () => console.log(getComputedStyle(out.querySelector(".md-link")).color));
  7. </script>
Live preview · @pardnchiu/nanomd@2.0.11
中文