# Grid layouts

Build a miniature workspace with a heading, tools, a canvas, and a status row. Grid keeps the tool and canvas columns aligned while the header and footer span both.

<div class="docs-demo-layout">

```csharp
using Xui.Core.Canvas;
using Xui.Core.UI;
using Xui.Core.UI.Layout;

public sealed class GridExample : Grid
{
    public GridExample()
    {
        Margin = 24;
        TemplateColumns = [TrackSize.Fr(1), TrackSize.Fr(2)];
        TemplateRows = [TrackSize.Px(72), TrackSize.Fr(1), TrackSize.Px(64)];
        ColumnGap = 12;
        RowGap = 12;
        TemplateAreas = ["heading heading", "tools canvas", "footer footer"];
        Add(Panel("A tiny workspace", "heading", 0xD6B8F4FF));
        Add(Panel("Tools", "tools", 0xEADCFBFF));
        Add(Panel("Canvas", "canvas", 0xF5EEFCFF));
        Add(Panel("All changes saved", "footer", 0xEADCFBFF));
    }

    private static Border Panel(string text, string area, uint color)
    {
        var panel = new Border
        {
            Padding = 12,
            BackgroundColor = new Color(color),
            Content = new Label
            {
                Text = text,
                FontFamily = "Inter",
                FontSize = 16
            }
        };
        panel[Area] = area;
        return panel;
    }
}
```

<figure class="docs-demo" data-demo-url="../../../wasm/?embed=1&amp;demo=layout/grid">
<div class="docs-demo-stage">
<img src="layout-grid.svg" width="400" height="600" alt="Grid layouts: initial rendered view" />
<button type="button" class="docs-demo-start" hidden><span class="docs-demo-pill">Start demo</span></button>
</div>
<div class="docs-demo-actions">
<button type="button" class="docs-demo-close" hidden>Close demo</button>
<span class="docs-demo-status" role="status" aria-live="polite"></span>
</div>
<figcaption>Grid layouts · <a href="layout-grid.cs">C# source</a> · <a href="layout-grid.svg">SVG</a> · <a href="../../../wasm/?embed=1&amp;demo=layout/grid">Open demo</a></figcaption>
</figure>

</div>


## Describe tracks

`TemplateColumns` and `TemplateRows` define explicit tracks. `TrackSize.Px(72)` reserves a fixed 72 logical units. `TrackSize.Fr(1)` takes a proportional share of available space after fixed tracks and gaps are accounted for. The two columns use a 1:2 ratio.

`TrackSize.Auto` sizes from content. Use it for content whose natural size should determine a track, and use fractional tracks when the container has a bounded dimension to distribute. A fraction is not a magic way to obtain a viewport from an unconstrained vertical stack.

`RowGap` and `ColumnGap` add separation between tracks. The grid's margin leaves space outside the entire workspace.

## Name the regions

Each `TemplateAreas` string describes one row. Repeating `heading` across both cells makes it span the columns; the child declares `panel[Grid.Area] = "heading"`.

Use rectangular named regions and keep their names consistent with the child extras. A dot marks an unnamed cell. Names make a layout easier to read when several children span tracks.

Alternatively, place a child with `Grid.RowStart` and `Grid.ColumnStart`. Lines are 1-based; zero means automatic placement. `RowEnd` and `ColumnEnd` are exclusive end lines, while `RowSpan` and `ColumnSpan` express spans directly. Avoid mixing a named area and unrelated explicit lines without a reason.

## Change the layout safely

Grid track and area properties are plain settable values. After changing them at runtime, call `Invalidate()` to request the layout work. Child extras similarly do not provide automatic property-change notification.

Try switching the second column from `Fr(2)` to `Fr(1)`. Then increase the header text size: a fixed 72-unit row remains fixed, so decide whether that row should instead be `Auto` for larger text.

The grid API resembles CSS Grid, but it is a C# layout implementation, not a browser engine. Use the supported track forms and test the layouts you depend on. `MinMax` currently accepts numeric minimum and maximum lengths; it is not the full CSS grammar for nested track expressions.

For larger structural changes at a width threshold, see [Adapt to available space](../adaptive/index.md).
