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.
At a glance
The root is page
pageis the root element of every Lynx app. Its selector does the job of:rootandbody.htmlandbodymatch nothing.- Put app-wide CSS variables on
page. Every element below it can read them.
Layout
- Elements stack vertically by default. Lynx's default
displayislinear, which lays children out in a column.flexandgridwork as on the web. display: blockbecomeslinear. Lynx has no block layout, and would lay ablockelement 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-flexandinline-gridbecomeflexandgrid.inlineandinline-blockbecomelinear. An element can't sit inline next to others, though, so it still stretches across its parent.
flex-direction: rowneedsdisplay: flex. On a linear element it does nothing. In Tailwind, writeflex flex-row, notflex-rowalone.- Children stretch across their parent. A child with no width fills the parent's cross axis. Add
align-self: flex-start(Tailwindself-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-widthandmin-heightdefault to 0, notauto. A flex item can shrink below its content and clip its text. Set amin-widthto stop it.- Every element is
position: relativeby default, soz-indexworks on any element. - Siblings paint in source order. A later sibling draws on top, even when an earlier one is
position: absolute. position: fixedacts likeposition: absoluteagainst thepage. For floating UI such as menus, use<overlay>.position: stickyworks only inside a<scroll-view>.overflowisvisibleorhidden. A<view>never scrolls. Use<scroll-view>or<list>.
Elements and layout covers which element to use for what.
Text
- Only a
<text>draws text. Text placed directly in a<view>is drawn in a<text>of its own, which inherits text styles (see Text inheritance). - Put text styles on the
<text>. Acolor,font-sizeorline-heighton a<view>doesn't reach the text inside it (see Text inheritance). line-heightworks only on<text>.white-spaceisnormalornowrap. There's nopre, so spaces and line breaks always collapse.- There's no
text-transform, so Tailwind'suppercasedoes nothing. Change the case in TypeScript. - There's no
text-align: justify. - Clamp lines with the
text-maxlineattribute, 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-maxlinewithout one. <ui-text [maxLines]>sets both.
- Pair it with
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 covers what inherits, and how to make every text property inherit as on the web.
Units
remis 14px, not 16px. Rem-based sizes come out 12.5% smaller than in a browser. That includes Tailwind's scale:h-11is 38.5px. Usepxwhere a size must line up with something exact.- The UI components theme makes
1rem16px, withpage { font-size: 16px }, so Tailwind's scale matches the web there. - Lengths need units. Only
0can go without one, andmargin: 5is ignored. Times need units too:0ms, not0. calc()andenv()work.min(),max()andclamp()don't.
Colors
- Space-separated
hsl()fails silently. The element draws transparent, with no warning. Lynx's parser readshsl()only in its comma form. currentColorisn'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:
- Don't store raw channels (
--primary: 240 5.9% 10%) to wrap later inhsl(var(--primary)). That expands to the space-separatedhsl()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 inrgba(), below. - Set variables in a stylesheet, a
styleattribute 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():
- Lynx replaces
var()with the variable's text before it reads the color, so24, 24, 27fills three arguments, as in a browser. - Write the channels with commas for
rgba(). Space-separated channels work only in thergb(var(--x) / 0.5)form. - An undefined variable drops the whole value, and the element draws transparent.
The UI components theme works this way. Its Tailwind plugin turns bg-primary/50 into rgba(var(--primary-rgb), 0.5). See Theming.
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). - At-rules that work:
@font-face,@keyframes,@importand@media, against the page's size (see Adaptive layouts).
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 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:
- Every operator works:
[a],[a=v],[a~=v],[a|=v],[a^=v],[a$=v],[a*=v]and theiflag, in global and component styles, and inside a:not()of compounds, such as:not([disabled]). :rootandhtmlare the page. Bind the attribute on the root component's host, which is the page:host: { '[attr.data-theme]': 'theme()' }.:root[data-theme='dark']becomespagewith 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:
The mouse events are for the web preview, which sends no touches for a cursor. The UI components 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 covers :host, ::ng-deep, view encapsulation and type selectors.
