Cookie banner

Allow users to accept or reject cookies which are not essential to making your service work.

Open this example in Storybook

When to use this component

Use this component if your website or form sets any cookies on a user’s device.

Remember, you must:

tell users about the cookies your website or form sets on their device

let users accept or reject any cookies that are not essential to providing your service

The term ‘non-essential cookies’ includes:

HTML5 local storage

service workers

any other technologies that store files on the user’s device

This component page shows several options for using a cookie banner, based on the types of cookies you’re using on your website or form.

How it works

Show the cookie banner every time the user accesses your form or website until they either:

accept or reject cookies using the buttons in the cookie banner

save their cookie preferences on the website’s cookies page

Once the user has accepted or rejected cookies, the cookie banner should:

hide the cookie banner message

show a confirmation message – and a ‘hide’ button to let the user close the banner

set a cookie to save the user’s preferences for a set period

Make sure the cookie banner does not:

show when the user visits again, once their preferences have been saved

set any non-essential cookies unless the user accepted them on a previous visit

Position the cookie banner after the opening <body> tag and before the ’skip to main content‘ link. If you’re using the Nunjucks page template, use the bodyStart block.

Do not make the cookie banner ‘sticky’ to the top of the page by using position: fixed or any other method. This is to make sure it does not cover or obscure any content which has a focus applied.

Option 1: If you’re only using essential cookies

You can choose not to have a cookie banner if your website or form only sets essential or ‘strictly necessary’ cookies, as these do not need user consent.

However, you must tell users that you set essential cookies. You can do this with a cookies page linked from the footer.

Option 2: If you set non-essential cookies on the server

You can choose this option if your website or form sets non-essential cookies on the server or on the client.

To get consent from the user, display the cookie banner inside a form that lets them submit their choice to accept or reject cookies. To accept or reject non-essential cookies requires JavaScript.

Here’s an example of a cookie banner inside a form:

Open this example in Storybook

Get the Nunjucks macro for this example from the Camden Frontend library

Once the user has accepted or rejected cookies and set their cookie preferences, reload or update the page to show a confirmation message.

Here’s an example of a confirmation message inside a form:

Open this example in Storybook

Get the Nunjucks macro for this example from the Camden Frontend library

In the cookie banner, tell the user about all the cookies you’re using in the service – whether they’ve enabled JavaScript or not. This way, you will not need to ask the user to give their consent again on their next visit.

If the user is entering information into a form as part of the service, they might lose their place when they submit their choice to accept or reject cookies.

To help users running JavaScript on their device, you can write some JavaScript code to let them submit their choice and prevent the page from reloading.

Include all possible messages that the user could see in the cookie banner when the page loads. Hide these with the hidden HTML attribute where needed.

Option 3: If you set non-essential cookies, but only on the client

You can choose to make your banner only work with JavaScript if your service only needs to set non-essential cookies on the client.

When the page loads, the hidden html attribute hides the component, as well as all the cookie banner messages it contains, which the user might otherwise see.

Show the cookie banner only to users that have enabled JavaScript

Use JavaScript to show cookie banner messages to users that have not accepted or rejected cookies by removing the hidden attribute as needed.

Write your own JavaScript code so that when the user accepts or rejects cookies, the cookie banner will:

hide the cookie message by adding the hidden attribute

show a confirmation message by removing its hidden attribute

give the confirmation message the tabindex="-1" and role="alert" attributes – this will allow the element to be focused so assistive technology can read the message

shift focus to the confirmation message

Here’s an example:

Open this example in Storybook

Get the Nunjucks macro for this example from the Camden Frontend library

Show a confirmation message confirming that the user has either accepted or rejected cookies by removing the hidden attribute.

Open this example in Storybook

Get the Nunjucks macro for this example from the Camden Frontend library

Show a confirmation message confirming that the user has either accepted or rejected cookies by removing the hidden attribute.

Open this example in Storybook

Get the Nunjucks macro for this example from the Camden Frontend library

What to cover in your cookie banner

Include the name of the website or form in the banner heading to help users understand that the cookies you’re talking about are different from the ones set by Camden.gov.uk.

You’ll need to change the example cookie banner text if your website or form:

allows third parties to set cookies (tell the user that both your organisation and other organisations will be setting cookies)

uses cookies for reasons other than collecting analytics information or remembering the user’s settings

Keep the text as short as possible while making sure it’s an accurate description of how you use cookies. For example, if you use more than one ‘functional’ cookie and there’s not enough space to mention what each of them does, you could ask for permission to set cookies so ‘you can use as many of the service’s features as possible’.

If you’re using essential cookies and analytics cookies

You can use this example text for a service which sets essential and analytics cookies. Analytics cookies are those set by your organisation to collect information about how people are using your digital service.

Open this example in Storybook

Get the Nunjucks macro for this example from the Camden Frontend library

Research on this component

This component is based on the cookie banner component from the GOV.UK Design System.

You can take part in the ‘Cookie banner’ discussion on GOV.UK’s Design System’s GitHub and share your research.