Skip to main content
Table of contents

Import CSS, assets and JavaScript


Import all the CSS

To import all the Sass rules from GOV.UK Frontend, add the following to your Sass file:

@import "node_modules/govuk-frontend/govuk/all";

Import specific parts of the CSS

If you want to improve how quickly your service’s pages load in browsers, you can import only the Sass rules you need.

  1. Import node_modules/govuk-frontend/govuk/base in your Sass file.
  2. Import the parts of the CSS you need.

For example, add the following to your Sass file to import the CSS you need for a basic GOV.UK page.

@import "node_modules/govuk-frontend/govuk/base";

@import "node_modules/govuk-frontend/govuk/core/all";
@import "node_modules/govuk-frontend/govuk/objects/all";
@import "node_modules/govuk-frontend/govuk/components/footer/index";
@import "node_modules/govuk-frontend/govuk/components/header/index";
@import "node_modules/govuk-frontend/govuk/components/skip-link/index";
@import "node_modules/govuk-frontend/govuk/utilities/all";
@import "node_modules/govuk-frontend/govuk/overrides/all";

You can remove lines that import parts of the CSS you do not need.

Read more about the different parts of GOV.UK Frontend’s CSS.

You do not need /index at the end of your component imports if you’re using Dart Sass, LibSass 3.6.0 or higher, or Ruby Sass 3.6.0 or higher.

Import an individual component’s CSS using a single import

You can also import a component and all its dependencies without importing node_modules/govuk-frontend/govuk/base first.

To import the button component for example, add the following to your Sass file:

@import "node_modules/govuk-frontend/govuk/components/button/button";

Simplify Sass import paths

If you want to make Sass import paths shorter, add node_modules/govuk-frontend to either your:

You can then import without using node_modules/govuk-frontend/ in your import path. For example:

@import "govuk/components/button/button";

Override with your own CSS

If you want to override GOV.UK Frontend’s styles with your own styles, @import GOV.UK Frontend’s styles before your own Sass rules.

Using GOV.UK Frontend with our old frameworks

If your project uses GOV.UK Frontend toolkit, GOV.UK Template or GOV.UK Elements, you can configure GOV.UK Frontend to work with them.

Font and image assets

To use the font and image assets from GOV.UK Frontend, you can either:

  • serve the assets from the GOV.UK Frontend assets folder - recommended
  • copy the font and image files into your application

Set up your routing so that requests for files in <YOUR-SITE-URL>/assets are served from /node_modules/govuk-frontend/govuk/assets.

For example if you’re using express.js, add the following to your app.js file:

var path = require('path');
app.use('/assets', express.static(path.join(__dirname, '/node_modules/govuk-frontend/govuk/assets')))

Copy the font and image files into your application

If you decide to copy the assets instead, copy the:

  • /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

You should use an automated task or your build pipeline to copy the files, so your project folder stays up to date when we update GOV.UK Frontend.

If you have your own folder structure

If you use a different folder structure than <YOUR-APP>/assets/images and <YOUR-APP>/assets/images, you can set Sass variables so that Sass builds the CSS to point to your folders.

Set one of the following before the @import line in your Sass file:

  • $govuk-assets-path
  • $govuk-images-path and $govuk-fonts-path

Set the $govuk-assets-path variable if your font and image folders have the same parent folder. For example:

$govuk-assets-path: '/<YOUR-ASSETS-FOLDER>';

Set the $govuk-images-path and $govuk-fonts-path variables if your font and image folders have different parent folders. For example:

$govuk-images-path: "/<YOUR-IMAGES-FOLDER>/";
$govuk-fonts-path: "/<YOUR-FONTS-FOLDER>/";

You can also use your own function to generate paths, for example if you’re using asset-pipeline in sass-rails. Set the $govuk-image-url-function and $govuk-font-url-function variables to the name of your function.


To import the JavaScript from GOV.UK Frontend, you can either:

  • add GOV.UK Frontend’s JavaScript file to your HTML
  • import the JavaScript using a bundler like Webpack

Add the JavaScript file to your HTML

If you decide to add the JavaScript to your HTML, first either:

  • set up your routing so that requests for the JavaScript file are served from node_modules/govuk-frontend/govuk/all.js
  • copy the node_modules/govuk-frontend/govuk/all.js file into your application

Then import the JavaScript file before the closing </body> tag of your HTML page or page template, and run the initAll function to initialise all the components.

  <script src="<YOUR-APP>/<YOUR-JS-FILE>.js"></script>

Import JavaScript using a bundler

If you decide to import using a bundler, use import to import all of GOV.UK Frontend’s components, then run the initAll function to initialise them:

import { initAll } from 'govuk-frontend'

If you’re using a bundler that uses CommonJS like Browserify, you should use require:

const GOVUKFrontend = require('govuk-frontend')

Select and initialise an individual component

You can select and initialise a specific component by using its data-module attribute. For example, initialise the first radio component on a page using govuk-radios:

    var Radios = window.GOVUKFrontend.Radios
    var $radio = document.querySelector('[data-module="govuk-radios"]')
    if ($radio) {
      new Radios($radio).init()

Select and initialise part of a page

If you update a page with new markup, for example a modal dialogue box, you can run initAll with a scope parameter to initialise the components on part of a page.

For example:

    var $modal = document.querySelector('.modal')
      scope: $modal

If your JavaScript is not working properly

If your site has a Content Security Policy (CSP), the CSP may block the inline JavaScript in the page template. You may see a warning similar to the following in the developer tools in your browser:

Refused to execute inline script because it violates the following Content Security Policy directive: "default-src 'self'".

You can unblock the JavaScript by including the following hash in your CSP:


You do not need to make any changes to the HTML.

You can read guidance about editing CSP files on the MDN website.