Styling
A guide to styling Sveltery Base components with your preferred styling engine.
Sveltery Base components are unstyled, don't bundle CSS, and are compatible with Tailwind, CSS Modules, CSS-in-JS, or any other styling solution you prefer. You retain total control of your styling layer.
Style hooks
CSS classes
Components that render an HTML element accept a class prop to style the element with CSS classes.
The prop also accepts any Svelte class value, such as an array or an object. Base UI's className can also be a function of the component's state. Sveltery Base's class cannot, so style each state through its data attribute.
Data attributes
Components provide data attributes designed for styling their states. For example, Switch can be styled using its [data-checked] and [data-unchecked] attributes, among others.
CSS variables
Components expose CSS variables to aid in styling, often containing dynamic numeric values to be used in sizing or transform calculations. For example, Popover exposes CSS variables on its Popup component like --available-height and --anchor-width.
Check out each component's API reference for a complete list of available data attributes and CSS variables.
Style prop
Components that render an HTML element accept a style prop to style the element with a CSS string.
Base UI's style can also be a function of the component's state. Sveltery Base's style cannot. To style an element from the component's state, render the element yourself with a render snippet, which receives the state.
Tailwind CSS
Apply Tailwind CSS classes to each part via the class prop.
CSS Modules
Apply custom CSS classes to each part via the class prop.
Then style those classes in a CSS Modules file. Vite supports CSS Modules in Svelte components.
Svelte style blocks
Svelte scopes the rules in a component's <style> block to the elements in that component's markup. A part renders its element inside the library's component, so a scoped selector such as .Item does not reach it, and Svelte warns that the selector is unused. Wrap the selector in :global(...):
Elements that you write in a render snippet belong to your component's markup, so scoped rules apply to them without :global.
CSS-in-JS
Svelte has no styled() wrapper for components. Generate a class for each part with a framework-agnostic CSS-in-JS library, such as Emotion's @emotion/css, then apply it via the class prop.