> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# Adaptive layouts

Lay a screen out for whatever size the app has: a phone folded or unfolded, turned on its side, or sharing the screen with another app.

Lynx 4 lays the page out again whenever the app's size changes, as long as the [host app](/guide/glossary.md#host-app) tells it the new size. The apps `ng build --platform` makes do, on iOS and Android, and send the size to your code too. So most of a screen adapts by itself, and the rest takes a signal.

## Lengths that follow the size

| Unit        | Measures against                                     | Follows a new size |
| ----------- | ---------------------------------------------------- | ------------------ |
| `%`         | The parent element                                   | Yes                |
| `vw`, `vh`  | The page: `100vw` is its width                       | Yes                |
| `rpx`       | The app window: `750rpx` is its width                | Yes                |
| `px`        | Nothing: CSS pixels, points on iOS and dp on Android | No                 |
| `rem`, `em` | The font size, which follows the text size instead   | No                 |

```css
.card {
  width: 100%; /* the parent's width, folded or not */
  max-width: 600px; /* but no wider than a phone column */
}
```

- **Prefer `%` and flex layout** for a screen's columns: they follow the parent, so a component works the same in a sheet or a split view.
- **`rpx` scales with the window**, so a `750rpx` box is as wide as the window on a phone's outer screen and on its inner one. A 16px margin written as `32rpx` doubles when the phone unfolds. Keep `rpx` for things meant to be a share of the window.

## Change the screen with `LynxViewport`

When the layout itself should change, such as showing a list and its details side by side once there's room, read [`LynxViewport`](/api/angular-lynx/device/viewport.md):

```typescript title="src/app/inbox.ts"
import { Component, computed, inject } from '@angular/core';
import { LYNX_ELEMENTS, LynxViewport } from '@blotch/angular-lynx';

@Component({
  selector: 'app-inbox',
  imports: [LYNX_ELEMENTS],
  template: `
    <view class="flex flex-row h-full">
      <view class="flex-1"><text>Messages</text></view>
      @if (twoPane()) {
        <view class="flex-1"><text>The open message</text></view>
      }
    </view>
  `,
})
export class Inbox {
  readonly #viewport = inject(LynxViewport);
  readonly twoPane = computed(() => this.#viewport.width() >= 600);
}
```

- **`width()` and `height()` are the page's size**, which `vw`, `vh` and a page-wide `%` measure against. **`screenWidth()` and `screenHeight()` are the window's**, which `rpx` measures against. In an app from `ng build --platform` the two are the same, except with `"edgeToEdge": false` and, on Android, while the keyboard is open.
- **`orientation()`** is `'portrait'` or `'landscape'`: the page's shape, not the way the phone is held. An unfolded phone held upright can be either.
- **The values arrive before Lynx lays the page out at the new size**, so the screen changes in one step.

[`LynxSystemInfo`](/api/angular-lynx/device/system-info.md)'s `screenWidth` and `screenHeight` are the screen's size when the page started, and don't follow a fold. Use `LynxViewport` instead.

## `@media` rules

Lynx 4 supports CSS media queries, and the build turns them on. A `@media (min-width: 600px)` rule, or Tailwind's `md:` and the other breakpoints, applies on iOS and Android, against the page's size, and again when the size changes:

```css
.panel {
  padding: 16px;
}

@media (min-width: 600px) {
  .panel {
    padding: 32px;
  }
}
```

- **Lynx measures the page:** `width` and `height` are the LynxView's size in CSS pixels, as `LynxViewport.width()` and `height()` are. Lynx's [media queries](https://lynxjs.org/next/api/css/media-query) page lists the other features it reads, such as `orientation` and ranges like `(width >= 600px)`.
- **Don't toggle `display` with one.** On Lynx 4.1, a `display` set in a media rule doesn't always apply, at launch or after the size changes, though colors, sizes and opacity do. Show or hide a part of the screen with `@if` and `LynxViewport` instead:

```angular-html
@if (viewport.width() >= 600) {
  <view class="detail"></view>
}
```

`@media (prefers-color-scheme: dark)` and Tailwind's `dark:` on their own are turned into rules the page's appearance switches, so they follow `LynxTheme`. A query that asks for a color scheme and something else, such as `(prefers-color-scheme: dark) and (min-width: 600px)`, is left to Lynx, which answers the color scheme from the app's appearance in the apps `ng build --platform` makes: see [Dark mode](/guide/dark-mode.md).

## What changes the size

| Change                                | iOS                                           | Android                                       |
| ------------------------------------- | --------------------------------------------- | --------------------------------------------- |
| Folding or unfolding a foldable phone | No foldable iPhones                           | Yes                                           |
| Rotating the phone                    | With `"orientation": "all"` in `angular.json` | With `"orientation": "all"` in `angular.json` |
| Split screen, a resized window        | iPad only, and the apps are iPhone-only today | Yes                                           |
| The keyboard opening                  | No: the keyboard covers the page              | Yes: the page gets shorter                    |

Through all of these the app keeps running: the screen isn't rebuilt, and its state stays.

## In your own native app

A native app that [embeds the app](/guide/embedding/index.md) does what Lynx's [foldable guide](https://lynxjs.org/next/guide/inclusion/foldable-devices) describes whenever its window or LynxView changes size, in this order:

1. **The window's size**, which `rpx` follows: `updateScreenMetricsWithWidth:height:` on iOS, in points, or `updateScreenMetrics(width, height)` on Android, in pixels. On its own it lays nothing out.
2. **The LynxView's size:** `updateViewportWithPreferredLayoutWidth:preferredLayoutHeight:` on iOS, or `updateViewport(widthMeasureSpec, heightMeasureSpec)` on Android, which a LynxView also calls itself when it's measured.
3. **The global props** `screenWidth`, `screenHeight`, `viewportWidth` and `viewportHeight`, in CSS pixels, which `LynxViewport` reads.

Without the global props, `LynxViewport` still follows the page's size, a moment later: from the window's `resize` event, once Lynx has laid the page out.

## Related

- [`LynxViewport`](/api/angular-lynx/device/viewport.md)
- [Safe area](/guide/safe-area.md)
- [What the generated apps pass](/guide/embedding/host-data.md#what-the-generated-apps-pass)
