States and selectors
Put a selector in front of the declaration to style a state, a pseudo-element, an attribute, or another element.
States and pseudo-elements
A CSS pseudo-class or pseudo-element is written with -- in place of : or :::
--hover-c-red
--focus-visible-outline-2px-solid-blue
--disabled-o-0.5
--before-cont
--placeholder-c-slateTheme9Generated CSS
.--hover-c-red:hover {
color: red;
}
.--focus-visible-outline-2px-solid-blue:focus-visible {
outline: 2px solid blue;
}
.--disabled-o-0\.5:disabled {
opacity: 0.5;
}
.--before-cont::before {
content: "";
}
.--placeholder-c-slateTheme9::placeholder {
color: light-dark(#8b8d98,#696e77);
}The same pattern covers --active, --checked, --open, --popover-open, --first-child, --last-child, --after, --marker and the rest. Combine a state and a pseudo-element by putting the state first:
--hover--before-c-redGenerated CSS
.--hover--before-c-red:hover::before {
color: red;
}The other order, --before--hover, compiles to ::before:hover, which never matches.
Selectors that take arguments
--nth-child(2n+1)-bgc-slateTheme2
--not(--first-child)-margin-top-8px
--has(--checked)-bgc-greenTheme3Generated CSS
.--nth-child\(2n\+1\)-bgc-slateTheme2:nth-child(2n+1) {
background-color: light-dark(#f9f9fb,#18191b);
}
.--not\(--first-child\)-margin-top-8px:not(:first-child) {
margin-top: 8px;
}
.--has\(--checked\)-bgc-greenTheme3:has(:checked) {
background-color: light-dark(#e6f6eb,#132d21);
}- Anything longer than one word goes in parentheses:
--not(--first-child). A one-word argument can skip them:--not--hover. - Inside the parentheses, a bare word is an element:
--has(nav),--is(h1,h2),--not(p). A leading_is allowed too (--has(_nav)), and__means a direct child:--has(__nav)is:has(> nav). - States and attributes work inside:
--has(input--checked),--has(img[alt]),--not(--has(nav)). --is(…)and--where(…)work the same way.
Attribute selectors
Write the attribute in brackets, without quotes. Quotes are never allowed in class names.
[aria-expanded=true]-c-red
[data-open]-d-block
[aria-current=page][fw-600,c-accentRATheme1100]Generated CSS
.\[aria-expanded\=true\]-c-red[aria-expanded="true"] {
color: red;
}
.\[data-open\]-d-block[data-open] {
display: block;
}
.\[aria-current\=page\]\[fw-600\,c-accentRATheme1100\][aria-current="page"] {
font-weight: 600;
color: light-dark(oklch(from var(--accentColor,oklch(1 .25049 284.23)) 46.9058% c h),oklch(from var(--accentColor,oklch(1 .25049 284.23)) 72.3297% c h));
}Other elements
Combinators style elements around the one with the class. The number of underscores picks the combinator:
| Write | Selects |
|---|---|
_li-c-red |
every descendant li |
__li-c-red |
direct children: > li |
___p-c-red |
the next sibling: + p |
____p-c-red |
every later sibling: ~ p |
_all-m-0, __all-m-0 |
every descendant or child: * |
_.(card-title)-c-red |
descendant .card-title |
_ul__li--hover-c-red |
ul > li:hover |
Class names that contain - go in parentheses: _.(card-title). Without them the dash is read as the start of the property.
_li-c-red
__li-c-red
___p-c-red
_.(card-title)-c-redGenerated CSS
._li-c-red li {
color: red;
}
.__li-c-red > li {
color: red;
}
.___p-c-red + p {
color: red;
}
._\.\(card-title\)-c-red .card-title {
color: red;
}Style from a parent: &
Everything before & becomes a selector for an ancestor of the element:
_.dark&c-white
[data-theme=dark]&c-white
[data-open]&[o-1,tf-none]Generated CSS
.dark ._\.dark\&c-white {
color: white;
}
[data-theme="dark"] .\[data-theme\=dark\]\&c-white {
color: white;
}
[data-open] .\[data-open\]\&\[o-1\,tf-none\] {
opacity: 1;
transform: none;
}This is the main tool for component state. Put one data- or aria- attribute on the component’s root, let every child read it with [attribute=value]&…, and let JavaScript change only the attribute:
<div data-state="on" class="d-flex aic g-12px">
<span class="w-36px h-20px br-999px p-2px bgc-slateTheme6 [data-state=on]&bgc-greenTheme9 d-flex">
<span class="w-16px h-16px br-50% bgc-white [data-state=on]&tf-translateX(16px)"></span>
</span>
<span class="fs-14px c-slateTheme11 [data-state=on]&c-slateTheme12">Notifications</span>
</div>Generated CSS
.d-flex {
display: flex;
}
.aic {
align-items: center;
}
.g-12px {
gap: 12px;
}
.w-36px {
width: 36px;
}
.h-20px {
height: 20px;
}
.br-999px {
border-radius: 999px;
}
.p-2px {
padding: 2px;
}
.bgc-slateTheme6 {
background-color: light-dark(#d9d9e0,#363a3f);
}
[data-state="on"] .\[data-state\=on\]\&bgc-greenTheme9 {
background-color: light-dark(#30a46c,#30a46c);
}
.w-16px {
width: 16px;
}
.h-16px {
height: 16px;
}
.br-50\% {
border-radius: 50%;
}
.bgc-white {
background-color: white;
}
[data-state="on"] .\[data-state\=on\]\&tf-translateX\(16px\) {
transform: translateX(16px);
}
.fs-14px {
font-size: 14px;
}
.c-slateTheme11 {
color: light-dark(#60646c,#b0b4ba);
}
[data-state="on"] .\[data-state\=on\]\&c-slateTheme12 {
color: light-dark(#1c2024,#edeef0);
}root.dataset.state = on ? 'on' : 'off'; // that's all the styling logic
Browser parts
Each browser applies its own prefixed parts: --webkit-slider-thumb, --moz-range-thumb, --webkit-progress-value, --moz-progress-bar, --scrollbar-thumb, --file-selector-button and --details-content.