Back to top
On this page
Importing
Add rh-back-to-top to your page with this import statement:
<script type="module">
import '@rhds/elements/rh-back-to-top/rh-back-to-top.js';
</script>
Copy to Clipboard
Copied!
Wrap lines
Overflow lines
To learn more about installing RHDS elements on your site using an import map read our getting started docs.
Usage
<div id="overflow">
<p>Scroll down to reveal the back to top element</p>
<rh-back-to-top href="#">Back to top</rh-back-to-top>
</div>
<script type="module">
import '@rhds/elements/rh-back-to-top/rh-back-to-top.js';
</script>
<style>
#overflow {
min-block-size: calc(100dvh + 401px);
}
p {
font-weight: bold;
}
</style>
Copy to Clipboard
Copied!
Wrap lines
Overflow lines
rh-back-to-top
Provides a fixed-position anchor link for scrolling back to the top of
long pages. Appears after a configurable scroll threshold; should be
last in tab order. Renders an <a> element so screen readers announce
it as a navigation link. Keyboard-accessible via Tab and Enter (WCAG 2.1.1).
Avoid placing more than one instance per page.
Slots
1
| Slot Name | Summary | Description |
|---|---|---|
[default]
|
link text content (default slot) |
Text displayed within the back to top button. Defaults to
"Back to top" if not provided. Serves as the ARIA accessible
name for the anchor link (per WCAG 2.5.3 Label in Name).
Use visible, descriptive text so screen readers announce
the link purpose; avoid icon-only content without a label. |
Attributes
4
| Attribute | DOM Property | Description | Type | Default |
|---|---|---|---|---|
visible
|
visible |
Controls the visibility behavior of the back to top button.
Usage guidelines
|
|
|
scrollable-selector
|
scrollableSelector |
CSS selector for the element to monitor for scroll events. When not provided, the component monitors the window's scroll position (default behavior). When provided, monitors the specified element's scroll position instead. Usage guidelines
|
|
|
scroll-distance
|
scrollDistance |
Distance in pixels from the top of the scrollable element to trigger button visibility. The button appears when the user scrolls past this threshold and disappears when scrolling back above it. Default is 400px. Usage guidelines
|
|
|
href
|
href |
Page fragment identifier (anchor) for the target element to scroll to. Must be a hash link pointing to an element ID on the page. The hash symbol (#) is automatically prepended if not provided. Usage guidelines
Accessibility
|
|
|
Methods
0
None
Events
0
None
CSS Shadow Parts
1
| Part Name | Summary | Description |
|---|---|---|
trigger
|
anchor link that triggers scroll to top |
The clickable link element that navigates to the top of the page or specified target. This part can be styled to customize the button's appearance including hover states, borders, shadows, and positioning. The button uses a pill shape with the accent color background and includes both text and an upward caret icon. |
CSS Custom Properties
1
| CSS Property | Description | Default |
|---|---|---|
--rh-back-to-top-background-color |
—
|
Design Tokens
14
| Token | Description | Copy |
|---|---|---|
--rh-space-xl
|
Back-to-top inline-end inset Back-to-top block-end inset 24px spacer |
|
--rh-color-text-primary-on-dark
|
Trigger text color in light mode Primary text color for dark theme |
|
--rh-color-text-primary-on-light
|
Trigger text color in dark mode Primary text color for light theme |
|
--rh-color-accent-base
|
Trigger background color |
|
--rh-space-md
|
Trigger horizontal spacing 8px spacer |
|
--rh-border-radius-pill
|
Trigger border radius Pill border radius; Example: Label |
|
--rh-space-xs
|
Trigger vertical spacing Trigger content gap 4px spacer |
|
--rh-line-height-body-text
|
Trigger line height Line height for body text |
|
--rh-border-width-md
|
Trigger outline width Trigger border width Trigger hover/focus outline width Trigger hover/focus border width 2px border width: Example: Alert |
|
--rh-color-border-strong-on-dark
|
Trigger hover/focus border color in light mode Strong border color (dark theme) |
|
--rh-color-border-strong-on-light
|
Trigger hover/focus border color in dark mode Strong border color (light theme) |
|
--rh-color-interactive-primary-hover
|
Trigger hover/focus outline color Trigger hover/focus background color |
|
--rh-font-size-body-text-xs
|
Trigger text size 12px font size |
|
--rh-space-3xl
|
Back-to-top inline-end inset at viewport >= md Back-to-top block-end inset at viewport >= md 48px spacer |
|
Other libraries
To learn more about our other libraries, visit this page.
Feedback
To give feedback about anything on this page, contact us.
Other libraries
To learn more about our other libraries, visit the getting started page.