Skip to main content

Get started

Get one GOV.UK Frontend component working in your application, so you can test everything works before you add more components or styles.

You will need to do all the following steps to get your component working.

  1. Install GOV.UK Frontend.
  2. Add the HTML for a component to your application.
  3. Get the CSS working.
  4. Get the font and images working.
  5. Get the JavaScript working.

1. Install

Install GOV.UK Frontend using npm.

If you’ve installed using precompiled files, get started with a basic page instead.

2. Add the HTML for a component to your application

Go to the example accordion component on the GOV.UK Design System website, then copy the HTML.

Paste the HTML into a page or template in your application.

3. Get the CSS working

  1. Add the following to the main Sass file in your project, so your Sass compiler adds all of GOV.UK Frontend’s styles to your CSS file.
   @import "node_modules/govuk-frontend/dist/govuk/all";
  1. Add your CSS file to your page layout if you need to. For example:
   <head>
     <!-- // ... -->
     <link rel="stylesheet" href="<YOUR-STYLESHEETS-FOLDER>/<YOUR-CSS-FILE>.css">
   </head>
  1. Run your application and check that the accordion displays correctly.

The accordion will use a generic font until you get the font and images working, and will not be interactive until you get the JavaScript working.

There are also different ways you can import GOV.UK Frontend’s CSS.

4. Get the font and images working

Your component will not use the right font or images until you’ve added GOV.UK Frontend’s assets to your application.

  1. Copy the following 2 folders:
  • /node_modules/govuk-frontend/dist/govuk/assets/images folder to <YOUR-APP>/assets/images
  • /node_modules/govuk-frontend/dist/govuk/assets/fonts folder to <YOUR-APP>/assets/fonts
  1. Run your application, then use the Fonts tab in Firefox Page Inspector to check the accordion is using the GDS Transport font.

In your live application, we recommend using an automated task or your build pipeline instead of copying the files manually.

5. Get the JavaScript working

  1. Add the following to the top of the <body class="govuk-template__body"> section of your page template:
   <script>document.body.className += ' js-enabled' + ('noModule' in HTMLScriptElement.prototype ? ' govuk-frontend-supported' : '');</script>
  1. Copy the /node_modules/govuk-frontend/dist/govuk/govuk-frontend.min.js file into your application.

  2. Import the file before the closing </body> tag of your page template, then run the initAll function to initialise all the components. For example:

   <body class="govuk-template__body">
     ...
     <script type="module" src="<YOUR-JAVASCRIPTS-FOLDER>/govuk-frontend.min.js"></script>
     <script type="module">
       import { initAll } from '<YOUR-JAVASCRIPTS-FOLDER>/govuk-frontend.min.js'
       initAll()
     </script>
   </body>
  1. Run your application and check it works the same way as the Design System accordion example, by selecting the buttons and checking the accordion shows and hides sections.

In your live application:

  • you must use initAll to initialise all components that use GOV.UK Frontend’s JavaScript, or some components will not work correctly for disabled users who use assistive technologies
  • we recommend using an automated task or your build pipeline instead of copying the files manually

You can now get the full code for page layouts and other components from the Design System website.