Search code examples
markdownentity-relationshiperdmermaid

Mermaid ER diagram styling


I want to draw an ER diagram with Mermaid. But I can't find out how to style the box to make it with different colors.

I know it's possible to set the theme color, but I can't do it to each box. I've tried different syntaxes and found some information on the offical documentation, but I can't figure out how to use them.official docs I want to make it like this:example


Solution

  • Here is an example (Mermaid Live link)

    Note, that in the live link, I put the init directly in the code rather than the config. This is because it makes it easier if I want to copy it somewhere else.

    %%{init: {
      "theme": "default",
      "themeCSS": [
        ".er.relationshipLabel { fill: black; }", 
        ".er.relationshipLabelBox { fill: white; }", 
        ".er.entityBox { fill: lightgray; }",
        "[id^=entity-some] .er.entityBox { fill: lightgreen;} ",
        "[id^=entity-mytable] .er.entityBox { fill: powderblue;} ",
        "[id^=entity-anothertable] .er.entityBox { fill: pink;} "
        ]
    }}%%
    

    The first three settings set the default css, the last three are specific to a table using a pattern.

    Note that when testing in Mermaid Live and maybe some other implementations, you need to prefix the table name/pattern with entity-, but in some other implementations (e.g. Gitlab), you would need to remove that prefix to get it to work.