---
title: "Back To Top"
description: "A small floating button for long pages. It shows after you scroll down, its ring fills as you read, and a press goes back to the top."
url: https://gelatinui.com/components/back-to-top
markdown_url: https://gelatinui.com/components/back-to-top.md
access: free
install: "npx shadcn@latest add @gelatin-ui/back-to-top"
registry_item: https://gelatinui.com/r/back-to-top.json
version: "2026-11-17"
index: https://gelatinui.com/llms.txt
docs_index: https://gelatinui.com/docs/llms.txt
---

# Back To Top

> A small floating button for long pages. It shows after you scroll down, its ring fills as you read, and a press goes back to the top.

Page: https://gelatinui.com/components/back-to-top

## When to use it

Best for: Long pages and scroll panels, where a reader needs a fast way back to the top and a sense of place.

- Use it on pages longer than about three screens.
- Use `BackToTopPill` on docs pages, where the label and the read percent help.
- Use `showOn="scroll-up"` when the button should stay out of the way while the reader reads down.
- Use `offset` to lift it above a cookie bar, a bottom bar, or a chat launcher.

## When not to use it

- For a jump to a section of the page, use a [Table of Contents](https://gelatinui.com/components/table-of-contents.md).
- For a read progress bar with no button, use [Scroll Progress](https://gelatinui.com/components/scroll-progress.md).

## Notes for agents

- Place one `BackToTop` per page. With no `container` it floats at the end-bottom corner of the window through a portal.
- For a scroll panel, pass `container` and put `BackToTop` as the last child inside that panel.
- Pass a skip link or the page heading as `focusTarget`. Without it, focus lands on the container or the body.
- At the end of the page the ring closes and the button fills.

## Anatomy

```text
BackToTop
├─ BackToTopButton (0..n)
└─ BackToTopPill (0..n)
```

## API reference

### BackToTop

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `container` | `RefObject<HTMLElement \| null>` |  | The element that scrolls, for a panel or a Scroll Area viewport. Without it the page counts, and the button floats at the end-bottom corner of the window. With it, place `BackToTop` as the last child inside that element: it sticks to its end-bottom corner. |
| `threshold` | `number` | 400 | The scroll in px before it shows. Default 400. |
| `showOn` | `"scroll" \| "scroll-up"` | "scroll" | `scroll`: it shows past the threshold (default). `scroll-up`: only while the reader scrolls back up. |
| `offset` | `number` | 0 | Lifts it by this many px, above a bottom bar, a cookie bar, or a chat launcher. Default 0. |
| `focusTarget` | `RefObject<HTMLElement \| null>` |  | Where focus lands after the scroll: a skip link or the page heading. Default: the container, or the body. |
| `disabled` | `boolean` | false | Hides it and stops the work. |

### BackToTopButton

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `className` | `string` |  |  |
| `busy` | `boolean` |  | The button waits for the app (busy words): a spinner, `aria-busy`, and no clicks. Focus stays on the button. The style hook stays `data-loading`. |
| `spinnerPlacement` | `"center" \| "start"` |  | `center` (default): the spinner covers the label. `start`: it takes the leading icon slot. The icon sizes always use `center`, because their one icon is the label. |
| `glass` | `"clear" \| "dark" \| "light"` |  |  |
| `size` | `"default" \| "lg" \| "sm"` | "default" | `sm` 36px (with a 44px touch target), `default` 44px, or `lg` 52px. |
| `shortcut` | `string \| null` | "Home" | The key the tooltip names. Default "Home"; `null` shows no key. |

Also takes every prop of Base UI `Button`: https://base-ui.com/react/components/button

### BackToTopPill

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `className` | `string` |  |  |
| `busy` | `boolean` |  | The button waits for the app (busy words): a spinner, `aria-busy`, and no clicks. Focus stays on the button. The style hook stays `data-loading`. |
| `spinnerPlacement` | `"center" \| "start"` |  | `center` (default): the spinner covers the label. `start`: it takes the leading icon slot. The icon sizes always use `center`, because their one icon is the label. |
| `glass` | `"clear" \| "dark" \| "light"` |  |  |
| `size` | `"default" \| "icon" \| "icon-lg" \| "icon-sm" \| "icon-xs" \| "lg" \| "sm" \| "xl" \| "xs"` | "default" |  |

Also takes every prop of Base UI `Button`: https://base-ui.com/react/components/button

## Styling

| Part | Slot | States | CSS variables | Tokens |
| --- | --- | --- | --- | --- |
| BackToTop | `back-to-top` |  | `--back-to-top-offset`, `--spacing` |  |
| BackToTopButton | `back-to-top-button` |  | `--foreground` | `hit-area` |
| BackToTopPill | `back-to-top-pill` |  | `--foreground` | `hit-area` |

## Usage

Put `BackToTop` once on a long page with a `BackToTopButton` in it. Pass a skip link as `focusTarget`, so focus lands there after the scroll.

```tsx title="docs-page.tsx"
import { useRef } from "react";
import type { ReactNode } from "react";

import { BackToTop, BackToTopButton } from "@/components/actions/back-to-top";

export function DocsPage({ children }: { children: ReactNode }) {
  const skipLinkRef = useRef<HTMLAnchorElement>(null);
  return (
    <>
      <a ref={skipLinkRef} href="#main" className="fixed start-4 top-2 -translate-y-16 focus:translate-y-0">
        Skip to content
      </a>
      <main id="main" className="mx-auto max-w-2xl px-6 py-10">
        {children}
      </main>
      <BackToTop threshold={600} focusTarget={skipLinkRef}>
        <BackToTopButton aria-label="Back to top" shortcut="Home" />
      </BackToTop>
    </>
  );
}
```

## Compose

Rules:

- Touch targets (`touch-targets`): Give every control a hit area of at least 44 px on touch screens, even when it looks smaller.
- Compose the parts (`compose-parts`): Build each item from its named parts as children, and pass your text and icons in as parts.
- Every state is designed (`every-state`): Give each view of data its loading, empty, and error states: a skeleton in the final layout, an `Empty` with an illustration and one next action, and an error that says what failed and how to retry.

## Add it

```bash
npx shadcn@latest add @gelatin-ui/back-to-top
```

It adds 52 registry items: 48 files, 7480 lines, and the npm packages `@base-ui/react`, `@mantine/hooks`, `@phosphor-icons/react`, `clsx`, `motion`, `tailwind-merge`, `tailwind-variants`.

## Built on

- [Badge](https://gelatinui.com/components/badge.md)
- [Button](https://gelatinui.com/components/button.md)
- [Kbd](https://gelatinui.com/components/kbd.md)
- [Portal](https://gelatinui.com/components/portal.md)
- [Scroll Progress](https://gelatinui.com/components/scroll-progress.md)
- [Tooltip](https://gelatinui.com/components/tooltip.md)

## For AI agents

- Every item, one file: https://gelatinui.com/llms.txt
- MCP server (`search_items`, `get_item`, `get_install_plan`): https://gelatinui.com/mcp

## Related

- [Button](https://gelatinui.com/components/button.md)
- [Table of Contents](https://gelatinui.com/components/table-of-contents.md)
- [Scroll Progress](https://gelatinui.com/components/scroll-progress.md)
- [Anchor](https://gelatinui.com/components/anchor.md)
- [Badge](https://gelatinui.com/components/badge.md)
- [Kbd](https://gelatinui.com/components/kbd.md)

## Keyboard

| Keys | Action |
| --- | --- |
| Enter, Space | On the button: scrolls back to the top, then moves focus to `focusTarget`. |

## Motion

| Part | Motion |
| --- | --- |
| Show and hide | panel · it rises in from below and fades out |
| Scroll back | none · Motion eases the scroll for 250 to 600ms, longer for a longer page; a wheel or a touch stops it |
| Press | press · 100ms · scales to 0.97 and the shadow drops |
| Read progress | none · the ring follows the scroll in each frame |
