Styling
Scoped stylesheets
A Haml template can have a stylesheet with the same name: PostMeta.haml and
PostMeta.css, or +page.haml and +page.css. Class names in that pair are
scoped, so .meta in one component never collides with .meta in another.
-# PostMeta.haml
%p.meta
%time{ datetime: $date }= $date
/* PostMeta.css */
.meta {
color: var(--muted);
font-size: 0.875rem;
}
A class written as .card in Haml is dropped from the output when the
stylesheet does not define it. There is no error. If a class is missing from
the page, check that the stylesheet exists and has the same name as the
template.
Global rules
Wrap a selector in :global(...) to leave it unscoped. The outermost layout’s
stylesheet is the place for rules that apply to the whole site:
:global(body) {
margin: 0 auto;
max-width: 42rem;
}
:global(a) {
color: var(--link);
}
Styling Markdown
Markdown is compiled on its own, so its elements carry no scoped classes. The
page wraps the content in an element it owns and reaches in with :global:
.body
= Rista::H[post]
.body :global(pre) {
padding: 1rem;
overflow-x: auto;
}
Light and dark
A static page cannot read a cookie and render a different theme, so the choice happens in the browser. Most of it needs no JavaScript.
The root element opts in to both schemes, and every colour is a variable with a light and a dark value:
:global(:root) {
color-scheme: light dark;
--background: light-dark(#eff1f5, #1e1e2e);
--text: light-dark(#4c4f69, #cdd6f4);
}
:global(:root[data-theme="light"]) {
color-scheme: light;
}
:global(:root[data-theme="dark"]) {
color-scheme: dark;
}
With only the first rule, the page follows the system setting. The toggle in
this site’s header sets data-theme on the root element and saves the choice
in localStorage.
A small inline script in the document head applies the saved value before the
first paint, so the page never flashes the other theme. The button is hidden
in the markup and shown by the script, so it does not appear when JavaScript
is off.
Images
An imported image is an object with src, width and height. Writing the
size into the markup reserves the space before the image loads:
:ruby
Cover = import("./cover.jpg")
%img{ src: Cover.src, width: Cover.width, height: Cover.height, alt: "" }
An imported SVG gives its URL as src in the same way.
Several sizes
A query on the import asks for resized copies. The image then has a srcset
that lists them, and the browser downloads the one that fits the screen:
:ruby
Cover = import("./cover.jpg?width=480,800,1200")
%img{ src: Cover.src, srcset: Cover.srcset, sizes: "(max-width: 42rem) 100vw, 42rem", width: Cover.width, height: Cover.height, alt: "" }
sizes says how wide the image is shown, which only the page’s CSS knows.
Cover.src is still the original file.
| Query | Effect |
|---|---|
width=480,800 | one copy per width |
format=webp | the copies, and src, in that format |
quality=80 | the quality of the copies |
For a smaller format with a fallback, import the file twice and put the
second in a <picture>:
:ruby
Cover = import("./cover.jpg?width=480,800,1200")
WebpCover = import("./cover.jpg?width=480,800,1200&format=webp")
%picture
%source{ type: "image/webp", srcset: WebpCover.srcset, sizes: "(max-width: 42rem) 100vw, 42rem" }
%img{ src: Cover.src, srcset: Cover.srcset, sizes: "(max-width: 42rem) 100vw, 42rem", width: Cover.width, height: Cover.height, alt: "" }
The covers of this site’s posts are built this way, in src/posts.rb. The
query works in import_glob too.