# OpenTUI Layout System
OpenTUI uses the Yoga layout engine, providing CSS Flexbox-like capabilities for positioning and sizing components in the terminal.
## Overview
Key concepts:
- **Flexbox model**: Familiar CSS Flexbox properties
- **Yoga engine**: Facebook's cross-platform layout engine
- **Terminal units**: Dimensions are in character cells (columns x rows)
- **Percentage support**: Relative sizing based on parent
## Flex Container Properties
### flexDirection
Controls the main axis direction:
```tsx
// Row (default) - children flow horizontally
1
2
3
// Output: 1 2 3
// Column - children flow vertically
1
2
3
// Output:
// 1
// 2
// 3
// Reverse variants
... // 3 2 1
... // Bottom to top
```
### justifyContent
Aligns children along the main axis:
```tsx
{/* Children at start (left for row) */}
{/* Children at end (right for row) */}
{/* Children centered */}
{/* First at start, last at end, rest evenly distributed */}
{/* Equal space around each child */}
{/* Equal space between all children and edges */}
```
### alignItems
Aligns children along the cross axis:
```tsx
{/* Children at top */}
{/* Children at bottom */}
{/* Children vertically centered */}
{/* Children stretch to fill height */}
{/* Children aligned by text baseline */}
```
### flexWrap
Controls whether children wrap to new lines:
```tsx
{/* Children overflow (default) */}
{/* Children wrap to next row */}
{/* Children wrap upward */}
```
### gap
Space between children:
```tsx
A
B
C
// Output: A B C (2 spaces between)
```
## Flex Item Properties
### flexGrow
How much a child should grow relative to siblings:
```tsx
1
2
1
// Widths: 7.5 | 15 | 7.5 (1:2:1 ratio)
```
### flexShrink
How much a child should shrink when space is limited:
```tsx
Shrinks
Fixed
```
### flexBasis
Initial size before growing/shrinking:
```tsx
Starts at 20, can grow
Half of parent
```
### alignSelf
Override parent's alignItems for this child:
```tsx
Centered
Top
Bottom
```
## Dimensions
### Fixed Dimensions
```tsx
{/* Exactly 40 columns by 10 rows */}
```
### Percentage Dimensions
Parent must have explicit size:
```tsx
{/* Half of parent */}
```
### Min/Max Constraints
```tsx
{/* Constrained sizing */}
```
## Spacing
### Padding (inside)
```tsx
// All sides
Content
// Individual sides
Content
```
### Margin (outside)
```tsx
// All sides
Content
// Individual sides
Content
```
## Positioning
### Relative (default)
Element flows in normal document order:
```tsx
{/* Normal flow */}
```
### Absolute
Element positioned relative to nearest positioned ancestor:
```tsx
Positioned at (10, 5)
```
### Position Properties
```tsx
Content
```
## Display
### Visibility Control
```tsx
// Visible (default)
Visible
// Hidden (removed from layout)
Hidden
```
## Overflow
```tsx
{/* Content can extend beyond bounds (default) */}
{/* Content clipped at bounds */}
{/* Scrollable when content exceeds bounds */}
```
## Z-Index
Control stacking order for overlapping elements:
```tsx
Behind
In front
```
## See Also
- [Layout Patterns](./patterns.md) - Common layout recipes
- [Components/Containers](../components/containers.md) - Box and ScrollBox details