# Build layouts that adapt

Version: 0.5.0
Framework commit: d503df2befa756e681fcdc6250e9552dc7388bea
Canonical URL: /docs/0.5.0/design/layout/

Layout has two phases: measure asks children how much space they need; arrange assigns their final bounds. The parent determines the constraints a child sees. Understand that relationship before adding fixed widths or margins to compensate for unexpected behavior.


## Try a bounded results area

This layout keeps the search field at its desired height and gives the remaining space to a scrolling list:

```xml
<Grid xmlns="https://github.com/ferroui" RowDefinitions="Auto,*"
      Height="320" Margin="16">
  <TextBox PlaceholderText="Search records" Margin="0,0,0,12" />
  <ListBox Grid.Row="1">
    <ListBoxItem Content="Ada" />
    <ListBoxItem Content="Grace" />
    <ListBoxItem Content="Margaret" />
  </ListBox>
</Grid>
```

The fixed height makes this fragment easy to test. In a real resizable screen, let the window or parent supply the available height. Add enough items to overflow and reduce the height: the list should scroll while the search field remains visible. Compare the [live Grid example](/docs/0.5.0/controls/reference/grid/) at different preview widths.

## Choose a panel

Use Grid for forms and resizable regions. Use StackPanel for a short flow in one direction. Use WrapPanel when items should continue on another line. Use DockPanel for edges around a remaining content region. Use Canvas when positioning is intrinsically coordinate-based.

```xml
<Grid xmlns="https://github.com/ferroui"
      ColumnDefinitions="Auto,*" RowDefinitions="Auto,Auto" Margin="24">
  <TextBlock Text="Name" Margin="0,0,12,8" />
  <TextBox Grid.Column="1" PlaceholderText="Display name" />
  <Button Grid.Row="1" Grid.Column="1"
          HorizontalAlignment="Left" Content="Save" />
</Grid>
```

The same form in declarative Rust: grid rows and columns take the XAML notation, attached properties use their owner's name, and a tuple or a number converts to a `Thickness`:

```rust
use ferroui_base::layout::HorizontalAlignment;
use ferroui_view::prelude::*;

let form = rsx! {
    Grid { column_definitions: "Auto,*", row_definitions: "Auto,Auto", margin: 24.0,
        TextBlock { text: "Name", margin: (0.0, 0.0, 12.0, 8.0) }
        TextBox { Grid.column: 1, placeholder_text: "Display name" }
        Button { Grid.row: 1, Grid.column: 1, horizontal_alignment: HorizontalAlignment::Left, "Save" }
    }
};
```

## Constraints and scrolling

A StackPanel can offer effectively unbounded space along its stacking direction. A virtualizing list needs a bounded viewport to know which containers to realize. Put the list in a constrained Grid row instead of an outer scrolling StackPanel.

Use `MinWidth`, `MaxWidth`, and alignment to express intent. Fixed pixel coordinates are fragile when text changes, localization makes labels longer, or a user increases the font size.

## Margin, padding, and alignment

Margin reserves space outside a control; padding reserves space around its content. Alignment positions the control within its allocation. A stretched control with a fixed width remains constrained by that width.

Inspect actual bounds in the developer tools when something appears clipped. Check the parent first, then the child, and look for an earlier fixed-size constraint rather than adding more clipping exceptions.

## When the layout still looks wrong

| Result | Check |
| --- | --- |
| The list grows instead of scrolling | Does its parent give it a finite height? |
| A field will not stretch | Is a fixed `Width` or parent column constraining it? |
| Text clips after localization | Is a fixed height preventing wrapping? |
| Spacing doubles unexpectedly | Are both adjacent margins reserving space? |

## Try the controls


Change the column definitions and preview width. Compare the result with the sizing rules above.

### A responsive form

```xml
<Grid xmlns="https://github.com/ferroui" ColumnDefinitions="Auto,*" RowDefinitions="Auto,Auto" Margin="12">
  <TextBlock Text="Name" Margin="0,0,16,12" />
  <TextBox Grid.Column="1" PlaceholderText="Your name" />
  <TextBlock Grid.Row="1" Text="Team" Margin="0,0,16,0" />
  <TextBox Grid.Row="1" Grid.Column="1" PlaceholderText="Your team" />
</Grid>
```

```rust
use ferroui::prelude::*;
use ferroui::base::metadata::from_markup_value;
use ferroui::markup_xaml_loader::FerroRuntimeXamlLoader;

#[repr(C)]
pub struct App {
    base: Application,
}

ferro_class!(App: Application);
ferro_impl_classes!(App: FerroObjectImpl);

impl NewApplication for App {
    fn new_application() -> Ref<Self> {
        instantiate(Self { base: Application::construct() })
    }
}

impl ApplicationImpl for App {
    fn initialize(this: &Self) {
        Self::parent_initialize(this);
        ferroui::controls::register_types();
        FerroRuntimeXamlLoader::register();
        this.styles().add(FluentTheme::new().as_style());
    }

    fn on_framework_initialization_completed(this: &Self) {
        let lifetime = this.application_lifetime();
        if let Some(desktop) = lifetime.as_ref().and_then(|l| l.as_classic_desktop_style_application_lifetime()) {
            let value = FerroRuntimeXamlLoader::load(include_str!("View.xaml"), None, None, None, false)
                .expect("Example XAML should load");
            let view = from_markup_value::<Ref<Control>>(&Some(value)).expect("Example root must be a control");
            let window = Window::new();
            window.set_title(Some("FerroUI documentation example".to_owned()));
            window.set_width(560.0);
            window.set_height(420.0);
            window.set_padding(Thickness::uniform(24.0));
            window.set_content(Some(Control::boxed(view)));
            desktop.set_main_window(Some(window));
        }
        Self::parent_on_framework_initialization_completed(this);
    }
}

fn main() -> std::process::ExitCode {
    let args: Vec<String> = std::env::args().skip(1).collect();
    let result = AppBuilder::configure::<App>().use_platform_detect().start_with_classic_desktop_lifetime(&args);
    std::process::ExitCode::from(result as u8)
}

```

[Download the complete desktop project](/docs/0.5.0/generated/examples/grid.zip). The browser preview runs real FerroUI controls with precompiled Rust support; this desktop application loads the same XAML.


## Public APIs and controls

- [Grid](/docs/0.5.0/controls/reference/grid/), [StackPanel](/docs/0.5.0/controls/reference/stackpanel/), [WrapPanel](/docs/0.5.0/controls/reference/wrappanel/), and [DockPanel](/docs/0.5.0/controls/reference/dockpanel/) — layout strategies.
- [ScrollViewer](/docs/0.5.0/controls/reference/scrollviewer/) and [ListBox](/docs/0.5.0/controls/reference/listbox/) — scrolling content.
- [`Control`](/docs/0.5.0/api/ferroui-controls/struct-control/) — sizing, alignment, and bounds.
- [`GridDefinitions`](/docs/0.5.0/api/ferroui-view/prelude/trait-griddefinitions/) — grid rows and columns from declarative Rust.
- [Developer tools](/docs/0.5.0/tools/devtools/) — inspect actual constraints and values.
