Skip to content

[v10] Add code and monospace font utilities#1962

Merged
colinrotherham merged 16 commits into
support/10.xfrom
add-code-modifier-class
Jun 8, 2026
Merged

[v10] Add code and monospace font utilities#1962
colinrotherham merged 16 commits into
support/10.xfrom
add-code-modifier-class

Conversation

@edwardhorsford

@edwardhorsford edwardhorsford commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Description

My service displays NHS number and something called an accession number, and I wanted to display these in a fixed width font. We tried using .nhsuk-input--code but this is meant for text inputs and excessively spaces letters, especially spaces.

Chatting with @colinrotherham it felt like it could be helpful to have a general purpose modifier for this.

I've set it to to:

  • use monospace font (like the .nhsuk-input--code)
  • slightly increase default letter spacing
  • slightly reduce word spacing (width of spaces)

Screenshot:
Screenshot 2026-06-04 at 14 33 13

I also took the opportunity to update the example NHS numbers in the frontend app to the same example from the design system, and one explicitly starting with 9, which is a reserved range.

At the suggestion of @colinrotherham I've made an internal @mixin nhsuk-font-monospace() mixin and used it in this new modifier and .nhsuk-input--code.

Update: We've split out two new sets of utilities:

  • Utility class nhsuk-u-font-code with Sass mixin nhsuk-font-code
  • Utility class nhsuk-u-font-monospace with Sass mixin nhsuk-font-monospace

With changes to the nhsuk-input--code modifier limited to tighter letter and word spacing


Sass deprecations

I've updated this PR to include Sass deprecations to prefer nhsuk-typography-* nhsuk-font-*

We've renamed Sass mixins for font weights. You can still use the previous names but we'll remove them in a future breaking release.

If you use Sass and you've used the following Sass mixins:

  • replace nhsuk-typography-weight-normal with nhsuk-font-weight-normal
  • replace nhsuk-typography-weight-bold with nhsuk-font-weight-bold
  .app-component {
    display: block;
-   @include nhsuk-typography-weight-bold;
+   @include nhsuk-font-weight-bold;
    @include nhsuk-responsive-margin(4, "bottom");
  }

@colinrotherham colinrotherham temporarily deployed to nhsuk-frontend-pr-1962 June 4, 2026 13:34 Inactive

@colinrotherham colinrotherham left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks Ed

I'd forgotten GOV use govuk-!-font-tabular-numbers

i.e. There's no -family- in there

Wondering whether "monospace" fits better in terms of fonts?

