Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 42 ยท XAML and Layouts

Stack and Grid Layouts

Learn VerticalStackLayout, HorizontalStackLayout and Grid in .NET MAUI - spacing, padding, Auto, star and absolute sizes, row and column spans, plus a runnable star-size solver.

A layout is a control whose job is to position other controls. MAUI offers several: VerticalStackLayout and HorizontalStackLayout (one line of children), Grid (rows and columns), FlexLayout, AbsoluteLayout and the older StackLayout. This lesson covers the two you will use most: stacks and the grid. The next lesson covers flex and absolute layouts.

The XAML below is read-only. The runnable block is a labelled C# stand-in for the Grid's sizing rules (Auto, absolute and star), because that is the part people find hardest.

Stack layouts

VerticalStackLayout puts children one under another; HorizontalStackLayout puts them side by side. Both are lightweight and fast, and both have Spacing (gap between children) and Padding (space inside the edge):

XML
<VerticalStackLayout Padding="20" Spacing="12">
    <Label Text="Title" FontSize="24" />
    <Label Text="Subtitle" TextColor="Gray" />
    <Button Text="Continue" />
</VerticalStackLayout>

A vertical stack gives each child as much height as it asks for, and the full width. A horizontal stack gives each child the height of the stack and the width it asks for. When the content may be taller than the screen, wrap the layout in a ScrollView:

XML
<ScrollView>
    <VerticalStackLayout> ... many children ... </VerticalStackLayout>
</ScrollView>

The older StackLayout has an Orientation property and can be switched at run time, but the two specific stack types are faster and should be your default. Remember that stacks do not stretch their children to fill leftover space; for that you want a Grid or a flex layout.

Grid

A Grid divides its space into rows and columns. You declare them with RowDefinitions and ColumnDefinitions (or the compact string form) and place children with the attached properties Grid.Row and Grid.Column:

XML
<Grid RowDefinitions="Auto,*,Auto"
      ColumnDefinitions="*,*"
      RowSpacing="8" ColumnSpacing="8" Padding="16">

    <Label Grid.Row="0" Grid.ColumnSpan="2" Text="Inbox" FontSize="28" />

    <CollectionView Grid.Row="1" Grid.ColumnSpan="2" />

    <Button Grid.Row="2" Grid.Column="0" Text="Delete" />
    <Button Grid.Row="2" Grid.Column="1" Text="Reply" />
</Grid>

Defaults: rows and columns start at 0, a child with no Grid.Row or Grid.Column sits in cell (0, 0), and a grid with no definitions has a single cell that fills the space. Grid.RowSpan and Grid.ColumnSpan make a child cover several cells.

The three kinds of size

Value Meaning
Auto As big as the biggest child in that row or column needs
a number (80) A fixed size in device-independent units
* or 2* A share of the remaining space, divided by the star numbers

The algorithm: first fixed and Auto rows and columns take their space; then what remains is split among star rows or columns in proportion to their numbers. *,2* gives the first column one third and the second two thirds of the leftover width. * alone means 1*.

Use Auto for headers and buttons that should not grow, and * for the area that fills the rest. Use absolute sizes sparingly: they do not adapt to different screens or font sizes.

Solving star sizes, runnable

The stand-in below implements this rule for one axis. It is not the MAUI Grid (which also handles spans, min and max and measuring children), but it gives the same widths for simple cases, so you can experiment with definitions.

C# Stand-in: Auto, absolute and star sizes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

In the first call, 80 is fixed, leaving 320 for the stars: * gets 320/3 = 106.7 and 2* gets 213.3.

Alignment inside a cell

A control placed in a cell is positioned by HorizontalOptions and VerticalOptions (values Start, Center, End, Fill). The default is Fill, so a button fills its cell. To keep a button small in a wide cell, use HorizontalOptions="Start". We cover these properties in sizing, alignment and spacing.

Stack or Grid?

Prefer a Grid when you need alignment across rows and columns, or when one area must take "all remaining space". Prefer stacks for simple lists of items. A common mistake is nesting many stacks to emulate a grid: it is slower (each nested layout is measured separately) and harder to read. Flatter trees perform better, a point we return to in styling and performance.

A form layout with labels and entries is a classic grid use:

XML
<Grid ColumnDefinitions="Auto,*" RowDefinitions="Auto,Auto" ColumnSpacing="12" RowSpacing="10">
    <Label Grid.Row="0" Text="Name"  VerticalOptions="Center" />
    <Entry Grid.Row="0" Grid.Column="1" />
    <Label Grid.Row="1" Text="Email" VerticalOptions="Center" />
    <Entry Grid.Row="1" Grid.Column="1" Keyboard="Email" />
</Grid>

The first column is Auto, so both labels share the width of the widest label, and the entries take the rest.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A Grid has ColumnDefinitions="100,,2" and is 400 units wide. How wide is the last column?

    100 is fixed, 300 remains and is split 1:2, so the last column gets 200.
  2. 2. Which layout is the best default for a simple vertical list of controls?

    VerticalStackLayout is lightweight and designed exactly for this.
  3. 3. What does Grid.ColumnSpan="2" do?

    A span makes the child occupy several rows or columns.
  4. 4. Where does a child go if it sets no Grid.Row and Grid.Column?

    Both attached properties default to 0.
  5. 5. When should a layout be wrapped in a ScrollView?

    Stacks and grids do not scroll by themselves; ScrollView provides scrolling.

Summary

  • Stack layouts place children in a line with Spacing and Padding; wrap in ScrollView for long content.
  • Grid uses Auto, absolute and star sizes; stars share what is left after fixed and Auto sizes.
  • Use Grid.Row, Grid.Column and the span properties to place children.
  • Prefer flat layouts to deeply nested stacks.

Next: Flex and Absolute Layouts