Setup - Production code

The code you need to start building user interfaces for NHS websites and services.

Include the NHS.UK frontend library in your project

To start using the NHS website styles, components and patterns contained here, you’ll need to include the NHS.UK frontend library in your project.

There are 2 ways to do this. You can either install it using node package manager (npm) or include the compiled files in your application.

Option 1: install using npm

We recommend installing the NHS.UK frontend library using npm.

Using this option, you will be able to:

  • selectively include the CSS or JavaScript for individual components
  • build your own styles or components based on the palette or typography and spacing mixins
  • customise the build (for example, overriding colours or enabling global styles)
  • use the component Nunjucks templates

Option 2: include compiled files

If your project does not use npm, or if you want to try out the NHS.UK frontend library in your project without installing it through npm, you can download and include compiled stylesheets, JavaScript and the asset files.

Using this option, you will be able to include all the CSS and JavaScript of the NHS.UK frontend library in your project.

You will not be able to:

  • selectively include the CSS or JavaScript for individual components
  • build your own styles or components based on the palette or typography and spacing mixins
  • customise the build, for example, overriding colours or enabling global styles
  • use the component Nunjucks templates

Styling page elements

The service manual provides CSS classes for styling content, instead of global styles.

The class names follow the Block Element Modifier (BEM) naming convention. This can look a bit daunting at first, but it makes robust code that's easy to maintain.

Explore the Styles section of the service manual to see what classes are available.

Using components

The components in the service manual are designed to be accessible and responsive. There are 2 ways to implement them in your application.

You can either use HTML or, if you're using Nunjucks with node.js and you installed the NHS.UK frontend library using npm, you can use a Nunjucks Macro.

You can get the code from the HTML or Nunjucks tabs below the examples on our component pages, like this example of a button.

Open this button example in new window
Copy
<button class="nhsuk-button" type="submit">
  Save and continue
</button>
Copy
{% from 'button/macro.njk' import button %}

{{ button({
  "text": "Save and continue"
}) }}

Using Nunjucks macros

A Nunjucks macro is a simple template that generates more complex HTML.

Nunjucks macros save you time by managing repetitive or error-prone tasks, like linking form labels to their controls.

Nunjucks macros also make it easier to keep your application up to date. You can run a command to update component code instead of having to manually update your HTML.

To use Nunjucks macros in your application, you’ll need to setup Nunjucks views to point to the location of NHS.UK frontend components, which is node_modules/nhsuk-frontend/packages/components/.

To include a specific component macro in your page template, you need to import the macro.

For example, to use the button macro, use the import statement % from 'node_modules/nhsuk-frontend/packages/components/button/macro.njk' import button %}.

If you’re using Nunjucks macros in production, be aware that using html arguments or ones ending with html can be a security risk. The Nunjucks templating documentation has guidance on how to mitigate the risks.

Keeping your code up to date

We update the NHS.UK frontend library from time to time. Check for recent releases in the NHS.UK frontend changelog.

Need help?

If you’ve got a question about the NHS digital service manual or want to feedback, get in touch.

Updated: January 2020