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

# CSS Modules

Give a component class names that can't clash with any other component's. With [CSS Modules](https://github.com/css-modules/css-modules), the build turns each class in a `.module.css` file into a unique name, and you apply it from TypeScript.


## When to use them

Reach for CSS Modules when:

- **You pick classes in TypeScript.** The class names are values you can pass around, compute or store.
- **Several components share one stylesheet.** Each imports the same `.module.css` file.

For most components, `styles` or `styleUrl` is simpler. They're scoped to the component too. See [Component styles](/guide/styling/component-styles.md).

## Use a CSS Module

Write ordinary CSS in a file ending in `.module.css`:

```css title="src/app/card/card.module.css"
.card {
  background-color: white;
  border-radius: 12px;
  padding: 16px;
}

.title {
  font-size: 18px;
  font-weight: bold;
  color: #333333;
}
```

Import it into the component, and bind each class with `[class]`:

```typescript title="src/app/card/card.ts"
import { Component } from '@angular/core';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';
import styles from './card.module.css'; // [!code highlight]

@Component({
  selector: 'app-card',
  imports: [LYNX_ELEMENTS],
  template: `
    <view [class]="styles['card']">
      <text [class]="styles['title']">Card title</text>
    </view>
  `,
})
export class Card {
  protected readonly styles = styles; // [!code highlight]
}
```

- **`styles` maps each class to its unique name**, such as `card` to something like `card-a1b2c3`. The build writes the CSS under the unique names.
- **Use brackets, `styles['card']`, not `styles.card`.** The type is an index signature, and Angular's strict TypeScript settings only allow bracket access on those.
- **The component exposes `styles` as a field** because a template can only read the component's own members.

There's nothing to configure. Any file ending in `.module.css` is a CSS Module.

## TypeScript types

Apps created with `npm create angular-lynx` already have the types. They come from `src/rspeedy-env.d.ts`, which `ng add` writes:

```typescript title="src/rspeedy-env.d.ts"
/// <reference types="@blotch/angular-lynx-build/client" />
```

If TypeScript reports `Cannot find module './card.module.css'`, add that file.

## Compared with component styles

|                    | Component styles (`styles`, `styleUrl`)                                     | CSS Modules                        |
| ------------------ | --------------------------------------------------------------------------- | ---------------------------------- |
| Class names        | As written                                                                  | Unique, generated by the build     |
| Scoping            | To the component's own template, with `Emulated` encapsulation, the default | Each name exists only in its file  |
| How you apply them | `class="title"`                                                             | `[class]="styles['title']"`        |
| Share across files | Copy the CSS, or move it to the global stylesheet                           | Import the same `.module.css` file |

## Related

- [CSS on Lynx](/guide/styling/index.md)
- [Tailwind CSS](/guide/tailwindcss.md)
- [CSS Modules example](/examples/css-modules.md)
