Back to top
About this component
The back to top link works on desktop and on mobile content items. It appears when the user is interacting with a long page of content. It’s a shortcut that allows users to navigate back to the top of the page.
We designed our back to top component so that:
- it appears when the user begins to scroll down the page
- it is fixed in the lower-right of the browser window
- the link text says what the component does in plain English
- the background of the link is semi-transparent, so that it does not completely cover content beneath
- the link is small so it does not cover important content on the page but large enough that the touch area can be easily tapped on mobile
When to use this component
Use the component on pages where the main purpose of scrolling up the page is to access common features found at the top of the page (for example, search or top level navigation).
It is particularly useful on any long content pages.
Performance data
On mygov.scot users of mobile devices click the back to top link the most (92% of clicks). It is clicked more often on longer pages with an average read time of 2 minutes 22 seconds.
Best practice
We referred to the best practice guidance for back to top links from the Nielsen Norman Group when developing this component.
Website analytics
You can track back to top link clicks through the original page path and a data attribute describing the action.
The Design System’s ‘tracking’ script adds the data attribute.
Accessibility
We designed the back to top component to be accessible to all users. It uses the states and described on our accessibility page and our focus states page.
The back to top link is focusable and users can trigger it with the enter key, like any other link. It will take users back to the top of the current page.
Implementation
There are two parts to a back to top implementation:
- the back to top component itself
- a target element that the back to top link will scroll the user to
The target element needs to have an id
attribute of page-top
.
You should place the back to top component after the article content but above the footer.
You should place the target element after any site-wide notification banners. This means that screen reader users will not encounter that content again when they use the link.
The component uses JavaScript to calculate its bottom position, immediately above the footer.
Set up a ‘back to top’ by creating a new BackToTop
object. It takes three parameters:
- the DOM element of the back to top button
- the window object
- an optional object of customisation settings
The customisation settings can have the following property:
footerElSelector
: a CSS selector for the footer element. If this is not specified, the component will use the default of.ds_site-footer
.
Example JavaScript
const backToTop = new window.DS.components.BackToTop(
document.getElementById('my-back-to-top'),
window,
{
footerElSelector: '.my-footer-element'
}
);
backToTop.init();
Live example
Marriage in Scotland (mygov.scot)
Feedback, help and support
If you need help or support you can e-mail us at designsystem@gov.scot
There is a problem
Thanks for your feedback