Tailwind CSS
Style Lynx elements with Tailwind CSS utility classes. AngularLynx supports Tailwind CSS v3 through @lynx-js/tailwind-preset, which limits Tailwind's utilities to the CSS that Lynx supports.
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 does three things:
- Installs
tailwindcss@3and@lynx-js/tailwind-presetas dev dependencies. - Writes
tailwind.config.tswith the Lynx preset. - Replaces
src/styles.css(orsrc/styles.scss) with Tailwind's directives. Copy out any global styles you want to keep first.
There's nothing to add to angular.json. The build turns Tailwind on when it finds a tailwind.config.ts.
Set it up by hand
-
Install Tailwind v3 and the preset:
-
Create
tailwind.config.tsin the project root:tailwind.config.ts -
Add the directives to your global stylesheet:
src/styles.css
The schematic leaves out @tailwind components, whose rules Lynx can't fully parse.
The Lynx preset works with Tailwind v3. Tailwind v4 isn't supported, so don't install @tailwindcss/postcss or @tailwindcss/vite, which are v4 packages.
Use utility classes
- Put text classes on the
<text>.text-whiteon the blue<view>wouldn't color its label, because CSS doesn't inherit on Lynx. 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].
Write classes in inline templates
.html templates
Tailwind only finds classes written in inline templates (template:) for now. Classes that appear only in a separate .html template get no CSS.
During ng build and ng serve, AngularLynx hands Tailwind the files your app bundles, in place of content. Those are your .ts files. An .html template is read by the Angular compiler, so Tailwind never sees it.
ng generate component makes an .html template by default. For components styled with Tailwind, generate an inline one:
To make that the default, set it in angular.json:
The schematic writes content: ['./src/**/*.ts']. Change it to ./src/**/*.{html,ts}, as in the manual setup, so editor tools that read content see your .html templates too.
Mix with component styles
Tailwind classes come from the global stylesheet, so they combine with a component's own styles:
Component styles aren't isolated on Lynx, so give their classes distinctive names, like app-badge. See Component styles.
What doesn't work
The preset drops utilities Lynx can't render. These are the gaps you'll notice first:
- The
hover:,focus:,sm:,md:anddark:variants don't work on a phone. Lynx has no:hoveror:focus, and AngularLynx apps don't support@media. For dark mode, see Dark mode. block,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.
CSS on Lynx lists every CSS difference. The preset's support matrix lists every utility.
UI components
The UI components add semantic color utilities such as bg-background and text-foreground through their own Tailwind plugin. They 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
