Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 42 ยท XAML and Layouts

Sizing, Alignment and Spacing

Learn WidthRequest, HeightRequest, HorizontalOptions, VerticalOptions, Margin, Padding, Thickness, safe areas, device-independent units and how MAUI measures and arranges controls.

Every visual control in MAUI has the same set of layout properties. Once you know them, Grid, stacks and flex layouts all become easier, because they all use these properties to decide where a child goes and how big it is. This lesson covers size requests, alignment, margin and padding, units and safe areas, and the two-step process (measure, then arrange) that sits behind them.

XAML is read-only. The runnable blocks are stand-ins that compute the box model and the alignment rule in plain C#.

Device-independent units

MAUI measures in device-independent units (DIPs, sometimes called points). A phone with a high-density screen has many physical pixels per unit, so WidthRequest="100" looks about the same physical size on different screens. You almost never think in pixels. To convert, use DeviceDisplay.Current.MainDisplayInfo.Density (pixels = units ร— density).

Requesting a size

WidthRequest and HeightRequest ask for a size. They are requests: the parent may give less if there is no room. MinimumWidthRequest and MinimumHeightRequest set lower bounds; MaximumWidthRequest and MaximumHeightRequest set upper bounds.

XML
<Image Source="avatar" WidthRequest="64" HeightRequest="64" />
<Button Text="Wide" MinimumWidthRequest="200" />

The default is -1, which means "no request: use the natural size". Prefer flexible layouts to fixed sizes; set a size request only for things that really have a fixed size, such as icons and avatars.

After layout, the real values are Width and Height (read-only; they are -1 until the control is laid out).

HorizontalOptions and VerticalOptions

These control a child's position inside the space its parent gives it:

Value Effect
Fill Stretch to fill (the default for most controls)
Start Stick to the left/top (right in right-to-left languages)
Center Centre it, using its natural size
End Stick to the right/bottom
XML
<Button Text="Save" HorizontalOptions="End" />
<Label Text="Centered" HorizontalOptions="Center" VerticalOptions="Center" />

In older Xamarin.Forms code you will see FillAndExpand and CenterAndExpand. The AndExpand part is gone in MAUI; to take extra space use a Grid with a * row or FlexLayout.Grow. Also remember HorizontalTextAlignment and VerticalTextAlignment (on Label and Entry) align the text inside the control, a different thing from aligning the control itself.

Margin and Padding

  • Margin is space outside a control's border, between it and its neighbours. It is set on the control.
  • Padding is space inside the border, between the border and the content. It exists on layouts, Border, Frame, Button, Entry and other controls that have content.

Both use a Thickness, which can be written in XAML as:

Written Meaning
Margin="10" 10 on all sides
Margin="20,10" 20 left and right, 10 top and bottom
Margin="1,2,3,4" left, top, right, bottom
XML
<Label Text="Card" Margin="0,16,0,0" Padding="12" BackgroundColor="LightGray" />

Here the background colour fills the padding area (the label's own space) but not the margin. In layouts, Spacing (stacks) or RowSpacing and ColumnSpacing (grid) add gaps without using margins on every child.

The box model, runnable

Given a requested size, margin and padding, how much room does a control need in total? The stand-in prints the box dimensions. It mirrors the model, not MAUI's actual code.

C# Stand-in: margin, padding and total space

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.

Measure, then arrange

MAUI lays out a screen in two passes:

  1. Measure: the parent asks each child "how big would you like to be, given at most this much space?" The child answers with its desired size (including its requests, padding and margin).
  2. Arrange: the parent decides the final rectangle for each child (in its slot) and tells it. The child's HorizontalOptions and VerticalOptions then decide where inside the slot it sits and whether it stretches.

This is why Center makes a button shrink to its natural size, while Fill makes it as wide as its slot. The second stand-in shows the alignment rule.

C# Stand-in: aligning a child inside a slot

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.

Safe areas and device quirks

Modern phones have notches, rounded corners and gesture bars. The safe area is the part of the screen not covered by them. MAUI's ContentPage respects the safe area by default on iOS and Android. If you want content to extend edge to edge, set SafeAreaEdges (available in recent versions) on the page or layout, or on older versions ios:Page.UseSafeArea="False" with the iOS platform-specific namespace.

Other things that change layout: the on-screen keyboard (resizes or pans the page), screen rotation (width and height swap) and font scaling set in system accessibility settings. Test with large fonts and in landscape orientation.

Habits for flexible layouts

  • Avoid hard-coded widths for text containers; let text wrap.
  • Use Grid star sizes for screens that should adapt.
  • Prefer Spacing over many individual margins.
  • Keep touch targets at least 44 x 44 units (a common accessibility guideline).
  • Set MaximumWidthRequest to stop content stretching too wide on tablets and desktop windows.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the difference between Margin and Padding?

    Margin separates a control from its neighbours; padding separates its border from its content.
  2. 2. What does Margin="20,10" mean?

    Two numbers are horizontal then vertical.
  3. 3. A button has HorizontalOptions="Center". How wide is it?

    Center uses the natural size and centres it; Fill would stretch it.
  4. 4. What is WidthRequest?

    It is a request in device-independent units. The final size is in Width.
  5. 5. Which two passes does MAUI layout use?

    Parents measure children, then arrange them in final rectangles.

Summary

  • Sizes are in device-independent units; WidthRequest and HeightRequest are requests.
  • HorizontalOptions and VerticalOptions position a child inside its slot (Start, Center, End, Fill).
  • Margin is outside, padding is inside; both are Thickness values.
  • Layout is measure then arrange; account for safe areas, keyboards and font scaling.

Next: Namespaces and Markup Extensions