Styling
Marko integrates CSS directly into components through inline <style> tags, auto-discovered stylesheets, CSS Modules, and explicit imports.
Inline Styles
Marko enhances <style> so styles are extracted, deduplicated, and optimized by the project's bundler. A template may include any number of <style> tags.
By default, styles are globally scoped. Projects using global styles often follow a naming convention such as BEM to prevent collisions.
<style>
/* Bundled and loaded once */
.fancy {
color: green;
}
</style>
<div class="fancy">Hello!</div>style
--
/* Bundled and loaded once */
.fancy {
color: green;
}
--
div class="fancy" -- Hello!Append a file extension to <style> to use CSS preprocessors such as SCSS or Less:
<style.scss>
$primary-color: green;
.fancy-scss {
color: $primary-color;
}
</style>
<div class="fancy-scss">Hello!</div>
<style.less>
@primary-color: blue;
.fancy-less {
color: @primary-color;
}
</style>
<div class="fancy-less">Hello!</div>style.scss
--
$primary-color: green;
.fancy-scss {
color: $primary-color;
}
--
div class="fancy-scss" -- Hello!
style.less
--
@primary-color: blue;
.fancy-less {
color: @primary-color;
}
--
div class="fancy-less" -- Hello!Inline CSS Modules
Adding a tag variable to <style> compiles it as a CSS Module, scoping class names automatically and exposing the class map as an object:
<style/styles>
.foo { border: 1px solid red; }
.bar { background: green; }
</style>
<div class=styles.foo/>
<div class=[styles.foo, styles.bar]/>
<div class={ [styles.bar]: true }/>
<div.${styles.foo}/>style/styles
--
.foo { border: 1px solid red; }
.bar { background: green; }
--
div class=styles.foo
div class=[styles.foo, styles.bar]
div class={ [styles.bar]: true }
div.${styles.foo}File extensions combine with CSS Modules for preprocessor support:
<style.scss/styles>
$primary-color: green;
.fancy {
color: $primary-color;
}
</style>
<div class=styles.fancy>Hello</div>style.scss/styles
--
$primary-color: green;
.fancy {
color: $primary-color;
}
--
div class=styles.fancy -- HelloDynamic Values
A <style> tag accepts ${...} interpolations so reactive values can live alongside static rules:
<let/hue=200>
<style>
.swatch {
/* Updates whenever `hue` changes */
background: hsl(${hue} 80% 50%);
border-radius: 4px;
}
</style>
<input type="range" min="0" max="360" value:parseFloat:=hue>
<div class="swatch"/>let/hue=200
style
--
.swatch {
/* Updates whenever `hue` changes */
background: hsl(${hue} 80% 50%);
border-radius: 4px;
}
--
input type="range" min="0" max="360" value:parseFloat:=hue
div class="swatch"The stylesheet is still extracted and bundled once. Each interpolation compiles to a CSS custom property updated by Marko as state changes.
Dynamic values work only in CSS declaration value positions and apply to subsequent sibling elements and their descendants. See <style> Dynamic Values for details.
Per-Item Values
Because dynamic values apply to the siblings following each <style> tag, placing one inside a <for> scopes the interpolated values to each iteration:
<nav class="brands">
<for|brand| of=input.brands>
<style>
.brand {
color: ${brand.color};
}
</style>
<a class="brand" href=brand.url>${brand.name}</a>
</for>
</nav>nav class="brands"
for|brand| of=input.brands
style
--
.brand {
color: ${brand.color};
}
${" "}
--
a class="brand" href=brand.url -- ${brand.name}Each iteration renders a <style> element among the items, so :nth-child counts those elements and misses its targets. Use :nth-of-type instead, which counts only elements of the same tag.
Auto-Discovered Styles
A stylesheet adjacent to any .marko template is discovered automatically and bundled identically to an inline <style>. Name it style.<ext> next to index.marko, or <name>.style.<ext> next to <name>.marko.
.fancy {
color: green;
}<div class="fancy">Hello!</div>div class="fancy" -- Hello!Moving styles into an adjacent file keeps large templates concise without adding explicit imports.
Imported Styles
Stylesheets can also be imported explicitly, which is useful when sharing styles across templates:
.fancy {
color: red;
}import "./fancy.css";
<div class="fancy">Hello!</div>import "./fancy.css";
div class="fancy" -- Hello!Imported CSS Modules
External CSS Module files (*.module.css) export their class map as the default export:
.fancy {
color: red;
}import styles from "./something.module.css";
<div class=styles.fancy/>import styles from "./something.module.css";
div class=styles.fancyVite and most modern bundlers support *.module.css out of the box.
Contributors
Helpful? You can thank these awesome people! You can also edit this doc if you see any issues or want to improve it.