Flex
Flexible layout: direction, axes, spacing, translation and combination of components.
When to use
Flex arranges the immediate children of a row or column, aligns them, and sets their spacing. It is suitable for action bars, cards and areas with stretchable elements. Space is useful for individual elements with wrappers and dividers; Grid - for a proportional grid.
Flex creates one root container, without additional wrappers for each child. The content is transmitted through Svelte snippet children.
Axes, spacing and translation
orientation=horizontal/vertical specifies the direction and takes precedence over vertical. Without local configuration, vertical can be inherited from ConfigProvider.flex.
| Problem | Setting | Example |
|---|---|---|
| Select row or column | orientation, vertical |
Layout direction |
| Align immediate children | justify, align |
Two-axis alignment |
| Set distances | gap |
Spacing between elements |
| Allow multiple lines | wrap |
Line wrapping |
| Distribute free width | flex on child elements |
Free space distribution |
justify acts along the main axis, align - along the transverse one. When moving from row to column, their meaning is preserved relative to the axes, so the screen direction changes. Explicit alignment values make the result predictable.
gap accepts small/medium/large, px number or CSS string. Presets follow the general theme padding. wrap=true corresponds to wrap; nowrap and wrap-reverse are also available. Carrying requires available width and suitable child sizes.
Flex prop refers to the container itself as an element of the parent flexbox. To distribute its contents, set flex to the immediate children. Combination with components shows regular Card, Tag and Button without special adapters.
Root element and settings
component is the default div and can be a native tag or a Svelte component. Native container uses a semantic list; keep appropriate child tags, such as li for ul.
The native component receives children, class, style, dir and additional props. Pass them on to your real root. For bind:nativeElement, declare it $bindable nativeElement and bind it to the DOM via bind:this; without such forwarding ref Flex will remain undefined.
ConfigProvider.flex inherits class, style and vertical. The passed nested record replaces the parent one; local orientation/vertical and style take precedence. Explicit flex and custom gap are added after style. There are no separate semantic classNames/styles and no own named component design tokens.
A non-empty local prefixCls specifies the full CSS prefix. Without it, including when the line is empty, ConfigProvider.prefixCls with the suffix -flex is used, the default is ant-flex. The custom component receives the already resolved classes and must pass them to its root. Example ConfigProvider shows nested inheritance and independent adjacent scope.
Alignment and CSS order do not change the reading order and Tab. Maintain a meaningful sequence of interactive elements in the DOM. bind:nativeElement is available after mount and is cleared upon unmount.
Examples
Layout direction
Flex arranges immediate children into a row or column without creating a wrapper for each element.
<script lang="ts">
import { Flex } from '@woodger/antd';
let vertical = $state(false);
</script>
<style>
.tiles { margin-top: 16px; }
.tile {
flex: 1;
min-width: 0;
padding: 12px 4px;
border-radius: 4px;
background: var(--ant-color-primary-bg);
text-align: center;
}
</style>
<label><input type="checkbox" bind:checked={vertical} />{(" To column")}</label>
<div class="tiles"><Flex {vertical} gap="small">
{#each [1, 2, 3, 4] as number (number)}<div class="tile">{(number)}</div>{/each}
</Flex></div>
Two-axis alignment
justify controls the main axis, align controls the cross axis. The height of the container leaves room for comparison.
<script lang="ts">
import { Flex, type FlexAlign, type FlexJustify } from '@woodger/antd';
let justify = $state<FlexJustify>('center');
let align = $state<FlexAlign>('center');
</script>
<style>
.controls {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 12px;
margin-bottom: 16px;
}
label {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
.tile {
padding: 8px;
background: var(--ant-color-primary-bg);
}
</style>
<div class="controls">
<label>{("Main axis ")}<select
bind:value={justify}
>
<option
value="flex-start"
>
{("At the beginning")}
</option>
<option value="center">
{("In the center")}
</option>
<option value="space-between">
{("Between")}
</option>
<option value="space-around">
{("Around")}
</option>
</select></label>
<label>{("Cross axis ")}<select
bind:value={align}
>
<option
value="flex-start"
>
{("At the beginning")}
</option>
<option value="center">
{("In the center")}
</option>
<option value="flex-end">
{("At the end")}
</option>
<option value="stretch">
{("Stretch")}
</option>
</select></label>
</div>
<Flex
{justify}
{align}
gap="small"
style="
min-height: 140px;
border: 1px dashed var(--ant-color-border);
padding: 8px;
"
>
<div
class="tile"
>{("One")}</div><div
class="tile"
>{("Two")}</div><div
class="tile"
>{("Three")}</div>
</Flex>
Spacing between elements
Named intervals follow a theme; the number specifies the pixels. You can also pass a CSS string, such as 1rem.
<script lang="ts">
import { Button, Flex } from '@woodger/antd';
let preset = $state('small');
let customGap = $state(12);
</script>
<style>
label {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 8px;
margin-bottom: 12px;
}
</style>
<label>{("Interval type ")}<select
bind:value={preset}
><option
value="small"
>small</option><option
value="medium"
>medium</option><option
value="large"
>large</option><option
value="custom"
>{("Number")}</option></select></label>
{#if preset === 'custom'}
<label>{("Spacing in pixels ")}<input
type="range"
min="0"
max="40"
bind:value={customGap}
/><output>{customGap}</output></label>
{/if}
<Flex wrap gap={preset === 'custom' ? customGap : preset}>
<Button>
{("First")}
</Button><Button>
{("Second")}
</Button><Button>
{("Third")}
</Button>
</Flex>
Line breaks
wrap wraps elements when there is not enough width. Without wrapping, content can be scrolled within the display area.
<script lang="ts">
import { Button, Flex } from '@woodger/antd';
let wrap = $state(true);
</script>
<style>
.viewport {
max-width: 100%;
overflow-x: auto;
margin-top: 16px;
padding-bottom: 8px;
}
</style>
<label><input type="checkbox" bind:checked={wrap} />{(" Transfer elements")}</label>
<div
class="viewport"
role="region"
aria-label={("Flex transfer area")}
tabindex="0"
>
<Flex {wrap} gap="small">
{#each Array.from({ length: 10 }, (_, index) => index + 1) as number (number)}
<Button style="flex:none">{("Item ")}{(number)}</Button>
{/each}
</Flex>
</div>
Combination with components
Nested Flex collects the contents of the Card: the main text is stretched, and the actions are transferred to a narrow screen.
<script lang="ts">
import { Button, Card, Flex, Tag } from '@woodger/antd';
let saved = $state(false);
</script>
<Card title={("Project documentation")}>
<Flex vertical gap="medium">
<Flex align="center" justify="space-between" wrap gap="small">
<span>{("Examples of components")}</span><Tag
color="green"
>{("Ready")}</Tag>
</Flex>
<p>{("One card combines release information and actions.")}</p>
<Flex wrap gap="small">
<Button type="primary" onclick={() => { saved = !saved; }}>{(saved ? "Saved" : "Save")}</Button>
<Button href={("/contacts")}>{("Contact")}</Button>
</Flex>
</Flex>
</Card>
Free space allocation
flex sets the behavior of a nested Flex as an element of the parent container. Here the free space is divided in a ratio of 1:2.
<script lang="ts">
import { Flex } from '@woodger/antd';
</script>
<Flex gap="small">
<Flex
flex={1}
justify="center"
style="
min-width: 0;
padding: 16px 4px;
background: var(--ant-color-primary-bg);
"
>1</Flex>
<Flex
flex={2}
justify="center"
style="
min-width: 0;
padding: 16px 4px;
background: var(--ant-color-fill-secondary);
"
>2</Flex>
</Flex>
- Development
- Check
- Publication
Native container
component selects the HTML tag. The list remains a real ul with immediate li; A React component cannot be passed.
<script lang="ts">
import { Flex } from '@woodger/antd';
</script>
<Flex component="ul" wrap gap="small" style="
padding: 0;
list-style: none;
" aria-label={("Release stages")}>
<li>
{("Development")}
</li><li>
{("Check")}
</li><li>
{("Publication")}
</li>
</Flex>
Project activities
Saved review example with direction, interval and distribution of actions.
<script lang="ts">
import { Button, Flex, type FlexJustify } from '@woodger/antd';
let vertical = $state(false);
let gap = $state('small');
let justify = $state<FlexJustify>('flex-start');
</script>
<style>
.controls {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 12px;
}
.controls label {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 8px;
}
</style>
<div class="controls">
<label><input
type="checkbox"
bind:checked={vertical}
/>{(" Vertical layout")}</label>
<label>{("Interval ")}<select bind:value={gap}>
<option value="small">
{("Small")}
</option>
<option
value="medium"
>{("Medium")}</option>
<option
value="large"
>{("Large")}</option>
</select></label>
<label>{("Alignment ")}<select
bind:value={justify}
><option
value="flex-start"
>{("At the beginning")}</option><option
value="center"
>{("In the center")}</option><option
value="space-between"
>{("Between elements")}</option></select></label>
</div>
<Flex {vertical} {gap} {justify} wrap style="margin-top:16px">
<Button type="primary">
{("Main action")}
</Button><Button>
{("Additional")}
</Button><Button
type="dashed"
>{("Other actions")}</Button>
</Flex>
API
Themes of the ant.design live page are mapped to the public API of the installed package. Full definitions of Props are in @woodger/antd.
| Theme | Example or limitation |
|---|---|
| Basic | Row and column. |
| align | Main and transverse axles. |
| gap | Thematic and numerical intervals. |
| Wrap | Transfer and local scrolling. |
| Combination with components | Composition of Card, Tag and Button. |
| Property | Description | Type | Default |
|---|---|---|---|
prefixCls | Full local CSS prefix. The empty string inherits the common prefix; ant-flex is used without configuration. The custom component receives the allowed classes. | string | ConfigProvider + -flex |
orientation | Direction; takes precedence over vertical. | horizontal | vertical | horizontal |
vertical | Column instead of row if orientation is not specified; without a local prop, ConfigProvider.flex.vertical is inherited. | boolean | false |
wrap | true matches wrap. Without wrapping, wide content requires limiting or scrolling. | boolean | wrap | nowrap | wrap-reverse | — |
justify | Distribution along the main axis. | FlexJustify | CSS normal |
align | Alignment along the transverse axis. | FlexAlign | stretch for column |
gap | Theme preset, CSS string or number in px. | small | medium | large | string | number | — |
flex | CSS flex container as an element of its parent. | string | number | — |
component | Native HTML tag or custom Svelte component with forwarding root props. | keyof HTMLElementTagNameMap | Component<Props> | div |
children | Child elements without additional wrappers. | Snippet | — |
style | CSS of the root container; explicit props flex and numeric/string gap are added after style. | string | — |
nativeElement | bind:nativeElement returns root; the custom component must pass a $bindable ref. | HTMLElement | — |
Semantic DOM
Flex creates one container, div by default. children is rendered directly, so the CSS, focus, and semantics of the immediate children are preserved. component selects an HTML tag or Svelte component; bind:nativeElement returns the root after mount if the custom component passes a ref.
ConfigProvider.flex inherits class/style and vertical. A custom component must pass children, class/style and dir to its root; nativeElement needs $bindable ref. There are no separate classNames/styles. Local props specify defaults, direction is inherited from ConfigProvider.
Registration tokens
Flex does not have its own component tokens. small/medium/large use common paddingXS/padding/paddingLG and react to the section theme. Numeric and CSS spacing remain defined by the application.
| Token | Current value |
|---|---|
padding | 16 |
paddingLG | 24 |
paddingXS | 8 |
FAQ
When do you need Space?
For individual elements with wrappers and separators. Flex manages immediate children via CSS flexbox.
Why justify and align are swapped in a column?
Their meaning is associated with the main and transverse axes. The main axis of the column is vertical.
Why doesn't the element stretch from flex?
Prop flex refers to the container itself. To stretch its contents, adjust the children and their parent alignment.
Why is there no nativeElement in your own component?
The native Svelte root must declare $bindable nativeElement and pass a reference to its DOM node.