GUIDE / FUNDAMENTALS

Styles and themes

Write style rules, apply classes conditionally, react to hover and focus, and use theme colors that switch between light and dark.

Core janelkit Included with janelkit. Nothing extra to install.
CHAPTER 13 / 29~4 min read · try examples at your own pace
Alpha / 0.2 — These pages document JanelKit 0.2 and its packages. Each series (0.1, 0.2…) may change the public API, always with migration notes: see Changes and migration. The website uses Svelte; the desktop examples use Python and JanelKit, not JavaScript.

What you will learn

  • How a style rule selects elements and which properties exist.
  • How to apply classes from Python data.
  • How states such as hover and focus change appearance without Python.
  • How theme tokens and the theme API make colors consistent and switchable.

A rule selects elements and sets properties

The style section holds rules. A rule has a selector, which says which elements it applies to, followed by braces with property: value; declarations. .page matches elements whose class includes page. Values are numbers in pixels (16px or 16), colors (#rrggbb, #aarrggbb, names, rgb() and rgba()) or theme tokens such as var(--primary).

JanelKit is not a browser. It supports a list of properties and translates them to Qt Quick. An unsupported property or value produces a warning with its location and is ignored. The check happens when a rule is applied to an element, so a rule that matches nothing is never checked.

janel
<window title="A little style" width={440} height={260}>
  <column class="page">
    <text class="heading">A readable interface</text>
    <text class="muted">Padding gives content room to breathe.</text>
    <button variant="primary">Continue</button>
  </column>
</window>

<style>
.page { padding: 24px; gap: 16px; }
.heading { font-size: var(--font-size-xl); font-weight: 600; }
.muted { color: var(--text-muted); }
</style>
Copy

Complete component · Save as src/App.janel in a scaffolded project.

How it works

  1. class="page" connects the <column> to the .page rule. The dot belongs only in the rule.
  2. var(--font-size-xl) and var(--text-muted) are theme tokens: named values from the active theme.
  3. variant="primary" is a <button> property, not a style rule; <button> also has default, danger and ghost.

What you should see

A large title, a softer line of text and an accent button, stacked with even spacing.

Apply classes from your data

class="card selected" applies two classes. class:selected={condition} applies selected only while the condition is true. class={…} accepts a string, a list (which may be nested) or a dictionary of class: condition. Each form updates when the data it reads changes.

A conditional class (class:name) with no matching rule produces a compile warning. A misspelled name in class="…" or class={…} does not, so if a style seems to have no effect, check the spelling on both sides.

janel
<script lang="python">
from janelkit import state
chosen = state(False)
</script>

<window title="Classes" width={400} height={240}>
  <column padding={24} gap={12}>
    <column class="card" class:chosen={chosen}>
      <text>This card follows the checkbox</text>
    </column>
    <checkbox bind:checked={chosen}>Choose the card</checkbox>
  </column>
</window>

<style>
.card { padding: 16px; border: 1px solid var(--border); border-radius: var(--radius); }
.chosen { border-color: var(--primary); background: var(--surface-alt); }
</style>
Copy

Complete component · Save as src/App.janel in a scaffolded project.

What you should see

The card has a thin border. Ticking the checkbox gives it the accent border and a different background; unticking removes them.

Selectors, scope and which rule wins

Rules are local: they apply to elements written in this component’s template, including content it passes into a child’s slot, never to the inside of another component. When several rules match, the more specific one wins, counting classes and pseudo-classes first, then elements; with equal specificity, the later rule wins. Attributes written on the element, such as gap={4}, beat plain rules; rules with a state, a class: or a class={…} beat attributes.

SelectorMatches
.titleElements with class title.
<button>Every <button> in this component (also flex).
.a, .bElements with class a, and elements with class b.
.button.primaryElements with both classes.
.card .titleA .title anywhere inside a .card.
.menu > .itemAn .item that is a direct child of a .menu.
.row:hoverA .row while the pointer is over it.
css
/* Inside the <style> section of a component. */
.card { padding: 16px; border: 1px solid var(--border); }
.card:hover { border-color: var(--primary); }
.card .subtitle { color: var(--text-muted); }
.card > .title { font-size: 18px; }
Copy

How it works

  1. The first rule styles every .card; the second only while the pointer is over one.
  2. .card .subtitle matches a subtitle at any depth inside a card; .card > .title only a title that is a direct child.

Change appearance on hover, press and focus

A pseudo-class applies a rule while an element is in a state. States are handled by the view, so hovering never calls Python. Add transition to animate the change. On a box with on:click, a :hover rule replaces the default highlight and an outline rule replaces the default focus ring.

Pseudo-classApplies while
:hoverthe pointer is over the element or one of its children
:pressed (or :active)it is being pressed; on a clickable box, also with Space
:focusit has keyboard focus
:focus-visibleit has focus that arrived from the keyboard
:focus-withinit or something inside it has focus
:disabledenabled={False}
:checked, :selected, :expandedthe attribute is true
:first-child, :last-childit is the first or last visible child of its container
janel
<script lang="python">
from janelkit import state
opened = state(0)
</script>

<window title="States" width={400} height={240}>
  <column padding={24} gap={12}>
    <row class="tile" on:click={opened += 1} label="Open the tile">
      <text>Hover or click this tile</text>
    </row>
    <text>Opened {opened} times</text>
  </column>
</window>

<style>
.tile { padding: 16px; border-radius: var(--radius); background: var(--surface); transition: background 150ms; }
.tile:hover { background: var(--surface-alt); }
.tile:pressed { background: var(--pressed); }
</style>
Copy

Complete component · Save as src/App.janel in a scaffolded project.

What you should see

The tile changes color smoothly under the pointer and darkens while pressed. Each click, or Space after reaching it with Tab, increases the count.

Inline styles for computed values

style="gap: 4px; background: #222" styles one element with the same properties as a rule. style={{"width": f"{percent}%", "background": color}} takes values computed in Python and updates when they change. Inline styles beat rules and states, so a :hover rule cannot override them.

janel
<!-- percent is a number state. -->
<flex style={{"width": f"{percent}%", "background": "var(--primary)", "height": 8}} />
Copy

Fragment · Add it to an existing component; it is not a complete file.

Common mistakes

  • If a :hover rule seems ignored, check whether the same property is set in style= on the element.

Theme tokens and the theme API

A token is a named design value from the active theme. var(--primary) means "the accent color of the current theme". When the theme changes, every token updates, so colors stay consistent across the application. JanelKit has a light theme, used by default, and a dark one.

The theme object switches and customizes themes. theme.name is reactive, so the template can show it.

TokensUse
--bg, --surface, --surface-altBackgrounds: window, cards and fields, secondary areas
--text, --text-mutedMain and secondary text
--borderBorders
--primary, --on-primaryAccent color and text on it
--danger, --on-danger, --success, --warningStatus colors
--hover, --pressedTranslucent layers for interaction
--radius-sm, --radius, --radius-lgCorner radii (4, 8, 14)
--font-family, --font-size-sm, --font-size, --font-size-lg, --font-size-xlTypography (12, 14, 18, 24)
--space-1 … --space-6Spacing (4, 8, 12, 16, 24, 32)
janel
<script lang="python">
from janelkit import theme
</script>

<window title="Themes" width={420} height={240}>
  <column class="page">
    <text>Current theme: {theme.name}</text>
    <button on:click={theme.toggle()}>Switch light / dark</button>
  </column>
</window>

<style>
.page { padding: 24px; gap: 16px; background: var(--bg); }
</style>
Copy

Complete component · Save as src/App.janel in a scaffolded project.

How it works

  1. theme.toggle() switches between light and dark when the button is clicked.
  2. theme.set("dark") selects a theme by name; theme.extend(brand="#7048e8") adds or changes tokens; theme.define("sepia", base="light", bg="#f4ecd8") creates a new theme.
  3. Token names with underscores are written with hyphens in styles: brand_dark becomes var(--brand-dark).

What you should see

Current theme: light at first. The button switches the whole window to the dark palette and the text to Current theme: dark.

Stay inside the supported subset

Supported: colors and backgrounds, borders per side and radii per corner, padding and margin, gap, the flexbox properties, fonts and text properties, opacity, transform, box-shadow, outline, cursor, overflow, position (relative, absolute, fixed) with z-index, and transitions of color, background, border color, opacity, size, radius and transform. An unsupported property or value produces a warning and is ignored; an unsupported selector or an at-rule such as @media or @keyframes is a compile error. The Style reference lists every property, value and element.

Text properties such as color and font-size set on a box are inherited by the <text> elements inside it, even inside child components. A box can also define its own variables, --accent: var(--primary), read with var(--accent) by what it contains.

Common mistakes

  • Eight-digit hex colors are #aarrggbb (alpha first), not the browser’s #rrggbbaa.
  • A misspelled color name such as blu compiles, but the application then refuses to start. Prefer theme tokens over color names.
  • Do not remove focus outlines to make screenshots cleaner; visible focus is how keyboard users find their place.

Learn moreStyle reference ↗Accessibility and keyboard ↗

Key points

  • Rules select elements by class, element or state; they are local to the component.
  • class:x={condition} and class={…} apply classes from data.
  • Use theme tokens for colors and sizes so the whole app can switch themes.

English documentation based on the JanelKit source. For additional details, browse the source docs ↗.