Skip to main content

Class Selector

A CSS class selector is used as a generic way to target a DOM element with a matching class name. In Stylable a CSS class is used primarily as a component part, for example a .navBtn of gallery component, but can also be used as utility class or a mixin.

This page goes over how Stylable handles class selector, for more details about the language feature itself, checkout the following resources:


To define a class symbol: set a class selector anywhere in a stylesheet.

Standalone definition

.navBtn {}

In-selector definition

/* compound */
button.navBtn.baseBtn {}

/* multiple selectors */
.navBtn, .thumb {}


The .root class has a special meaning in Stylable stylesheets. Every stylesheet has a root to represent the root element of the component or page of the stylesheet - see root reference.

Import and Export

A class selector can be imported into another stylesheet with the @st-import atrule.

Import class by name

/* reference the navBtn class  */
@st-import [navBtn] from './';

/* reference multiple classes */
@st-import [navBtn, thumb] from './';

/* map imported class to local name "galleryNavBtn" */
@st-import [navBtn as galleryNavBtn] from './';

Import stylesheet root class

/* get the gallery root class (default export) */
@st-import Gallery from './';

/* reference the gallery named root class */
@st-import [root as GalleryRoot] from './';
capitalize default stylesheet import

While any valid ident can be used for the default root import, It is strongly recommend to capitalize the name to signify it represents a component element.


A class can be accessed using the classes mapping on the Stylable stylesheet runtime.

import { classes } from './';

// map from local name to target class name

Use the class mapping to bind the stylesheet to the rendered view:

React example

/* gallery.jsx */
import { st, classes } from './';

function Gallery(props) {
return (
<div className={st(classes.root, props.className)}>
<button className={classes.navBtn}>next</button>
<button className={classes.navBtn}>prev</button>
component instance customization

In the component example above, we call the st() function in order to pass both the gallery root class and any class name provided externally in order to style the component from the outside.


Stylable automatically namespaces any class selector according to the stylesheet it is defined in:

.a {}

/* OUTPUT */

Disable in selector

Wrapping a class selector with :global() pseudo-class prevents namespacing:

:global(.icon-heart) {}

/* OUTPUT */
.icon-heart {}

see global selector reference

Map to global selector

Class selector can be mapped to a custom global selector with the -st-global declaration:

Map to global class

.button {
-st-global: '.myLib__button';
.button:hover {}

/* OUTPUT */
.myLib__button {}
.myLib__button:hover {}

Valid values

/* global selector */
.x { -st-global: '.a' }

/* compound selector of multiple classes */
.x { -st-global: '.a.b' }

/* complex selector (runtime `classes.x` not exported) */
.x { -st-global: '.a .b' }

/* non class selectors (runtime `classes.x` not exported) */
.x { -st-global: '[c]' }
.x { -st-global: '.a[c]' }

Invalid values

/* empty selector */
.x { -st-global: '' }
/* only a single selector is supported */
.x { -st-global: '.a, .b' }