Scrollbar
The scrollbar preset is off by default. Enable it with scrollbar: true.
import { presetVinicunca } from '@vinicunca/unocss-preset';
import { defineConfig } from 'unocss';
export default defineConfig({
presets: [
presetVinicunca({
scrollbar: true
})
]
});
<div class="scrollbar scrollbar-rounded scrollbar-w-4px h-40 overflow-auto">
...
</div>
With the default noCompatible: true, scrollbar-color rules emit nothing and scrollbar-width is only written for none. Webkit pseudo-elements still style the bar. CSS variables have no un- prefix (--scrollbar-width, not --un-scrollbar-width).
.scrollbar {
overflow: auto;
--scrollbar-track: #f5f5f5;
--scrollbar-thumb: #ddd;
--scrollbar-width: 8px;
--scrollbar-height: 8px;
--scrollbar-track-radius: 4px;
--scrollbar-thumb-radius: 4px;
}
.scrollbar::-webkit-scrollbar {
width: var(--scrollbar-width);
height: var(--scrollbar-height);
}
.scrollbar::-webkit-scrollbar-track {
background-color: var(--scrollbar-track);
}
.scrollbar::-webkit-scrollbar-thumb {
background-color: var(--scrollbar-thumb);
}
Variants
These prefixes rewrite the selector to a webkit scrollbar pseudo-element:
| Variant | Selector |
|---|---|
scrollbar: | ::-webkit-scrollbar |
scrollbar-track: | ::-webkit-scrollbar-track |
scrollbar-thumb: | ::-webkit-scrollbar-thumb |
The scrollbar shortcut already uses them (scrollbar:scrollbar-width-[var(--scrollbar-width)], scrollbar-thumb:scrollbar-background-color-[var(--scrollbar-thumb)], …). You can use them on any matching utility:
<div class="scrollbar scrollbar-thumb:bg-primary">
Shortcuts
overflow: auto, custom properties, webkit width/height/colors. Firefox scrollbar-color is skipped when noCompatible is true.border-radius from --scrollbar-track-radius and --scrollbar-thumb-radius.--scrollbar-width and --scrollbar-height to 8px. scrollbar-width: thin is only emitted when noCompatible is false.scrollbar-width: none (always) and ::-webkit-scrollbar { display: none } via scrollbar:hidden.Color and size
scrollbar-thumb-color-<color> and scrollbar-track-color-<color> set --scrollbar-thumb / --scrollbar-track from the theme palette.
scrollbar-(radius|w|h|track-radius|thumb-radius)-<n>:
| Token | Sets |
|---|---|
radius | track and thumb radius |
w | width |
h | height |
track-radius | track radius |
thumb-radius | thumb radius |
A unitless number goes through numberToUnit (default: divide by 4 and append rem). A number with a unit is used as-is:
scrollbar-w-4→--scrollbar-width: 1remscrollbar-w-4px→--scrollbar-width: 4px
noCompatible is false, Firefox scrollbar-color is used and webkit scrollbar sizing/radius no longer apply.Options
See Options for every field. Custom numberToUnit:
presetVinicunca({
scrollbar: {
numberToUnit: (value) => `${value}px`
}
});
Then scrollbar-w-4 generates --scrollbar-width: 4px.