Components

Cookie banner
Experimental

This component is currently experimental because more research is needed to validate it.

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

  <div class="app-tabs__heading js-tabs__heading"><a class="app-tabs__heading-link" href="#default-cookie-banner-example-nunjucks" aria-controls="default-cookie-banner-example-nunjucks" data-track="tab-nunjucks">Nunjucks</a></div>
<div class="app-tabs__container js-tabs__container" id="default-cookie-banner-example-nunjucks" role="tabpanel">
    

    <details class="govuk-details app-options" data-module="govuk-details" id="options-default-cookie-banner-example-details">
Nunjucks macro options
      <p>
      Use options to customise the appearance, content and behaviour of a component when using a macro, for example, changing the text.
      </p>
      <p>
      Some options are required for the macro to work; these are marked as "Required" in the option description.
      </p>
      <p>
      If you're using Nunjucks macros in production with "html" options, or ones ending with "html", you must sanitise the HTML to protect against  <a href="https://developer.mozilla.org/en-US/docs/Glossary/Cross-site_scripting">cross-site scripting exploits</a>.
      </p>
      
        <table class="govuk-table app-options__table" id="options-default-cookie-banner-example--primary">
          <caption class="govuk-table__caption govuk-heading-m  govuk-visually-hidden">Primary options</caption>
          <thead class="govuk-table__head">
            <tr class="govuk-table__row">
              <th class="govuk-table__header app-options__limit-table-cell" scope="col">Name</th>
              <th class="govuk-table__header app-options__limit-table-cell" scope="col">Type</th>
              <th class="govuk-table__header" scope="col">Description</th>
            </tr>
          </thead>
          <tbody class="govuk-table__body">
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">ariaLabel</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                  The text for the <code>aria-label</code> which labels the cookie banner region. This region applies to all messages that the cookie banner includes. For example, the cookie message and the confirmation message. Defaults to &#39;Cookie banner&#39;.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">hidden</th>
                <td class="govuk-table__cell ">boolean</td>
                <td class="govuk-table__cell ">
                  
                  Defaults to <code>false</code>. If you set this option to <code>true</code>, the whole cookie banner is hidden, including all messages within the banner. You can use <code>hidden</code> for client-side implementations where the cookie banner HTML is present, but hidden until the cookie banner is shown using JavaScript.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">classes</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                  The additional classes that you want to add to the cookie banner.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">attributes</th>
                <td class="govuk-table__cell ">object</td>
                <td class="govuk-table__cell ">
                  
                  The additional attributes that you want to add to the cookie banner. For example, data attributes.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">messages</th>
                <td class="govuk-table__cell ">array</td>
                <td class="govuk-table__cell ">
                  
                    <strong>Required.</strong>
                  
                  The different messages you can pass into the cookie banner. For example, the cookie message and the confirmation message.
                  
                  
                    See <a href="#options-default-cookie-banner-example--messages">messages</a>.
                  </td>
              </tr>
            
          </tbody>
        </table>
      
        <table class="govuk-table app-options__table" id="options-default-cookie-banner-example--messages">
          <caption class="govuk-table__caption govuk-heading-m ">Options for messages</caption>
          <thead class="govuk-table__head">
            <tr class="govuk-table__row">
              <th class="govuk-table__header app-options__limit-table-cell" scope="col">Name</th>
              <th class="govuk-table__header app-options__limit-table-cell" scope="col">Type</th>
              <th class="govuk-table__header" scope="col">Description</th>
            </tr>
          </thead>
          <tbody class="govuk-table__body">
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">headingText</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                  The heading text that displays in the message. You can use any string with this option. If you set <code>headingHtml</code>, <code>headingText</code> is ignored.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">headingHtml</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                  The heading HTML to use within the message. You can use any string with this option. If you set <code>headingHtml</code>, <code>headingText</code> is ignored. If you are not passing HTML, use <code>headingText</code>.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">text</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                    <strong>Required.</strong>
                  
                  The text for the main content within the message. You can use any string with this option. If you set <code>html</code>, <code>text</code> is not required and is ignored.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">html</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                    <strong>Required.</strong>
                  
                  The HTML for the main content within the message. You can use any string with this option. If you set <code>html</code>, <code>text</code> is not required and is ignored. If you are not passing HTML, use <code>text</code>.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">actions</th>
                <td class="govuk-table__cell ">array</td>
                <td class="govuk-table__cell ">
                  
                  The buttons and links that you want to display in the message. <code>actions</code> defaults to <code>button</code> unless you set <code>href</code>, which renders the action as a link.
                  
                  
                    See <a href="#options-default-cookie-banner-example--actions">actions</a>.
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">hidden</th>
                <td class="govuk-table__cell ">boolean</td>
                <td class="govuk-table__cell ">
                  
                  Defaults to <code>false</code>. If you set it to <code>true</code>, the message is hidden. You can use <code>hidden</code> for client-side implementations where the confirmation message HTML is present, but hidden on the page.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">role</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                  Set <code>role</code> to <code>alert</code> on confirmation messages to allow assistive tech to automatically read the message. You will also need to move focus to the confirmation message using JavaScript you have written yourself.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">classes</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                  The additional classes that you want to add to the message.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">attributes</th>
                <td class="govuk-table__cell ">object</td>
                <td class="govuk-table__cell ">
                  
                  The additional attributes that you want to add to the message. For example, data attributes.
                  
                  </td>
              </tr>
            
          </tbody>
        </table>
      
        <table class="govuk-table app-options__table" id="options-default-cookie-banner-example--actions">
          <caption class="govuk-table__caption govuk-heading-m ">Options for actions</caption>
          <thead class="govuk-table__head">
            <tr class="govuk-table__row">
              <th class="govuk-table__header app-options__limit-table-cell" scope="col">Name</th>
              <th class="govuk-table__header app-options__limit-table-cell" scope="col">Type</th>
              <th class="govuk-table__header" scope="col">Description</th>
            </tr>
          </thead>
          <tbody class="govuk-table__body">
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">text</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                    <strong>Required.</strong>
                  
                  The button or link text.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">type</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                  The type of button. You can set <code>button</code> or <code>submit</code>. Set <code>button</code> and <code>href</code> to render a link styled as a button. If you set <code>href</code>, it overrides <code>submit</code>.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">href</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                  The <code>href</code> for a link. Set <code>button</code> and <code>href</code> to render a link styled as a button.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">name</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                  The name attribute for the button. Does not apply if you set <code>href</code>, which makes a link.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">value</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                  The value attribute for the button. Does not apply if you set <code>href</code>, which makes a link.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">classes</th>
                <td class="govuk-table__cell ">string</td>
                <td class="govuk-table__cell ">
                  
                  The additional classes that you want to add to the button or link.
                  
                  </td>
              </tr>
            <tr class="govuk-table__row">
                <th class="govuk-table__header" scope="row">attributes</th>
                <td class="govuk-table__cell ">object</td>
                <td class="govuk-table__cell ">
                  
                  The additional attributes that you want to add to the button or link. For example, data attributes.
                  
                  </td>
              </tr>
            
          </tbody>
        </table>
  <div class="app-example__code">
    <pre data-module="app-copy" tabindex="0"><code class="hljs js">{% <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;govuk/components/cookie-banner/macro.njk&quot;</span> <span class="hljs-keyword">import</span> govukCookieBanner %}

{% set html %} <p class="govuk-body">We use some essential cookies to make this service work.</p> <p class="govuk-body">We’d also like to use analytics cookies so we can understand how you use the service and make improvements.</p> {% endset %} {{ govukCookieBanner({ ariaLabel: "Cookies on [name of service]", messages: [ { headingText: "Cookies on [name of service]", html: html, actions: [ { text: "Accept analytics cookies", type: "button", name: "cookies", value: "accept" }, { text: "Reject analytics cookies", type: "button", name: "cookies", value: "reject" }, { text: "View cookies", href: "#" } ] } ] }) }}