Pro plan
Same class names as the card beside it.
Components
Scoped styles namespace a component's CSS at build time, keeping its selectors and animations from leaking into the rest of the page.
Two components sit side by side on the same page. Their markup is identical: both use .card, .tag, and .title, and both style a bare p. Each still keeps its own look, because Bascik rewrites every selector into its component's namespace. Neither stylesheet reaches the other card or the page around them.
Same class names as the card beside it.
Same class names, its own styles.
<plan-card></plan-card>
<alert-card></alert-card> <!-- src/components/plan-card/plan-card.html -->
<article class="card">
<span class="tag">plan-card</span>
<h3 class="title">Pro plan</h3>
<p>Same class names as the card beside it.</p>
</article>
<!-- src/components/alert-card/alert-card.html -->
<article class="card">
<span class="tag">alert-card</span>
<h3 class="title">Disk almost full</h3>
<p>Same class names, its own styles.</p>
</article> /* src/components/plan-card/plan-card.css */
.card {
padding: 24px;
border-radius: 16px;
background: #1d2612;
border: 1px solid rgba(211, 255, 141, 0.35);
}
.tag {
display: inline-block;
padding: 2px 10px;
border-radius: 999px;
background: #d3ff8d;
color: #16210a;
font-size: 0.72rem;
font-weight: 700;
}
.title {
margin: 12px 0 6px;
font-size: 1.3rem;
color: #d3ff8d;
}
p {
margin: 0;
color: #c9d6b5;
}
/* src/components/alert-card/alert-card.css */
.card {
padding: 20px;
background: #2a1712;
border-left: 4px solid #ff8a5c;
}
.tag {
display: inline-block;
padding: 2px 8px;
border: 1px solid #ff8a5c;
color: #ff8a5c;
font-family: ui-monospace, monospace;
font-size: 0.7rem;
}
.title {
margin: 12px 0 6px;
font-family: ui-monospace, monospace;
font-size: 0.95rem;
letter-spacing: 0.08em;
text-transform: uppercase;
color: #ff8a5c;
}
p {
margin: 0;
color: #e6c4b8;
font-style: italic;
} <article class="bascik__plan-card__card">
<span class="bascik__plan-card__tag">plan-card</span>
<h3 class="bascik__plan-card__title">Pro plan</h3>
<p class="bascik__plan-card__el__p">Same class names as the card beside it.</p>
</article>
<article class="bascik__alert-card__card">
<span class="bascik__alert-card__tag">alert-card</span>
<h3 class="bascik__alert-card__title">Disk almost full</h3>
<p class="bascik__alert-card__el__p">Same class names, its own styles.</p>
</article> /* Hoisted into the page <head>, one block per component */
.bascik__plan-card__card {
padding: 24px;
border-radius: 16px;
background: #1d2612;
border: 1px solid rgba(211, 255, 141, 0.35);
}
.bascik__plan-card__tag {
display: inline-block;
padding: 2px 10px;
border-radius: 999px;
background: #d3ff8d;
color: #16210a;
font-size: 0.72rem;
font-weight: 700;
}
.bascik__plan-card__title {
margin: 12px 0 6px;
font-size: 1.3rem;
color: #d3ff8d;
}
.bascik__plan-card__el__p {
margin: 0;
color: #c9d6b5;
}
.bascik__alert-card__card {
padding: 20px;
background: #2a1712;
border-left: 4px solid #ff8a5c;
}
.bascik__alert-card__tag {
display: inline-block;
padding: 2px 8px;
border: 1px solid #ff8a5c;
color: #ff8a5c;
font-family: ui-monospace, monospace;
font-size: 0.7rem;
}
.bascik__alert-card__title {
margin: 12px 0 6px;
font-family: ui-monospace, monospace;
font-size: 0.95rem;
letter-spacing: 0.08em;
text-transform: uppercase;
color: #ff8a5c;
}
.bascik__alert-card__el__p {
margin: 0;
color: #e6c4b8;
font-style: italic;
} You can define component CSS in either of these places:
<style> tags inside the component HTML.css file next to the component HTMLBoth approaches are fully equivalent in functionality. At build time, Bascik extracts inline <style> tags from component HTML files, applies the full scoping pipeline, deduplicates the CSS across all instances on the page, and injects the compiled styles into the page <head>. Component markup in the <body> stays clean with no <style> tags left behind.
<!-- src/components/site-card.html -->
<style>
.card { padding: 20px; border: 1px solid #3a3d40; }
</style>
<article class="card">
<div data-bascik-slot></div>
</article>
<!-- src/pages/index.html -->
<!DOCTYPE html>
<html lang="en">
<body>
<site-card><p>Card content</p></site-card>
</body>
</html>
<!-- dist/index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.bascik__site-card__card { padding: 20px; border: 1px solid #3a3d40; }
</style>
</head>
<body>
<article class="bascik__site-card__card">
<p>Card content</p>
</article>
</body>
</html> If a component contains multiple <style> tags or both a paired .css file and inline <style> tags, Bascik combines all stylesheets before scoping.
Readability & Maintainability: While Bascik supports multiple <style> tags in a single component file, using multiple <style> tags (or mixing an inline <style> tag with a companion .css file) is not recommended for readability and maintainability. Choose one stylesheet pattern per component.
Use paired files for most components so the HTML and CSS stay easy to scan. Inline <style> tags are convenient when keeping a small component in a single file is preferred.
Scoped styles are defined in a .css file with the same name as the component, placed in the same directory:
src/components/
site-nav/
site-nav.html
site-nav.css ← scoped to site-nav Class names are scoped to the component type so every instance of the same component shares identical scoped class names. This is intentional: it allows Bascik to emit a single <style> block per component regardless of how many times it appears on the page.
ID and name attributes are scoped per component instance so multiple instances on the same page always have distinct DOM identifiers.
class → bascik__<componentName>__<originalName>
id → bascik__<componentName>__<instanceId>__<originalName>
name → bascik__<componentName>__<instanceId>__<originalName> In production builds (minify.identifiers: true, the default), these verbose names are hashed to short alphanumeric strings such as b2Y4G9eD1K8b for name compression. The HTML, CSS, and JavaScript are all rewritten with the same hashed names so they stay in sync. This is entirely a build-time transformation with no runtime overhead.
This component combines a class, an ID selector, bare h3 and p selectors, a local custom property, a media query, and a keyframe animation. Inspect Source and Output to see Bascik rewrite them while the meter sweeps in the preview.
Bare element selectors, keyframes, and custom properties stay inside this component.
<scope-lab /> <section class="scope-lab">
<div>
<h3>Scoped selectors are active</h3>
<p>Bare element selectors, keyframes, and custom properties stay inside this component.</p>
</div>
<span class="scope-lab-meter" id="signal" aria-hidden="true"></span>
</section> .scope-lab {
--signal-color: #d3ff8d;
display: flex;
flex-direction: column;
gap: 16px;
}
.scope-lab-meter {
height: 4px;
background: var(--signal-color);
transform-origin: left center;
animation: scope-sweep 1.6s ease-in-out infinite alternate;
}
#signal {
border-radius: 999px;
}
h3 { color: #f0f1f2; }
p { color: #8d929e; }
@keyframes scope-sweep {
from { transform: scaleX(0.15); }
to { transform: scaleX(1); }
}
@media (prefers-reduced-motion: reduce) {
.scope-lab-meter { animation: none; }
} <section class="bascik__scope-lab__scope-lab">
<div>
<h3 class="bascik__scope-lab__el__h3">Scoped selectors are active</h3>
<p class="bascik__scope-lab__el__p">Bare element selectors stay local.</p>
</div>
<span class="bascik__scope-lab__scope-lab-meter bascik__scope-lab__id__signal"
id="bascik__scope-lab__a1b2__signal" aria-hidden="true"></span>
</section> .bascik__scope-lab__scope-lab {
--bascik__scope-lab__signal-color: #d3ff8d;
}
.bascik__scope-lab__scope-lab-meter {
background: var(--bascik__scope-lab__signal-color);
animation: bascik__scope-lab__keyframe__scope-sweep 1.6s ease-in-out infinite alternate;
}
.bascik__scope-lab__id__signal {
border-radius: 999px;
}
.bascik__scope-lab__el__h3 { color: #f0f1f2; }
.bascik__scope-lab__el__p { color: #8d929e; }
@keyframes bascik__scope-lab__keyframe__scope-sweep {
from { transform: scaleX(0.15); }
to { transform: scaleX(1); }
}
@media (prefers-reduced-motion: reduce) {
.bascik__scope-lab__scope-lab-meter { animation: none; }
} Every class name in the .css file is prefixed with a unique instance ID. The corresponding HTML attributes are updated to match.
Both demos on this page show the source class names under Source and their rewritten names under Output.
Class names in a component's HTML template are only scoped if they are declared in the component's stylesheet (.css file or inline <style> block).
If a class name is not defined in the component's CSS, Bascik leaves it unscoped. This allows global utility classes (such as skip-link, flex, hidden, or global design tokens) defined in your global stylesheet (e.g. src/css/styles.css) to pass through and match normally across any component.
This applies to every component, including one with no stylesheet at all. A header or footer that relies entirely on a global stylesheet needs no .css file and no placeholder comment: none of its classes are defined by the component, so none are scoped. The same holds for class names a component's script passes to classList, querySelector, or className.
A component without a stylesheet has no class isolation. Scoping exists to keep a component's own rules from colliding with other components. With no rules of its own, there is nothing to isolate, so a script that queries .item in such a component matches every .item on the page. Add a stylesheet that defines the class to scope it.
<!-- src/components/site-nav.html -->
<a href="#main-content" class="skip-link dnav-logo">Skip to main content</a> /* src/components/site-nav.css */
/* .dnav-logo is declared locally → Bascik scopes it */
.dnav-logo {
display: inline-flex;
align-items: center;
} /* src/css/styles.css (global stylesheet) */
/* .skip-link is NOT in site-nav.css → Bascik leaves it unscoped */
.skip-link {
position: absolute;
top: -100px;
}
.skip-link:focus {
top: 16px;
} <!-- dist/index.html (compiled output) -->
<a href="#main-content" class="skip-link bascik__site-nav__dnav-logo">
Skip to main content
</a> site-nav.css declares .dnav-logo, but not .skip-link
You write
<a class="skip-link dnav-logo"> Bascik emits
<a class="skip-link bascik__site-nav__dnav-logo"> Bare element selectors like p {} or h2 {} are converted to generated class selectors and injected onto matching elements inside the component.
The selector and animation lab above uses this exact pattern. Its Source CSS contains bare selectors; its Output tabs show the generated classes in both CSS and HTML.
Isolation guarantee: Element styles only affect elements inside the component. A p {} rule in my-comp.css will never affect <p> tags on the page or in other components.
Media queries work normally. Class names inside them are scoped like any other rule:
The selector and animation lab includes a prefers-reduced-motion media query. Open Output → CSS to see its .scope-lab-meter selector rewritten inside the unchanged @media wrapper.
Keyframe names are also prefixed so animations from different components never collide:
The sweeping meter in the lab is driven by @keyframes scope-sweep. Open Output → CSS to see the scoped keyframe name and rewritten animation declaration.
CSS #id selectors are converted to scoped class selectors, and the generated class is injected onto the matching element in the HTML. This means #btn {} in a component is fully isolated, the same ID name in another component or on the page produces a completely different selector.
The lab styles #signal. Output → CSS shows its generated class selector; Output → HTML shows that class injected beside the per-instance scoped id.
Specificity note: Converting #id to a class drops specificity from (0,1,0,0) to (0,0,1,0). [id] and [id="…"] attribute-selector forms are stripped at compile time, use a class selector instead.
Bascik rewrites fragment-only url(#id) values when the ID is declared in the same component. This supports SVG gradients, masks, filters, clip paths, markers, fills, strokes, and other CSS properties that accept a local fragment URL:
<svg>
<defs><linearGradient id="brand-gradient"></linearGradient></defs>
<rect class="brand-shape"></rect>
</svg> .brand-shape {
fill: url(#brand-gradient);
} Quoted fragments such as url('#brand-gradient') and url("#brand-gradient") work the same way. Bascik leaves real URLs such as url(/images/shape.svg), data URLs, remote URLs, and cross-document fragments such as url(sprite.svg#icon) byte-identical. Fragment rewriting runs before later base-path URL processing, so the two transforms do not claim the same value.
IDs are scoped per component instance. Under the default deduplicateCss: true, one shared stylesheet cannot point to two different instance IDs. Bascik therefore emits per-instance CSS only for components containing a resolvable CSS fragment reference. This is the correct option because sharing the referenced ID across instances would emit invalid duplicate IDs. Components without CSS fragment references continue to deduplicate normally. With deduplicateCss: false, the existing per-instance CSS path handles fragments without any additional opt-out.
References to IDs inside a preserved subtree remain literal. Use Preserve Scoping when another tool owns the SVG ID graph.
--var-name declarations in a component's CSS are automatically scoped. All var(--var-name) references within the same file are updated to match, so custom properties stay isolated to their component.
The lab declares --signal-color and uses it for the animated indicator. Its Output CSS shows both the scoped declaration and rewritten var() reference.
Only locally-declared properties are scoped. If a component uses var(--global-var) but doesn't declare --global-var in its own CSS, that reference is left untouched so it still resolves from a global stylesheet.
Declared in the component CSS
You write
.card {
--signal-color: #d3ff8d;
background: var(--signal-color);
} Bascik emits
.bascik__brand-card__card {
--bascik__brand-card__signal-color: #d3ff8d;
background: var(--bascik__brand-card__signal-color);
} Declared in a global stylesheet
You write
.card {
background: var(--card-bg);
} Bascik emits
.bascik__brand-card__card {
background: var(--card-bg);
} Define your design tokens once in a global stylesheet, then consume them inside any component. Because the component never declares those properties locally, Bascik leaves the var() references as-is and they resolve from the global stylesheet at render time.
/* src/styles.css: design tokens, linked in every page <head> */
:root {
--brand: #d3ff8d;
--card-bg: #1e2022;
--text-muted: #8d929e;
} <!-- src/components/brand-card.html -->
<style>
.card {
padding: 24px 28px;
background: var(--card-bg); /* global: Bascik leaves untouched */
border-top: 3px solid var(--brand);
border-radius: 10px;
}
.card-label {
color: var(--text-muted);
}
</style>
<div class="card">
<p class="card-label" data-bascik-prop-label></p>
<div data-bascik-slot></div>
</div> /* dist/ output: class names scoped, var() refs preserved */
.bascik__brand-card__card {
padding: 24px 28px;
background: var(--card-bg);
border-top: 3px solid var(--brand);
border-radius: 10px;
}
.bascik__brand-card__card-label {
color: var(--text-muted);
} The Example Components use this pattern: each reads tokens such as var(--color-surface, ...) with a fallback, so it works with or without a token stylesheet. The Modal Dialog shows html:has() for a scroll lock, and the Popover Menu shows anchor positioning without anchor-name.
Bascik supports standard CSS @import statements in component CSS (.css files and inline <style> blocks):
@import "./tokens.css", @import "../shared/theme.css") are resolved and inlined recursively at build time. All selectors, custom properties, and keyframes inside imported stylesheets are automatically scoped to the component instance.@import "https://fonts.googleapis.com/...") are preserved and automatically hoisted to the top of the compiled stylesheet per W3C CSS specifications.layer(...), supports(...), and media query lists attached to @import rules are fully preserved and wrapped around the inlined CSS./* @import "…" not found */ comment. The rules after it are still scoped and emitted.@import text inside a CSS comment or a quoted string is left as written.directory.pages, directory.components, or pipeline.watchPaths).Attribute scoping can be controlled in bascik.config.ts. The following is an illustrative reference showing the defaults, so do not add it unless a project needs to disable one of these options:
import { defineConfig } from '@bascik/bascik/config';
export default defineConfig({
scoping: {
attributes: {
class: true, // scope class names
id: true, // scope id attributes
name: true, // scope name attributes
},
},
}); MDN reference. Scoped CSS changes how selectors are rewritten at build time, but the CSS you write is still normal CSS. Use MDN's CSS reference as the primary source for selectors, at-rules, and properties.
Because all instances of the same component share the same scoped class names (for CSS deduplication), document.querySelector('.myClass') inside a component script will always return the first matching element on the page, not necessarily the element belonging to the current instance. If you have multiple instances of the same component, each instance's script will target the same (first) element.
The correct pattern is to use an id attribute as your root anchor. id attributes are scoped per-instance, so getElementById always finds exactly the right element:
<!-- my-comp.html -->
<div id="root" class="wrapper">
<button id="btn">Click</button>
</div>
<script>
const root = document.getElementById('root'); // ✓ unique per instance
const btn = root.querySelector('button'); // ✓ scoped to this instance
</script> Avoid this anti-pattern:
<div class="wrapper">
<button class="btn">Click</button>
</div>
<script>
// ✗ finds the FIRST .wrapper on the page - wrong for instance 2+
const root = document.querySelector('.wrapper');
</script> By default, all instances of the same component share identical scoped class names so Bascik can emit a single <style> block per component, regardless of how many times it appears on the page. If you genuinely need class selectors to be unique per instance (for example, to use querySelector safely across multiple instances), set scoping.deduplicateCss to false:
import { defineConfig } from '@bascik/bascik/config';
export default defineConfig({
scoping: {
deduplicateCss: false, // each instance gets its own unique class names
},
}); deduplicateCss Trade-Off ComparisonSetting scoping.deduplicateCss in bascik.config.ts controls whether class names are scoped per component type or per component instance.
Go deeper. To understand how Bascik collects, scopes, and compiles CSS blocks at the parser level, check out the CSS Deduplication internals guide.
| Feature / Aspect | deduplicateCss: true (Default) | deduplicateCss: false |
|---|---|---|
| Class Scoping Scheme | bascik__card__wrapper (shared per component) | bascik__card__a1b2c3d4__wrapper (unique per instance) |
| CSS Payload | Single <style> block per component type, zero CSS duplication | Multiplied <style> blocks (one block per component instance) |
querySelector('.cls') Behavior | Targets the first instance on the page | Targets the matching element inside that specific instance |
| Instance Isolation Model | Use id + getElementById() for per-instance script isolation | Class selectors inherently isolate per instance |
| Best For | Virtually all production sites and design systems | Migrating legacy or third-party code that relies on class queries |
Given two instances of <my-card> on the same page:
<!-- Input Page HTML -->
<my-card></my-card>
<my-card></my-card> Output with deduplicateCss: true (Default, Shared Class Scope):
<!-- HTML Output: Shared classes, unique IDs -->
<div class="bascik__my-card__wrapper" id="bascik__my-card__a1b2c3d4__root">...</div>
<div class="bascik__my-card__wrapper" id="bascik__my-card__e5f6g7h8__root">...</div>
<!-- CSS Output: Emitted once in <head> -->
<style>
.bascik__my-card__wrapper { padding: 1rem; }
</style> Output with deduplicateCss: false (Per-Instance Class Scope):
<!-- HTML Output: Unique classes per instance -->
<div class="bascik__my-card__a1b2c3d4__wrapper" id="bascik__my-card__a1b2c3d4__root">...</div>
<div class="bascik__my-card__e5f6g7h8__wrapper" id="bascik__my-card__e5f6g7h8__root">...</div>
<!-- CSS Output: Emitted for every instance -->
<style>
.bascik__my-card__a1b2c3d4__wrapper { padding: 1rem; }
.bascik__my-card__e5f6g7h8__wrapper { padding: 1rem; }
</style> Using the id-based pattern with getElementById() is recommended because it gives you per-instance JS isolation while keeping deduplicateCss: true for minimal CSS payload.
Bascik's built-in CSS scoping pipeline is ultra-fast, zero-dependency, and lightweight. It handles selector scoping, custom property isolation, and whitespace minification out of the box.
Thanks to Bascik's BYOMinifier (Bring Your Own Minifier) feature, if your project requires browser vendor prefixing (such as -webkit- prefixes for Safari cross-browser compatibility) or advanced CSS transformations, you can plug custom CSS processors like PostCSS with Autoprefixer or LightningCSS directly into minify.css in bascik.config.ts.
// bascik.config.ts
import { defineConfig } from '@bascik/bascik/config';
import autoprefixer from 'autoprefixer';
import postcss from 'postcss';
export const build = defineConfig({
minify: {
css: async (css) => {
const result = await postcss([autoprefixer]).process(css, { from: undefined });
return result.css;
},
},
}); // bascik.config.ts
import { defineConfig } from '@bascik/bascik/config';
import { transform } from 'lightningcss';
export const build = defineConfig({
minify: {
css: (css) => {
const { code } = transform({
filename: 'style.css',
code: Buffer.from(css),
minify: true,
targets: { safari: (15 << 16) },
});
return code.toString();
},
},
}); When configured, Bascik runs your custom minify.css transformer on all scoped component <style> blocks, inlined global stylesheets, and static .css files during bascik --build and bascik --server.
This guide focuses on the CSS behavior you write and observe. The compiler implementation is documented separately.
Under the hood. Read the Scoping System internals for the scoping passes, generated attribute maps, selector transformations, and deduplication model.