- .nhsuk-u-font-family-code {
+ .nhsuk-u-font-monospace {

@edwardhorsford

Copy link
Copy Markdown
Contributor Author

Amusingly I'm the one who first added tabular numbers on GOV.UK.

Happy to try a different name and would prefer something shorter anyway. I guess for us to decide whether to frame it about monospace fonts, or to frame it about the end usage - for codes.

Comment thread packages/nhsuk-frontend/src/nhsuk/core/utilities/_typography.scss
@edwardhorsford edwardhorsford temporarily deployed to nhsuk-frontend-pr-1962 June 4, 2026 14:01 Inactive
@colinrotherham colinrotherham changed the title Add nhsuk-u-font-family-code modifier Add nhsuk-u-font-monospace modifier Jun 4, 2026
@edwardhorsford edwardhorsford temporarily deployed to nhsuk-frontend-pr-1962 June 4, 2026 14:07 Inactive
@edwardhorsford edwardhorsford changed the title Add nhsuk-u-font-monospace modifier Add monospace font modifier Jun 4, 2026
@anandamaryon1

Copy link
Copy Markdown
Contributor

Thanks for opening this @edwardhorsford

I did the input-code spacing and I think I went with the increased spacing as a balance – on different devices the monospace font renders with different fonts.

But I need to check this. Agree on my Mac that it looks OK with less spacing.

In terms of a modifier and/or input-code styling: I think it'd be useful to have both. The input-code having extra letter spacing and the general-purpose monospace having default spacing.

@anandamaryon1

Copy link
Copy Markdown
Contributor

Windows

Before (current):
Screenshot 2026-06-04 at 16 51 36

After (this PR):
Screenshot 2026-06-04 at 16 51 39

So, it looks a bit tighter due to the font, but maybe OK still?

@edwardhorsford

Copy link
Copy Markdown
Contributor Author

@anandamaryon1 we don't have to reduce the letter-spacing like I've done - or indeed could pick a number half way between. But I'd be very keen on reducing the word-spacing so that spaces are not so large / the groups of digits aren't so separated.

@anandamaryon1 anandamaryon1 temporarily deployed to nhsuk-frontend-pr-1962 June 8, 2026 11:17 Inactive
@anandamaryon1

Copy link
Copy Markdown
Contributor

@edwardhorsford I've pushed an update to meet in the middle with the nhsuk-input--code letter spacing and reduce the word-spacing. I've removed these from the basic nhsuk-u-font-monospace helper as when used on non-number content the reduced spacing makes it harder to read.

For example (letter-spacing: 0.075em; word-spacing: -0.5ch;):
image

@anandamaryon1 anandamaryon1 temporarily deployed to nhsuk-frontend-pr-1962 June 8, 2026 11:25 Inactive
@colinrotherham

Copy link
Copy Markdown
Contributor

I'm going to rebase this against support/10.x then we can get it released

@colinrotherham

Copy link
Copy Markdown
Contributor

@anandamaryon1 I'll make one tiny tweak though

Let's keep the font mixin API the same and only support $important

i.e. We can set the custom spacing like GOV.UK Frontend, only where necessary

.nhsuk-input--code {
- @include nhsuk-font-monospace($letter-spacing: 0.075em, $word-spacing: -0.5ch);
+ @include nhsuk-font-monospace;
+ letter-spacing: 0.075em;
+ word-spacing: -0.5ch;
}

@colinrotherham colinrotherham changed the base branch from main to support/10.x June 8, 2026 12:06
@colinrotherham colinrotherham changed the title Add monospace font modifier [v10] Add monospace font modifier Jun 8, 2026
@edwardhorsford

Copy link
Copy Markdown
Contributor Author

@anandamaryon1 Sounds good, happy with this compromise.

@anandamaryon1 anandamaryon1 added this to the v10.5.2 milestone Jun 8, 2026
@colinrotherham colinrotherham force-pushed the add-code-modifier-class branch from ea8aa43 to fddcceb Compare June 8, 2026 13:17
@colinrotherham colinrotherham temporarily deployed to nhsuk-frontend-pr-1962 June 8, 2026 13:17 Inactive
@colinrotherham colinrotherham force-pushed the add-code-modifier-class branch from fddcceb to 0677e07 Compare June 8, 2026 13:26
@colinrotherham colinrotherham temporarily deployed to nhsuk-frontend-pr-1962 June 8, 2026 13:26 Inactive
@colinrotherham

Copy link
Copy Markdown
Contributor

@anandamaryon1 @edwardhorsford Can you check the new changelog edit please?

Noting that "monospace" text should have spaces and characters at the same width I've split out:

  • HTML class nhsuk-u-font-code with Sass mixin nhsuk-font-code
  • HTML class nhsuk-u-font-monospace with Sass mixin nhsuk-font-monospace

Aligning "code" with <code>

You'll also notice that nhsuk-input--code has font-size-adjust: 0.5 applied matching <code> etc

Text using nhsuk-u-font-code

@anandamaryon1

Copy link
Copy Markdown
Contributor

Aligning "code" with <code>

You'll also notice that nhsuk-input--code has font-size-adjust: 0.5 applied matching <code> etc

Ah, I was expecting the font-size-adjust: 0.5 to be on nhsuk-u-font-monospace but not nhsuk-input--code. On the input it can't be mixed with other text so if anything bigger is better, for legibility

Comment thread CHANGELOG.md Outdated
@colinrotherham colinrotherham force-pushed the add-code-modifier-class branch from 0677e07 to 80e8498 Compare June 8, 2026 14:59
@colinrotherham colinrotherham temporarily deployed to nhsuk-frontend-pr-1962 June 8, 2026 15:00 Inactive
@colinrotherham colinrotherham temporarily deployed to nhsuk-frontend-pr-1962 June 8, 2026 15:04 Inactive
@colinrotherham colinrotherham force-pushed the add-code-modifier-class branch from 7496f17 to 2d510be Compare June 8, 2026 15:16
@colinrotherham colinrotherham self-requested a review June 8, 2026 15:16
@colinrotherham colinrotherham temporarily deployed to nhsuk-frontend-pr-1962 June 8, 2026 15:16 Inactive
@colinrotherham

Copy link
Copy Markdown
Contributor

Updated the PR (see description) to include some Sass deprecations

We'll get this released in v10.5.2

@colinrotherham colinrotherham force-pushed the add-code-modifier-class branch from 2d510be to 2dd57bb Compare June 8, 2026 15:36
@colinrotherham colinrotherham temporarily deployed to nhsuk-frontend-pr-1962 June 8, 2026 15:36 Inactive
@colinrotherham colinrotherham merged commit 67bf2f6 into support/10.x Jun 8, 2026
23 checks passed
@colinrotherham colinrotherham deleted the add-code-modifier-class branch June 8, 2026 15:42
@colinrotherham colinrotherham changed the title [v10] Add monospace font modifier [v10] Add code and monospace font utilities Jun 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants