GUIDE / FUNDAMENTALS

Layout

Arrange elements with <column>, <row> and flex, share free space, push items apart, wrap grids and scroll.

Core janelkit Included with janelkit. Nothing extra to install.
CHAPTER 12 / 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 <column>, <row> and flex place their children along a main axis.
  • The default behavior of each, so layouts are predictable.
  • How flex shares free space and how <spacer> and margins push items apart.
  • How to wrap a grid and when to scroll.

Three boxes that arrange their children

A layout answers two questions: where each element goes, and how much space it takes. <column> stacks its children from top to bottom. <row> places them from left to right. flex, lowercase, is the general box behind both: a row by default, configurable with direction and the full set of flexbox properties.

The direction a box places its children is its main axis: vertical in a <column>, horizontal in a <row>. The other direction is the cross axis. gap separates children along the main axis; padding separates them from the box edge.

janel
<window title="Boxes" width={460} height={300}>
  <column padding={24} gap={12}>
    <text>A Column stacks its children.</text>
    <textfield placeholder="Fields fill the width" label="Example field" />
    <button>Buttons keep their size</button>
    <row gap={8}>
      <text>A Row centers its children vertically:</text>
      <button>One</button>
      <button>Two</button>
    </row>
  </column>
</window>
Copy

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

What you should see

The text and the field stretch across the window; the button keeps its natural width. In the last line, the text and two buttons sit side by side, vertically centered.

Know the defaults

Most layout surprises come from defaults. In a <column>, <text>, <textfield>, <textarea>, <column>, <row> and <scroll> take the full width, like blocks, while buttons and checkboxes keep their natural size. In a <row>, children are centered vertically. In both, only children with flex shrink when space is short. flex follows web defaults instead: a row, children stretched across, and every child allowed to shrink down to its content.

Leftover space is not spread between children: it stays at the end unless justify-content says otherwise. The root element fills the whole window.

PropertyQuestion it answersExample
gapHow far apart are siblings?gap: 12px
paddingHow far from the box edge?padding: 24px
justify-contentWhere along the main axis?start, center, end, space-between, space-around, space-evenly
align-itemsWhere along the cross axis?start, center, end, stretch, baseline
align-selfOverride the alignment of one child?align-self: stretch
flexHow much free space does a child take?flex: 1
marginSpace outside one child; auto pushes it away.margin-left: auto

Share free space with flex

flex={1} on a child, or flex: 1 in a rule, gives it the free space along the main axis: height in a <column>, width in a <row>. Several flexible children share it in proportion, regardless of their content: flex 2 and flex 1 get two thirds and one third.

The container needs a size that comes from outside, such as filling the window or being stretched. A box that only wraps its content has no free space to share. A fixed width for a sidebar and flex: 1 for the main area is the most common desktop layout.

janel
<window title="Workspace" width={700} height={420}>
  <row class="workspace">
    <column class="sidebar">
      <text>Navigation</text>
    </column>
    <column class="main">
      <text>Your content goes here.</text>
    </column>
  </row>
</window>

<style>
.workspace { gap: 16px; padding: 20px; }
.sidebar { width: 180px; align-self: stretch; background: var(--surface-alt); padding: 16px; }
.main { flex: 1; align-self: stretch; padding: 16px; }
</style>
Copy

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

How it works

  1. The <row> is the root, so it fills the window and has free width to share.
  2. The sidebar asks for 180 pixels; align-self: stretch makes it as tall as the <row>.
  3. flex: 1 gives the main area all the remaining width.

What you should see

Resizing the window keeps the sidebar at 180 pixels while the main area grows and shrinks.

Push items apart with <spacer> or margin

<spacer> takes the free space in its container and pushes its siblings to the ends. It is the simplest way to build a toolbar with actions on both sides. margin-left: auto on one child does the same from a style rule.

janel
<window title="Toolbar" width={520} height={160}>
  <column padding={16} gap={12}>
    <row gap={8}>
      <button>New</button>
      <button>Open</button>
      <spacer />
      <button variant="primary">Save</button>
    </row>
  </column>
</window>
Copy

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

What you should see

New and Open sit on the left; Save sits at the right edge, whatever the window width.

Wrap a grid without Python

flex-wrap: wrap lets children flow onto new lines when they do not fit. With flex: 1 1 200px each card starts at 200 pixels and grows to fill its line; the grid reflows as the window is resized, with no code.

janel
<script lang="python">
cards = [f"Card {n}" for n in range(1, 9)]
</script>

<window title="Grid" width={640} height={360}>
  <flex class="grid">
    {#each cards as card}
      <column class="card"><text>{card}</text></column>
    {/each}
  </flex>
</window>

<style>
.grid { flex-wrap: wrap; gap: 12px; padding: 16px; align-items: start; }
.card { flex: 1 1 200px; padding: 16px; background: var(--surface); border-radius: var(--radius); }
</style>
Copy

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

What you should see

Eight cards arranged in rows of about three. Narrowing the window moves cards to new lines.

<scroll>, and sizes

<scroll> is a <column> that scrolls vertically when its content is taller than the space it has; by default it takes the free space of its container. It creates all its content, which is right for settings pages and modest lists. For hundreds or thousands of rows use <list>, which only creates the visible rows.

width and height set a preferred size in pixels; 100% takes the available space and other percentages are relative to the container. min-width, max-width, min-height and max-height limit how far an element grows or shrinks. A preferred size is a request, not a guarantee that content fits every window.

Common mistakes

  • flex: 1 has no effect when the container only wraps its content: there is no free space to share. Give the container a size, flex or align-self: stretch.
  • Do not nest a <scroll> inside a box that grows with its content; the <scroll> then never needs to scroll.
  • Spacing between children belongs in gap on the container, not in margins on every child.

Learn moreLarge lists ↗Styles and themes ↗

Key points

  • <column> stacks, <row> lines up, flex is the general box; gap and padding set spacing.
  • flex shares free space; <spacer> and margin auto push items apart.
  • flex-wrap builds grids; <scroll> scrolls, <list> handles very long lists.

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