-
Apologies but what is probably a basic question but I am having a hard time getting my custom CSS for the Mermaid.js support to take.
extra_css:
- stylesheets/admonitions.css
- stylesheets/extra.css
- stylesheets/icons.css
- stylesheets/mermaid.css
Looking at the inspector I even tried adding things like: #__mermaid_0 .note {
stroke: #0c1e58 !important;
fill: #0c1e58 !important;
} Even with that I was unsuccessful. Any advice would be greatly appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
Thanks for asking. Unfortunately, you cannot override Mermaid's CSS with additional CSS, because Mermaid.js diagrams must be encapsulated in shadow DOMs due to duplicate identifiers which lead to undefined behavior. If you wish to add or change some of the CSS Material for MkDocs applies to Mermaid.js, you would need to make your changes here and rebuild the theme. I recommend reading #2170 and mermaid-js/mermaid#1318 to understand the current design limitations. |
Beta Was this translation helpful? Give feedback.
-
Thank you! So it might be a better solution (if required) to generate the Mermaid chart outside of mkdocs as a SVG with the custom CSS and then import that image? |
Beta Was this translation helpful? Give feedback.
-
Depending on your needs, Mermaid's built-in styling features may be enough for your use case since they support changing stroke and fill colors. This is what I am referring to in the official docs, please scroll down to the "Styling links" section. I have a working example of that: "mermaid And the result on the page (using Material for Mkdocs), scroll all the way down. |
Beta Was this translation helpful? Give feedback.
-
For those who stubbled upon this problem, here's my approach. // Helper function to safely get CSS variables
const getCSSVariable = (variable: string, fallback: string): string => {
if (typeof window !== 'undefined' && typeof document !== 'undefined') {
return getComputedStyle(document.documentElement).getPropertyValue(variable).trim() || fallback;
}
return fallback;
}; And initialise using mermaid.initialize({
startOnLoad: false,
securityLevel: 'loose', // Allow clicks in the rendered diagram
logLevel: 'error', // Only show errors, not warnings
theme: 'forest', // Set the theme to forest
themeVariables: {
primaryColor: getCSSVariable('--primary', '#BB2528'),
primaryTextColor: getCSSVariable('--primary-foreground', '#fff'),
primaryBorderColor: getCSSVariable('--primary', '#7C0000'),
lineColor: getCSSVariable('--accent', '#F8B229'),
secondaryColor: getCSSVariable('--secondary', '#006100'),
tertiaryColor: getCSSVariable('--background', '#fff'),
edgeLabelBackground: getCSSVariable('--background', '#fff'),
edgeLabelColor: getCSSVariable('--foreground', '#000')
}
}); Essentially I created a helper function to get CSS variable on runtime and replace it on initialisation. |
Beta Was this translation helpful? Give feedback.
Thanks for asking. Unfortunately, you cannot override Mermaid's CSS with additional CSS, because Mermaid.js diagrams must be encapsulated in shadow DOMs due to duplicate identifiers which lead to undefined behavior. If you wish to add or change some of the CSS Material for MkDocs applies to Mermaid.js, you would need to make your changes here and rebuild the theme.
I recommend reading #2170 and mermaid-js/mermaid#1318 to understand the current design limitations.