Tailwind CSS
Style Lynx elements with Tailwind CSS utility classes. New apps get Tailwind CSS 4, set up the way Angular CLI sets it up. Apps made before keep Tailwind CSS 3 with Lynx's preset, which still works.
Add Tailwind
npm create angular-lynx asks whether to add Tailwind, and adds it unless you say no. To add it to an existing app, run the schematic:
It sets up Tailwind 4, as ng new --style=tailwind does:
- Installs
tailwindcssand@tailwindcss/postcssas dev dependencies. - Writes
.postcssrc.json, which runs@tailwindcss/postcss. - Replaces
src/styles.csswith Tailwind's imports and Lynx's own utilities. Copy out any global styles you want to keep first.
An app that already depends on Tailwind 3 keeps it. Pass --tailwind-version=3 or 4 to choose.
Set it up by hand
-
Install Tailwind 4:
-
Run its PostCSS plugin:
.postcssrc.json -
Import Tailwind in your global stylesheet, then Lynx's own utilities:
src/styles.css
This imports Tailwind without Preflight, its reset for HTML elements such as h1 and button. A Lynx app has none of them, and the web preview would apply the reset where the device doesn't. The last import adds the Lynx utilities and variants.
ng new --style=tailwind
Running ng add @blotch/angular-lynx in an app made that way keeps its Tailwind 4 and its .postcssrc.json, and replaces @import 'tailwindcss'; with the imports above.
Use utility classes
- Put text classes on the
<text>.text-whiteon the blue<view>wouldn't color its label, because onlyfont-sizeinherits on Lynx by default. See Inheritance. - Rows need
flex flex-row. Elements stack in a column by default, andflex-rowalone does nothing. - Sizes run 12.5% smaller than on the web. Tailwind's scale uses
rem, which is 14px on Lynx. For an exact size, use an arbitrarypxvalue such asw-[44px].- With the UI components' theme,
1remis 16px, so sizes match the web.
- With the UI components' theme,
Lynx utilities and variants
@blotch/angular-lynx/tailwind.css adds what Lynx's Tailwind 3 preset adds for Lynx's own CSS. ng add imports it. Tailwind writes CSS only for the classes you use, so the rest cost nothing.
The solo-* classes each set the whole transform with one function, without Tailwind's variables. Lynx before 3.4 can't animate a variable, so use them to animate a transform there. Two on one element don't combine, and neither do they with translate-* or rotate-*. Each takes a negative (-solo-rotate-45) and an arbitrary value (solo-translate-x-[10px]).
State variants
Tailwind's data-* and aria-* variants work, Tailwind 3's and 4's, with group-*, peer-* and in-*: data-[state=open]:bg-primary, aria-expanded:rotate-90, group-data-[state=open]:text-foreground. Lynx matches no attribute selector, so the build turns each into a class the runtime gives the element while its attribute matches (see Attribute selectors).
The ui-* variants style a class instead of an attribute. Give the element a ui-* class for its state, and style it with the variant of the same name:
-
ui-<state>:applies while the element has theui-<state>class. The states areactive,disabled,readonly,checked,selected,indeterminate,invalid,initial,open,closed,leaving,entering,animating,busy,focused,complete,filledanddragging, plusui-side-{left,right,top,bottom}andui-align-{start,end,center}. -
parent-ui-<state>:applies while the element's direct parent has it. -
Tailwind's
group-*,peer-*andin-*combine with them:group-ui-open:under agroup ui-openancestor,peer-ui-checked:after apeer ui-checkedsibling, and named groups such asgroup-ui-open/menu:. -
For a state of your own, add a variant in
src/styles.css, after the import:src/styles.css
How Tailwind 4 works on Lynx
Tailwind 4 writes CSS for current browsers: oklch() colors, cascade layers, color-mix() for opacity modifiers, padding-inline for px-*, and the translate, rotate and scale properties. Lynx drops all of these, while the web preview, which runs in a browser, shows them.
So the build rewrites the CSS Tailwind writes into CSS with the same meaning that Lynx reads, for the device and the web preview alike:
It also leaves out the properties and values Lynx doesn't have, such as text-transform, so the web preview doesn't show styles the device won't. There's nothing to configure: the build does this for every stylesheet Tailwind 4 compiles.
Dark mode
dark: classes apply while the app is drawn dark, with Tailwind 3 and 4:
- They switch while the app is open, when the phone changes appearance or the app calls
setColorScheme(). See Dark mode. - Keep Tailwind's own
darkvariant. It writes aprefers-color-schemequery, which the build turns into rules underpage.dark. A@custom-variant darkcopied from a web app, such as(&:where(.dark, .dark *)), writes selectors Lynx can't match. Delete it. - With the UI components, you rarely need
dark:. Their classes, such asbg-backgroundandtext-foreground, switch by themselves.
Where Tailwind finds classes
Tailwind 4 scans your project's files for classes itself, .html templates and .ts files alike, and skips what .gitignore lists. To scan a folder outside the project, such as a library in your workspace, add it with @source:
Mix with component styles
Tailwind classes come from the global stylesheet, so they combine with a component's own styles:
The component's own rules are scoped to its template, as on the web. See Component styles.
What doesn't work
These are the gaps you'll notice first:
- The
hover:andfocus:variants don't work on a phone: Lynx has no:hoveror:focus.sm:,md:and the other breakpoints do, against the page's size (see Adaptive layouts), and so doesdark:: see Dark mode. inlineandinline-blockhave no effect. Lynx has no flow layout.overflow-autoandoverflow-scrolldon't scroll a<view>. Use<scroll-view>.uppercase,lowercaseandcapitalizedo nothing. Lynx has notext-transform.- Two filters on one element, such as
blur-sm grayscale, don't combine. Lynx takes one filter function. ring-offset-*doesn't widen a ring, anddrop-shadow-<color>doesn't color a drop shadow.
CSS on Lynx lists every CSS difference.
Tailwind 3
Apps made before Tailwind 4 support use Tailwind 3 with @lynx-js/tailwind-preset, which limits Tailwind's utilities to the CSS Lynx supports and adds the Lynx utilities and variants. They keep working: the build runs Tailwind 3 when it finds a tailwind.config.ts.
To set Tailwind 3 up in a new app, run ng generate @blotch/angular-lynx:add-tailwind --tailwind-version=3. Apps made with an older version have content: ['./src/**/*.ts'], which misses .html templates; ng update @blotch/angular-lynx adds .html to it.
Move to Tailwind 4
-
Run
ng generate @blotch/angular-lynx:add-tailwind --tailwind-version=4. It installs Tailwind 4, writes.postcssrc.jsonand replacessrc/styles.css. -
Move your config's
theme.extendinto@themeinsrc/styles.css, then deletetailwind.config.tsand uninstall@lynx-js/tailwind-preset. -
Rename the utilities Tailwind 4 renamed, such as
shadow-smtoshadow-xs. See Tailwind's upgrade guide. -
If you use the UI components, import the theme in
src/styles.css, after Tailwind's imports. Step 1 replaced the file, and Tailwind 4 takes the theme's classes from itstailwind.css:src/styles.cssKeep them above the
@themefrom step 2: CSS ignores an@importthat comes after other rules. The preset'sui-*andsolo-*classes keep working through@blotch/angular-lynx/tailwind.css. For a state you added withcreateLynxPreset()'suiVariantsoption, add a@custom-variant(see State variants). Then runnpx dolan update. It copiestailwind.cssinto the theme and deletes the Tailwind 3 plugin,tailwind-plugin.ts, with its test. It asks first about a file you edited.
Tailwind's own npx @tailwindcss/upgrade can't read a config that uses the Lynx preset, so remove the preset from presets before running it.
Prettier
prettier-plugin-tailwindcss sorts classes against your stylesheet. With Tailwind 4, point it there:
UI components
The UI components add semantic color utilities such as bg-background and text-foreground. With Tailwind 4 they come from the theme's tailwind.css, which you import after Tailwind; with Tailwind 3, from its Tailwind plugin. They take opacity modifiers, such as bg-primary/50, and switch colors when the page has a dark class. See Theming and Dark mode.
Related
- CSS on Lynx
- Dark mode
- Safe area: the
*-safepadding utilities add-tailwindschematic
