Stylish diagrams with CSS
One diagram, four looks. Only the stylesheet changes.
The four slides on this page are the same Python code. Each one attaches a different stylesheet. The
diagram says what each element is (class_="service", class_="store", class_="async"); the stylesheet
decides what that looks like. Rebrand a whole deck by swapping one file.
The diagram
Classes name a role, never a colour. The legend is generated from those names, so it restyles itself too.
Switch looks above the picture and open the Python and CSS tabs: only the .css tab changes.
"""The diagram every look in this folder restyles: the code only says what each element *is*."""
from spinagram import Diagram
def platform(id: str, title: str, sheet: str) -> Diagram:
d = Diagram(id, title, subtitle=f"The same diagram, styled by {sheet}",
layout={"engine": "layered", "direction": "right", "rank_gap": 64, "route": "ortho"})
d.stylesheet(sheet)
with d.group("Event-driven shop", id="canvas", class_="canvas", direction="right"):
with d.group("Clients", id="clients", class_="tier"):
web = d.box("Web app", text="React", icon="terrastruct:react", class_="client")
mobile = d.box("Mobile", text="iOS, Android", icon="selfhosted:android", class_="client")
gateway = d.box("API gateway", text="auth, rate limits", icon="aws:api-gateway", class_="edge")
with d.group("Services", id="services", class_="tier"):
users = d.box("Accounts", text="Go", icon="terrastruct:go", class_="service")
orders = d.box("Orders", text="Python", icon="terrastruct:python", class_="service hero")
search = d.box("Search", text="Elasticsearch", icon="selfhosted:elasticsearch", class_="service")
with d.group("Data", id="data", class_="tier"):
db = d.box("Orders DB", text="PostgreSQL", icon="terrastruct:postgres", class_="store")
bus = d.box("Event bus", text="Kafka on MSK", icon="aws:amazon-managed-streamingfor-apache-kafka", class_="stream")
web >> gateway
mobile >> gateway
gateway >> [orders, users, search]
orders.to(db, "SQL")
orders.to(bus, "order.placed", class_="async")
bus.to(search, "reindex", class_="async")
d.add_legend({".client": "Client", ".service": "Service", ".store": "Data store", ".stream": "Event stream",
".hero": "Revenue critical",
"edge.async": "Async event"}, position="bottom")
return dMidnight
A dark canvas group, glass cards with deep shadows, and a neon outline per role. The hero service glows
with a box-shadow that has no offset.
"""The diagram every look in this folder restyles: the code only says what each element *is*."""
from spinagram import Diagram
def platform(id: str, title: str, sheet: str) -> Diagram:
d = Diagram(id, title, subtitle=f"The same diagram, styled by {sheet}",
layout={"engine": "layered", "direction": "right", "rank_gap": 64, "route": "ortho"})
d.stylesheet(sheet)
with d.group("Event-driven shop", id="canvas", class_="canvas", direction="right"):
with d.group("Clients", id="clients", class_="tier"):
web = d.box("Web app", text="React", icon="terrastruct:react", class_="client")
mobile = d.box("Mobile", text="iOS, Android", icon="selfhosted:android", class_="client")
gateway = d.box("API gateway", text="auth, rate limits", icon="aws:api-gateway", class_="edge")
with d.group("Services", id="services", class_="tier"):
users = d.box("Accounts", text="Go", icon="terrastruct:go", class_="service")
orders = d.box("Orders", text="Python", icon="terrastruct:python", class_="service hero")
search = d.box("Search", text="Elasticsearch", icon="selfhosted:elasticsearch", class_="service")
with d.group("Data", id="data", class_="tier"):
db = d.box("Orders DB", text="PostgreSQL", icon="terrastruct:postgres", class_="store")
bus = d.box("Event bus", text="Kafka on MSK", icon="aws:amazon-managed-streamingfor-apache-kafka", class_="stream")
web >> gateway
mobile >> gateway
gateway >> [orders, users, search]
orders.to(db, "SQL")
orders.to(bus, "order.placed", class_="async")
bus.to(search, "reindex", class_="async")
d.add_legend({".client": "Client", ".service": "Service", ".store": "Data store", ".stream": "Event stream",
".hero": "Revenue critical",
"edge.async": "Async event"}, position="bottom")
return dBlueprint
Square corners, white ink, dashed construction lines for data, and open arrowheads.
"""The diagram every look in this folder restyles: the code only says what each element *is*."""
from spinagram import Diagram
def platform(id: str, title: str, sheet: str) -> Diagram:
d = Diagram(id, title, subtitle=f"The same diagram, styled by {sheet}",
layout={"engine": "layered", "direction": "right", "rank_gap": 64, "route": "ortho"})
d.stylesheet(sheet)
with d.group("Event-driven shop", id="canvas", class_="canvas", direction="right"):
with d.group("Clients", id="clients", class_="tier"):
web = d.box("Web app", text="React", icon="terrastruct:react", class_="client")
mobile = d.box("Mobile", text="iOS, Android", icon="selfhosted:android", class_="client")
gateway = d.box("API gateway", text="auth, rate limits", icon="aws:api-gateway", class_="edge")
with d.group("Services", id="services", class_="tier"):
users = d.box("Accounts", text="Go", icon="terrastruct:go", class_="service")
orders = d.box("Orders", text="Python", icon="terrastruct:python", class_="service hero")
search = d.box("Search", text="Elasticsearch", icon="selfhosted:elasticsearch", class_="service")
with d.group("Data", id="data", class_="tier"):
db = d.box("Orders DB", text="PostgreSQL", icon="terrastruct:postgres", class_="store")
bus = d.box("Event bus", text="Kafka on MSK", icon="aws:amazon-managed-streamingfor-apache-kafka", class_="stream")
web >> gateway
mobile >> gateway
gateway >> [orders, users, search]
orders.to(db, "SQL")
orders.to(bus, "order.placed", class_="async")
bus.to(search, "reindex", class_="async")
d.add_legend({".client": "Client", ".service": "Service", ".store": "Data store", ".stream": "Event stream",
".hero": "Revenue critical",
"edge.async": "Async event"}, position="bottom")
return dPastel
No outlines: tinted fills per role, round corners and soft shadows. Good for product docs and onboarding.
"""The diagram every look in this folder restyles: the code only says what each element *is*."""
from spinagram import Diagram
def platform(id: str, title: str, sheet: str) -> Diagram:
d = Diagram(id, title, subtitle=f"The same diagram, styled by {sheet}",
layout={"engine": "layered", "direction": "right", "rank_gap": 64, "route": "ortho"})
d.stylesheet(sheet)
with d.group("Event-driven shop", id="canvas", class_="canvas", direction="right"):
with d.group("Clients", id="clients", class_="tier"):
web = d.box("Web app", text="React", icon="terrastruct:react", class_="client")
mobile = d.box("Mobile", text="iOS, Android", icon="selfhosted:android", class_="client")
gateway = d.box("API gateway", text="auth, rate limits", icon="aws:api-gateway", class_="edge")
with d.group("Services", id="services", class_="tier"):
users = d.box("Accounts", text="Go", icon="terrastruct:go", class_="service")
orders = d.box("Orders", text="Python", icon="terrastruct:python", class_="service hero")
search = d.box("Search", text="Elasticsearch", icon="selfhosted:elasticsearch", class_="service")
with d.group("Data", id="data", class_="tier"):
db = d.box("Orders DB", text="PostgreSQL", icon="terrastruct:postgres", class_="store")
bus = d.box("Event bus", text="Kafka on MSK", icon="aws:amazon-managed-streamingfor-apache-kafka", class_="stream")
web >> gateway
mobile >> gateway
gateway >> [orders, users, search]
orders.to(db, "SQL")
orders.to(bus, "order.placed", class_="async")
bus.to(search, "reindex", class_="async")
d.add_legend({".client": "Client", ".service": "Service", ".store": "Data store", ".stream": "Event stream",
".hero": "Revenue critical",
"edge.async": "Async event"}, position="bottom")
return dEditorial
Black and white for print, with one red accent for the revenue-critical path.
"""The diagram every look in this folder restyles: the code only says what each element *is*."""
from spinagram import Diagram
def platform(id: str, title: str, sheet: str) -> Diagram:
d = Diagram(id, title, subtitle=f"The same diagram, styled by {sheet}",
layout={"engine": "layered", "direction": "right", "rank_gap": 64, "route": "ortho"})
d.stylesheet(sheet)
with d.group("Event-driven shop", id="canvas", class_="canvas", direction="right"):
with d.group("Clients", id="clients", class_="tier"):
web = d.box("Web app", text="React", icon="terrastruct:react", class_="client")
mobile = d.box("Mobile", text="iOS, Android", icon="selfhosted:android", class_="client")
gateway = d.box("API gateway", text="auth, rate limits", icon="aws:api-gateway", class_="edge")
with d.group("Services", id="services", class_="tier"):
users = d.box("Accounts", text="Go", icon="terrastruct:go", class_="service")
orders = d.box("Orders", text="Python", icon="terrastruct:python", class_="service hero")
search = d.box("Search", text="Elasticsearch", icon="selfhosted:elasticsearch", class_="service")
with d.group("Data", id="data", class_="tier"):
db = d.box("Orders DB", text="PostgreSQL", icon="terrastruct:postgres", class_="store")
bus = d.box("Event bus", text="Kafka on MSK", icon="aws:amazon-managed-streamingfor-apache-kafka", class_="stream")
web >> gateway
mobile >> gateway
gateway >> [orders, users, search]
orders.to(db, "SQL")
orders.to(bus, "order.placed", class_="async")
bus.to(search, "reindex", class_="async")
d.add_legend({".client": "Client", ".service": "Service", ".store": "Data store", ".stream": "Event stream",
".hero": "Revenue critical",
"edge.async": "Async event"}, position="bottom")
return dTechniques worth stealing
| Effect | CSS |
|---|---|
| Dark canvas | Wrap the diagram in a group and style it: group.canvas { fill: #070B16; fill-opacity: 1 } |
| Glow | A shadow with no offset: box-shadow: 0 0 32px rgba(244, 114, 182, 0.55) |
| Lifted card | A soft offset shadow: box-shadow: 0 6px 14px rgba(15, 23, 42, 0.10) |
| Layout-only group | Hide it: group.bare { fill-opacity: 0; stroke-width: 0 } |
| Async events | edge.async { stroke-dasharray: 6 5; marker-end: open-arrow } |
| One palette | Custom properties: :root { --brand: #6D28D9 }, then stroke: var(--brand) |
| Style by context | group.cluster node::title { color: #F8FAFC } |
Edge label pills always use the canvas colour (white by default), so give labels a dark color even on a
dark canvas, as Blueprint does.
Work on a look live
spinagram serve examples/looks/Edit a .css file and every diagram that uses it re-renders. Press Inspect (or i) and click an element
to see which rule set each value, or ask on the command line:
spinagram check examples/looks/ --styles --select ".hero"Every property and how SVG, PowerPoint and Figma draw it: CSS styling · CSS by backend.