Custom selectors
Utility classes cover most styling. When you need a real selector — descendant or child combinators, attributes, :has, :not or a pseudo-element — build it with the typed selector builder and attach declarations with ApplySelector.
Why a selector builder
Utilities produce single-class selectors such as
.p-4. Component styles often need more:
.card > h2,
input[type="text"],
.card:has(> img) or
.prose h2::before. The selector builder lets you
write those with typed calls instead of hand-written strings, then registers the declarations
through the same registry the utilities use.
ApplySelector
The ApplySelector overload takes two lambdas: the
first builds the selector, the second declares the styles with
IOnlyStyleBuilder — the same builder used by
Hover and Focus.
Css.ApplySelector(
s => s.Class("btn").Hover(),
css => css.Px(4).Py(2).Background(Color.Blue600).Color(Color.White));
// generated
.btn:hover { padding-inline: 1rem; padding-block: 0.5rem; ... }
A string overload, ApplySelector(selector, css => ...),
is the manual escape hatch: pass any selector text and it is emitted exactly as written, without
escaping or validation. Use it for selectors the typed builder does not cover. The
ApplyClass and
ApplyId helpers are thin sugar over it.
// Manual selector text, emitted exactly as written.
Css.ApplySelector(".card > h2, .card > h3", css => css.Mt(0));
Css.ApplySelector("dialog::backdrop", css => css.Background(Color.Black));
// Sugar: ApplyClass prefixes '.', ApplyId prefixes '#'.
Css.ApplyClass("docs-pill", css => css.Px(3).Py(1));
Css.ApplyId("hero", css => css.Py(12));
The chain reads in the same order the selector is emitted: media, theme, target, then pseudo.
A selector that ends with a combinator such as
Child() returns to the target stage, so
.prose > h2 is
s.Class("prose").Child().Element("h2").
s => s.Media("(min-width: 48rem)").Dark().Class("card").Hover()
// @media (min-width: 48rem) { .dark .card:hover }
s => s.Class("prose").Child().Element("h2").Hover()
// .prose > h2:hover
s => s.Class("a").AdjacentSibling().Class("b")
// .a + .b
s => s.Class("a").GeneralSibling().Class("b")
// .a ~ .b
Compound selectors
Classes, ids, attributes and pseudo-classes all belong to the same compound and can be chained
in any order. Element adds the type selector, and
Universal adds *.
s => s.Class("btn").Class("primary").Hover() // .btn.primary:hover
s => s.Class("btn").Id("submit") // .btn#submit
s => s.Element("input").Attribute("type", "text") // input[type="text"]
s => s.Class("a").Child().Universal() // .a > *
s => s.Element("li").NthChild("2n+1") // li:nth-child(2n+1)
Attributes
Attribute has three shapes: presence, equality and an
explicit operator with an optional case flag. Class names are escaped automatically, and a
leading digit is hex-escaped so Class("2xl") becomes
the valid selector .\32 xl.
| Utility | Generated CSS |
|---|---|
Attribute("required") |
[required] |
Attribute("type", "text") |
[type="text"] |
Attribute("href", "https", AttributeOperator.Prefix) |
[href^="https"] |
Attribute("href", ".pdf", AttributeOperator.Suffix) |
[href$=".pdf"] |
Attribute("class", "note", AttributeOperator.Includes) |
[class~="note"] |
Attribute("lang", "en", AttributeOperator.HyphenPrefix) |
[lang|="en"] |
Attribute("data-x", "y", AttributeOperator.Contains) |
[data-x*="y"] |
Attribute("data-state", "open", AttributeOperator.Exact, AttributeCase.Insensitive) |
[data-state="open" i] |
Selector lists
Targets builds a comma-separated selector list. Every
target shares the surrounding media and theme, and each target is written with the normal fluent
chain.
Css.ApplySelector(
s => s.Media("(min-width: 48rem)").Dark().Targets(
t => t.Class("btn").Hover(),
t => t.Class("card"),
t => t.Class("panel").Child().Class("title")),
css => css.P(6).RoundedLg());
// @media (min-width: 48rem) {
// .dark .btn:hover, .dark .card, .dark .panel > .title { ... }
// }
Functional pseudo-classes
Not, Is and
Where accept a selector list, just like
Targets. Has
accepts relative selectors, so a :has(...) argument may
start with a combinator.
s => s.Class("input").Not(
t => t.Class("disabled"),
t => t.Attribute("readonly"))
// .input:not(.disabled, [readonly])
s => s.Element("h2").Is(t => t.Class("title"), t => t.Id("main"))
// h2:is(.title, #main)
s => s.Class("card").Has(r => r.Child().Element("img"))
// .card:has(> img)
s => s.Class("card").Has(
r => r.Child().Element("img"),
r => r.AdjacentSibling().Class("title").Hover())
// .card:has(> img, + .title:hover)
Pseudo-elements
A keyword pseudo-element such as Before ends the
selector. Functional pseudo-elements such as ::part(...)
return to the compound stage, so a pseudo-class can follow.
s => s.Class("btn").Before() // .btn::before
s => s.Class("quote").After() // .quote::after
s => s.Element("my-button")
.FunctionalPseudoElement("part", "button")
.Hover() // my-button::part(button):hover
A registered component
The pill below is a class registered once at startup. Its base rule comes from
ApplyClass; the hover rule is built with the fluent
selector builder. Applied rules are emitted before utility rules, so a plain utility class can
still override a component style.
// once, at startup
Css.ApplyClass("docs-pill", css => css
.InlineBlock().Px(3).Py(1).RoundedFull()
.TextXs().FontBold()
.Background(Color.Blue100).Color(Color.Blue700));
Css.ApplySelector(
s => s.Class("docs-pill").Hover(),
css => css.Background(Color.Blue200).Color(Color.Blue800));
// usage
<span class="docs-pill">Registered with ApplySelector</span>
Good to know
Applied rules come first
ApplySelector are emitted before
the utility rules, following component/utility layering. This means a utility class always wins
over an applied selector when both have the same specificity.
Media sorting
@media, @supports,
@container) is emitted verbatim with the declarations
nested inside it. It is not part of the breakpoint sorting that utility media rules get.
Startup, not render
ApplySelector on every render would append the same
rule repeatedly.
One registration per process
ApplySelector writes to
ClassRegistry.DefaultRegistry, which lives in the
current process. An interactive WebAssembly app runs your components twice: once on the server
while prerendering and again in the browser. Put applied-selector registrations in code that runs
on both — a shared static constructor or module initializer — not only in a server-only startup
path such as Program.cs or a hosted service. Utilities
are registered as components render, so they are present on both sides automatically; only applied
selectors need this.