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

# CSS on Lynx

Lynx draws your app with its own CSS engine, not a browser's. Most layout and visual CSS carries over. Some defaults differ, though, and some features are missing.

This page covers what trips up web developers, and what to write instead.

For every property and value Lynx accepts, see the [Lynx CSS reference](https://lynxjs.org/api/css/properties.html).

## At a glance

| On the web                                    | On Lynx                                                                                  |
| --------------------------------------------- | ---------------------------------------------------------------------------------------- |
| `:root`, `html`, `body`                       | [`page`](#the-root-is-page)                                                              |
| `display: block` by default                   | `display: linear` (a vertical stack) by default. `flex` and `grid` work                  |
| `box-sizing: content-box`, collapsing margins | `border-box`, and margins never collapse                                                 |
| Text anywhere                                 | Text drawn by a `<text>`                                                                 |
| `color` and fonts inherit                     | [Into HTML text tags and loose text](/guide/styling/inheritance.md), not a Lynx `<text>` |
| `1rem` is 16px                                | `1rem` is 14px                                                                           |
| `overflow: auto` scrolls                      | Only `<scroll-view>` and `<list>` scroll                                                 |
| `position: fixed` pins to the viewport        | Positions against the `page`. Use `<overlay>` for floating UI                            |
| `:hover`, `::before`, `!important`            | [Not supported](#selectors-and-rules)                                                    |
| `hsl(240 6% 10%)`                             | [Dropped silently](#colors). Write `hsl(240, 6%, 10%)` or `rgba()`                       |

## The root is `page`

```css title="src/styles.css"
page {
  background-color: #ffffff;
  --brand: rgba(79, 70, 229, 1);
}
```

- [**`page`**](/guide/glossary.md#page) is the root [element](/guide/glossary.md#element) of every Lynx app. Its selector does the job of `:root` and `body`.
- `html` and `body` match nothing.
- Put app-wide CSS variables on `page`. Every element below it can read them.

## Layout

```css
.toolbar {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 8px;
}
```

- **Elements stack vertically by default.** Lynx's default `display` is `linear`, which lays children out in a column. `flex` and `grid` work as on the web.
- **`display: block` becomes `linear`.** Lynx has no block layout, and would lay a `block` element out as a row. So the build rewrites it in your stylesheets, and AngularLynx does the same in inline styles.
  - `:host { display: block }` keeps working.
  - `inline-flex` and `inline-grid` become `flex` and `grid`.
  - `inline` and `inline-block` become `linear`. An element can't sit inline next to others, though, so it still stretches across its parent.
- **`flex-direction: row` needs `display: flex`.** On a linear element it does nothing. In Tailwind, write `flex flex-row`, not `flex-row` alone.
- **Children stretch across their parent.** A child with no width fills the parent's cross axis. Add `align-self: flex-start` (Tailwind `self-start`) to make a badge hug its content.
- **Boxes are `border-box`**, and **margins never collapse**: a 16px bottom margin above a 16px top margin makes 32px.
- **`min-width` and `min-height` default to 0**, not `auto`. A flex item can shrink below its content and clip its text. Set a `min-width` to stop it.
- **Every element is `position: relative`** by default, so `z-index` works on any element.
- **Siblings paint in source order.** A later sibling draws on top, even when an earlier one is `position: absolute`.
- **`position: fixed`** acts like `position: absolute` against the `page`. For floating UI such as menus, use [`<overlay>`](/guide/overlays.md).
- **`position: sticky`** works only inside a `<scroll-view>`.
- **`overflow` is `visible` or `hidden`.** A `<view>` never scrolls. Use [`<scroll-view>`](/api/elements/scroll-view.md) or [`<list>`](/api/elements/list.md).

[Elements and layout](/guide/elements/index.md) covers which element to use for what.

## Text

```css
.summary {
  font-size: 15px;
  line-height: 22px;
  text-overflow: ellipsis;
}
```

```angular-html
<text class="summary" text-maxline="2">
  Your order left the warehouse this morning and arrives on Friday.
</text>
```

- **Only a [`<text>`](/api/elements/text.md) draws text.** Text placed directly in a `<view>` is drawn in a `<text>` of its own, which inherits text styles (see [Text inheritance](/guide/styling/inheritance.md)).
- **Put text styles on the `<text>`.** A `color`, `font-size` or `line-height` on a `<view>` doesn't reach the text inside it (see [Text inheritance](/guide/styling/inheritance.md)).
- **`line-height` works only on `<text>`.**
- **`white-space` is `normal` or `nowrap`.** There's no `pre`, so spaces and line breaks always collapse.
- **There's no `text-transform`**, so Tailwind's `uppercase` does nothing. Change the case in TypeScript.
- **There's no `text-align: justify`.**
- **Clamp lines with the `text-maxline` attribute**, as above, instead of `-webkit-line-clamp`.
  - Pair it with `text-overflow: ellipsis`. Without it, Android draws the extra lines past the box.
  - The build warns about a `text-maxline` without one.
  - [`<ui-text [maxLines]>`](/dolan/typography.md#line-limit) sets both.

## Inheritance

**Most CSS properties don't inherit on Lynx.** A `color` on a `<view>` doesn't reach the `<text>` inside it. `font-size`, CSS variables, HTML text tags and loose text are the exceptions.

[Text inheritance](/guide/styling/inheritance.md) covers what inherits, and how to make every text property inherit as on the web.

## Units

| Unit                  | On Lynx                                                      |
| --------------------- | ------------------------------------------------------------ |
| `px`                  | A device-independent pixel, as on the web                    |
| `rem`                 | 14px, Lynx's root font size. The default `font-size` is 14px |
| `rpx`                 | 1/750 of the screen width, so `750rpx` is always full width  |
| `%`, `em`, `vw`, `vh` | As on the web                                                |

- **`rem` is 14px, not 16px.** Rem-based sizes come out 12.5% smaller than in a browser. That includes Tailwind's scale: `h-11` is 38.5px. Use `px` where a size must line up with something exact.
- **The [UI components](/dolan/index.md) theme makes `1rem` 16px**, with `page { font-size: 16px }`, so Tailwind's scale matches the web there.
- **Lengths need units.** Only `0` can go without one, and `margin: 5` is ignored. Times need units too: `0ms`, not `0`.
- **`calc()` and `env()` work. `min()`, `max()` and `clamp()` don't.**

## Colors

| Format                     | Example                     | Works |
| -------------------------- | --------------------------- | ----- |
| Hex                        | `#18181b`, `#ff000080`      | Yes   |
| Named                      | `red`, `transparent`        | Yes   |
| `rgb()` / `rgba()`, commas | `rgba(24, 24, 27, 0.5)`     | Yes   |
| `rgb()`, spaces            | `rgb(24 24 27 / 0.5)`       | Yes   |
| `hsl()` / `hsla()`, commas | `hsla(240, 5.9%, 10%, 0.5)` | Yes   |
| `hsl()`, spaces            | `hsl(240 5.9% 10% / 0.5)`   | No    |
| `currentColor`             |                             | No    |

- **Space-separated `hsl()` fails silently.** The element draws transparent, with no warning. Lynx's parser reads `hsl()` only in its comma form.
- **`currentColor` isn't supported.** Use the color itself, or a CSS variable.

### Theme colors in CSS variables

Store each color whole in a variable, then reference the variable directly:

```css title="src/styles.css"
page {
  --primary: rgba(24, 24, 27, 1);
  --primary-foreground: rgba(250, 250, 250, 1);
  --primary-subtle: rgba(24, 24, 27, 0.1);
}

.button {
  background-color: var(--primary);
}

.button-label {
  color: var(--primary-foreground);
}
```

- **Don't store raw channels** (`--primary: 240 5.9% 10%`) to wrap later in `hsl(var(--primary))`. That expands to the space-separated `hsl()` Lynx drops, which is why shadcn-style themes render blank.
- **For a translucent shade, add a variable of its own**, like `--primary-subtle`, or use channels in `rgba()`, below.
- **Set variables in a stylesheet, a `style` attribute or a binding.** `[style.--primary]="brand()"` sets one at runtime. Everything inside the element reads it, on iOS, Android and in the web preview.

### One color at any opacity

Keep a color's channels in a second variable, comma-separated, and wrap it in `rgba()`:

```css title="src/styles.css"
page {
  --primary: rgba(24, 24, 27, 1);
  --primary-rgb: 24, 24, 27;
}

.scrim {
  background-color: rgba(var(--primary-rgb), 0.5);
}
```

- Lynx replaces `var()` with the variable's text before it reads the color, so `24, 24, 27` fills three arguments, as in a browser.
- **Write the channels with commas** for `rgba()`. Space-separated channels work only in the `rgb(var(--x) / 0.5)` form.
- An undefined variable drops the whole value, and the element draws transparent.

The [UI components](/dolan/index.md) theme works this way. Its Tailwind plugin turns `bg-primary/50` into `rgba(var(--primary-rgb), 0.5)`. See [Theming](/dolan/theming.md#opacity-modifiers).

## Selectors and rules

- **Selectors that work:** type (`text`), class, ID, `*`, compound (`.a.b`), descendant, child (`>`), sibling (`+`, `~`), `:not()`, and attribute selectors such as `[disabled]`, which the build turns into classes (see [Attribute selectors](#attribute-selectors)).
- **At-rules that work:** `@font-face`, `@keyframes`, `@import` and `@media`, against the page's size (see [Adaptive layouts](/guide/adaptive-layouts.md#media-rules)).

| Missing                                                     | Use instead                                                         |
| ----------------------------------------------------------- | ------------------------------------------------------------------- |
| `:hover`, `:focus`, `:active`                               | A class bound to a signal: see [Pressed states](#pressed-states)    |
| `::before`, `::after`                                       | A real element                                                      |
| `:first-child`, `:nth-child()` and other structural classes | A class bound to `@for`'s `$first`, `$last` or `$index`             |
| `:is()`, `:where()`, `:has()`                               | Separate selectors                                                  |
| `@supports`, `@layer`, `@container`, `@property`            | Nothing                                                             |
| `!important`                                                | A more specific selector                                            |
| `pointer-events`                                            | Leave it out: the build fails on it. The last sibling gets the taps |
| `float`, `columns`                                          | Flexbox or grid                                                     |
| `outline`                                                   | `border`                                                            |

**Combinators restyle when a class changes**, as on the web. `.selected .label` applies as soon as an ancestor gains `selected`.

To turn this off, set the `enableCSSInvalidation` [plugin option](/api/build/index.md) to `false`. That saves Lynx some work when a class changes. A combinator then only matches when the element's own classes are set.

### Attribute selectors

Lynx's CSS engine matches no attribute selector, so the build turns each into a class, and the runtime gives an element that class while its attribute matches. Write them as on the web:

```css title="src/styles.css"
.btn[disabled] {
  opacity: 0.5;
}

:root[data-theme='dark'] .card {
  background-color: #18181b;
}

[aria-expanded='true'] > .chevron {
  transform: rotate(90deg);
}
```

```html
<view class="btn" [attr.disabled]="busy() ? '' : null"></view>
<view [attr.aria-expanded]="open()"><view class="chevron"></view></view>
```

- **Every operator works:** `[a]`, `[a=v]`, `[a~=v]`, `[a|=v]`, `[a^=v]`, `[a$=v]`, `[a*=v]` and the `i` flag, in global and component styles, and inside a `:not()` of compounds, such as `:not([disabled])`.
- **`:root` and `html` are the page.** Bind the attribute on the root component's host, which is the page: `host: { '[attr.data-theme]': 'theme()' }`. `:root[data-theme='dark']` becomes `page` with a class, which weighs one type selector and one class (0,1,1), where on the web it weighs two classes (0,2,0). `html[…]` keeps its weight. If a rule of the same weight competes with a `:root[…]` rule, make the `:root[…]` one more specific or put it later.
- **A changing attribute restyles at once,** with the descendants a combinator matches.
- **A property binding of `false`** (`[disabled]="false"`) counts as no attribute, as a browser's boolean properties do.
- **Some stay as written,** with a build warning naming the selector and the file: one inside `:is()`, `:where()` or `:has()`, one inside a `:not()` with a combinator in it (`:not(.list [hidden])`), one with a namespace, and `[class]`, `[style]` and `[id]`. Use a class there.

### Pressed states

**`:active` rules don't apply** in AngularLynx builds, and neither does Tailwind's `active:` variant. Set a signal, such as `pressing = signal(false)`, from touch events instead, and bind a class to it:

```html
<view
  [class.bg-muted]="pressing()"
  (bindtouchstart)="pressing.set(true)"
  (bindtouchend)="pressing.set(false)"
  (bindtouchcancel)="pressing.set(false)"
  (bindmousedown)="pressing.set(true)"
  (bindmouseup)="pressing.set(false)"
  (bindmouseleave)="pressing.set(false)"
></view>
```

The mouse events are for the [web preview](/guide/glossary.md#web-preview), which sends no touches for a cursor. The [UI components](/dolan/index.md) draw their pressed states this way.

## Component styles

`styles` and `styleUrl` are scoped to the component, as in Angular on the web. Angular scopes them with attributes, which the build turns into classes.

[Component styles](/guide/styling/component-styles.md) covers `:host`, `::ng-deep`, view encapsulation and type selectors.

## Related

- [Tailwind CSS](/guide/tailwindcss.md)
- [CSS Modules](/guide/css-modules.md)
- [Dark mode](/guide/dark-mode.md)
- [Elements and layout](/guide/elements/index.md)
- [Lynx CSS reference](https://lynxjs.org/api/css/properties.html) and [selectors](https://lynxjs.org/api/css/selectors.html)
