Skip to main content
Table of contents

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/govuk/all";
    
  2. Add your CSS file to your page layout if you need to. For example:

      <head>
        ...
        <link rel="stylesheet" href="<YOUR-CSS-FILE>.css">
      </head>
    
  3. 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/govuk/assets/images folder to <YOUR-APP>/assets/images
  • /node_modules/govuk-frontend/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> section of your page template:

    <script>document.body.className = ((document.body.className) ? document.body.className + ' js-enabled' : 'js-enabled');</script>
    
  2. Copy the /node-modules/govuk-frontend/govuk/all.js file to <YOUR-JAVASCRIPT-FOLDER>/govuk.js.

  3. 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>
      ...
      <script src="<YOUR-JAVASCRIPT-FOLDER>/govuk.js"></script>
      <script>
        window.GOVUKFrontend.initAll()
      </script>
    </body>
    
  4. 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.