# Styles

The `<style>` block is CSS **scoped to the component**: its selectors only match elements written in this component's markup, not the ones of child
components. The compiler scopes selectors at build time and writes the CSS to a static stylesheet, so no style code runs in the browser. A file has one
`<style>` and one `<style global>`, at its top level.

```html
<p>Scoped paragraph, it stays in its lane</p>

<style>
	p { color: steelblue; }
</style>
```

Every compound of a selector gets the scope class of the component, a hash of its module and path. Only the elements a selector can match get the class:
their tag, static classes and ids fit one compound, or they have dynamic or spread attributes. The output is minified, and a site whose components have no
style ships no stylesheet.

## Selectors that match nothing

A selector with a part that matches no element of the markup never applies, and the compiler warns about it. A part stays quiet when the Kotlin of the file
may add its class or id while the page runs: it names it, or reads `classList` or `className`. For elements the markup doesn't write, such as the ones of a
child component, of `@html` or of a `.kt` file that adds classes, write [`:global(...)`](#scope-escapes).

The page itself is never an element of a component. `:root`, `html` and `body` stay scoped like any selector, so the compiler warns about them. Put the
rules of the page in a [`<style global>`](#scope-escapes) block.

## Kotlin values in CSS

`$name` and `${expr}` insert Kotlin values in a style block, like in a Kotlin string template. The value is converted with `toString()`, together with the
text glued to it (`${progress * 100}%` is one value). Several expressions in one `${a, b}` are joined with spaces, without the `null` and blank ones.


```html
<script kotlin>
	var hue by state(30.0)
	var corner by state(14.0)
</script>

<div class="sample">oklch(0.7 0.17 {hue})</div>
<input type="range" min="0" max="360" valueAsNumber={::hue}>
<input type="range" min="0" max="48" valueAsNumber={::corner}>

<style>
	.sample {
		background: oklch(0.7 0.17 $hue);
		border-radius: ${corner}px;
	}
</style>
```

Each value becomes a CSS custom property: the static stylesheet reads it with `var()`, and the component's root elements that hold an element the rule
styles carry its value in their `style` attribute, which the runtime updates when the state changes. Custom properties inherit, so the styled elements see
it, and no style code is generated.

```html
<script kotlin>
	prop val accent: String
	prop val progress: Double
</script>

<div class="bar"><span></span></div>

<style>
	.bar { background: $accent; }
	.bar span { width: ${progress * 100}%; }
</style>
```

```css
.bar.l-x7{background:var(--l-x7-0)}.bar.l-x7 span.l-x7{width:var(--l-x7-1)}
```

```html
<div class="bar l-x7" style="--l-x7-0:#e63946;--l-x7-1:30%"><span class="l-x7"></span></div>
```

When the markup can't tell what a rule styles, every root element carries the value: a nested rule, a rule in an at-rule, `:global(...)`, a class or id the
Kotlin of the file may set, or an element of a `@fun`.

### Constants

Compile-time constants (`const val` of a `.kt` file) are inlined in the stylesheet instead, and they are the only values allowed where CSS custom properties
don't work: selectors, `@media` and `@container` conditions, property names, CSS strings, the descriptors of `@font-face`, `@property`, `@counter-style`
and `@font-palette-values`, and `<style global>`. Any other value there is a compile error.

```kotlin
// Breakpoints.kt
const val TABLET = 768
```

```html
<ul class="grid">
	<li>Lists</li>
	<li>Forms</li>
</ul>

<style>
	@media (width >= ${TABLET}px) {
		.grid { grid-template-columns: repeat(2, 1fr); }
	}
</style>
```

In a Gradle project, the [plugin](/docs/sites/kobweb) doesn't give the compiler the constants yet, so a constant there is a value like any other: a custom
property, and an error where custom properties don't work.

## Scope escapes

`:global(...)` opts a selector out of scoping, to style children or the page. A `<style global>` block is not scoped at all, every selector in it applies to
the whole page, and it goes to the same static stylesheet. `@keyframes` aren't scoped either.

```html
<p>Scoped paragraph</p>
<div class="content">
	<Markdown text={body} />
</div>

<style>
	p { color: steelblue; }
	.content :global(h2) { margin-block: 2rem 1rem; }
	:global(body) { margin: 0; }
</style>

<style global>
	html { color-scheme: light dark; }
	body { font-family: system-ui, sans-serif; }
</style>
```
