GOV.UK Frontend
repository·main·Indexed 23 days ago
https://github.com/alphagov/govuk-frontendProvides the essential code and components required to build user interfaces for government platforms and services, adhering to the GOV.UK Design System. It includes a library of components such as Accordion, Back link, Breadcrumbs, Button, Character Count, Checkboxes, Date input, Details, Error message, and error-summary.
What's inside govuk-frontend
- The Textarea component is used to provide a multi-line text input field. For guidance on when to use a textarea versus other input types in your service, refer to the GOV.UK Design System.
Use the Select component
mainTheselectcomponent is used to provide a list of options for a user to choose from. For detailed guidance on when to use this component in your service, refer to the GOV.UK Design System documentation.Use Override classes for high-specificity styling
mainThe Overrides layer is used to force specific properties and override all other layers. Rules in this layer generally use
!important.Naming Convention
- Class names use the prefix
-!-. Example:.govuk-!-font-width-regular. - Conditional overrides append the condition to the end of the class name. Example:
.govuk-!-display-none-printwill only applydisplay: nonein a print layout.
- Class names use the prefix
Understand browser and assistive technology support
mainGOV.UK Frontend categorizes browser support into four grades. Note that JavaScript enhancements will only run in grades A, B, and C. Browsers in grade X (including IE11 and older) will not receive JavaScript enhancements.
Browser Grades
- grade A: Most recent stable versions of Chrome, Firefox, Edge, Samsung Internet, and Safari.
- grade B: All stable versions of Chrome, Firefox, and Edge released in the last 6 months, and the last 4 major stable releases of Safari not covered in Grade A.
- grade C: All browsers that support
<script type="module">(e.g., Chrome 61+, Edge 16-18, Edge 79+, Firefox 60+, Safari 11+). - grade X: All other browsers (including IE11 and older).
Assistive Technology
- The library supports recommended assistive technologies.
- The library supports users overriding colours in Windows, Firefox, and Chrome.
How the Details component handles browser compatibility and styling
mainThe
detailscomponent uses a progressive enhancement strategy to handle varying levels of browser support for the<details>element, CSS feature queries, and ES6 modules:- Modern Browsers (supporting
type=module,<details>, and feature queries): Receive full GOV.UK styling. - Edge 16-18 (supporting
type=moduleand feature queries but not<details>): These are filtered out using the-ms-ime-align: autocheck to ensure they don't receive interactable styles if they cannot support the component. - Legacy Browsers (IE 8-11): These browsers do not support
<details>,type=module, or feature queries. They are styled to look like inset text using a@media screen\0hack to prevent them from appearing interactable. - Opera Mini: This is a known gap where browsers support feature queries but not
<details>ortype=module. These browsers may appear interactable even if they are not. - Browsers with
<details>but no module/feature query support: These fall back to native<details>styling combined with GOV.UK spacing and font styles.
Note on JavaScript: In older browsers that require a polyfill for the
<details>element, the custom marker may still be displayed even if JavaScript is disabled. This is a cosmetic issue and does not impact content accessibility.- Modern Browsers (supporting
Customise Radios using component options
mainWhen using the Radios component via a macro, you can use options to customise its appearance, content, and behaviour (for example, changing the displayed text).
Detailed information regarding available options and their specific implementations can be found in the GOV.UK Design System options table.
Understand the GOV.UK Frontend CSS architecture
mainGOV.UK Frontend follows a layered architecture inspired by ITCSS to provide a logical structure for styles. The layers are organized as follows:
- Settings: Global variables (colors, spacing, typography). Does not output CSS.
- Tools: Logic-based mixins or functions for calculations or conditional CSS. Does not output CSS.
- Helpers: Mixins that abstract common styling (e.g., focus states, visually hidden content). Does not output CSS.
- Core: Basic content styles for HTML elements like typography, links, paragraphs, and headings.
- Objects: Layout systems or wrapper elements that can be used in various contexts.
- Components: Discrete UI pieces that map to the GOV.UK Design System components.
- Utilities: Single-purpose utility classes (e.g., clearing floats).
- Overrides: High-specificity classes used to override other layers, typically using
!important.
Configure visual regression testing with Percy
mainVisual regression testing is handled via Percy. To include a component example in Percy screenshots, add the
screenshotattribute to the example in the component's YAML fixture.Supported
screenshotvalues:true: Takes a screenshot of the example.variants: ['default', 'no-js']: Takes multiple screenshots.no-jscaptures the component with JavaScript disabled to ensure visual consistency.
Note: Percy commands are ignored during local testing (e.g.,
npm run test:screenshots). You will see the message[percy] Percy is not running, disabling snapshotsin the console.Use the Hint component
mainThe Hint component is a sub-component typically used within other input components (such as Text Input) to provide additional guidance or context to the user. It is not usually used as a standalone component but rather as part of a larger input pattern.Use the Label component
mainThe
Labelcomponent is a foundational building block used primarily within other input components (such as text inputs, checkboxes, or radio buttons) to provide a descriptive title for a form field.To see how it is implemented in a real-world scenario, refer to the text input component documentation.
How marker styling is implemented in the Details component
mainTo ensure consistent alignment and control over the summary arrow across all browsers, the
detailscomponent hides the browser's native marker and injects a custom one.This approach was chosen over targeting Firefox specifically with
@-moz-document(which was deprecated in Firefox) or using Unicode glyphs (which caused emoji substitution issues on Android). Instead, the component uses border-based triangles to ensure consistent rendering.Use the error-summary component
mainThe
error-summarycomponent is used to display a summary of errors at the top of a page, typically after a form submission fails.For detailed guidance on when and how to use this component within a service, consult the GOV.UK Design System documentation.