Wagtail’s user interface is built with:
HTML using Django templates
SVG for our icons, minified with SVGO
Linting and formatting¶
Here are the available commands:
make lintwill run all linting,
make lint-serverlints templates, and
make lint-clientlints JS/CSS.
make formatwill run all formatting and fixing of linting issues. There is also
Have a look at our
Makefile tasks and
package.json scripts if you prefer more granular options.
Use IDs for semantics only, classes for styling,
Order attributes with
class, then any
data-or other attributes with Stimulus
For comments, use Django template syntax instead of HTML.
Familiarise yourself with our stylelint-config-wagtail configuration, which details our preferred code style.
font-size, because they offer absolute control over text. Additionally, unit-less
line-heightis preferred because it does not inherit a percentage value of its parent element, but instead is based on a multiplier of the
Always use variables for design tokens such as colors or font sizes, rather than hard-coding specific values.
We use the
w-prefix for all styles intended to be reusable by Wagtail site implementers.
Most of our styles are combined into a single main stylesheet,
core.css. This is the recommended approach for all new styles, to reduce potential style clashes, and encourage reuse of utilities and component styles between views. Imports within
core.scss are structured according to ITCSS. There are two major exceptions to the ITCSS structure:
Legacy vendor CSS in
vendor/is imported in the order it was loaded in before adding in the main stylesheet, to avoid compatibility issues. If possible, those styles should be converted to components and loaded further down the cascade.
Legacy layout-specific styles in
layouts/are imported at the very end of the file, matching how styles were previously loaded across multiple stylesheets. If possible, those styles should be converted to components or utilities and loaded further up the cascade.
When creating new styles, always prefer components, adding a new stylesheet in the
components folder and importing it in
For all of our styles, we use:
A very old version of
normalize.cssas a CSS reset.
box-sizing: border-box, with elements always inheriting the
box-sizingof their parent.
Global CSS variables for colors, so they can be changed by site implementers.
Global CSS variables for font family, so they can be changed by site implementers.
--w-direction-factorCSS variable, set to
1by default and
-1for RTL languages, to allow reversing of calculations of physical values (transforms, background positions) and mirroring of icons and visuals with directional elements like arrows.
We use Tailwind to manage our design tokens via its theme, and generate CSS utilities. It is configured in
tailwind.config.js, with a base configuration intended to be reusable in other projects.
Wagtail uses most of Tailwind’s core plugins, with an override for them to create Logical properties and values styles while still using Tailwind’s default utility and design token names.
With utility classes, we recommend to:
Keep their number to a reasonable maximum, creating component styles instead if the utilities are inter-dependent, or if they are frequently reused together.
Avoid utilities relating to font size, weight, or other typographic considerations. Instead, use the higher-level type scale as defined in
We keep our Sass usage to a minimum, preferring verbose vanilla CSS over advanced Sass syntax. Here are specific Sass features to completely avoid:
@extend. Leads to unexpected cascading of styles.
And Sass features to use with caution:
Sass nesting. Avoid relying on Sass nesting specifically, and overly specific selectors. Most styles can be written with either one or two levels of nesting, 3 for specific UI states, and 4 in the most complex scenarios only.
Parent selector (
&) interpolation. Only use interpolation in class names sparingly, so we can more easily search for styles across the project.
Sass variables. Prefer Tailwind theme variables to reuse our design tokens, or CSS variables when a specific property changes based on state. Sass variables should only be used as shorter aliases for those scenarios, or as local component variables.
Mixins. Only create new mixins if the styles can’t be written as reusable component or utility styles.
Sass math. With most of our design tokens defined in Tailwind, loaded via PostCSS, we use
calcfunctions for math operations rather than Sass.
Forced colors mode¶
Also known as Windows High Contrast mode, or Contrast Themes. This is a feature of Windows for users to override websites’ styles with their own, so text is more readable. We intend to fully support it in all of our styles. Here are recommended practices:
Add additional borders where the background color would otherwise convey the position of specific elements, particularly for page regions and components layered above the page.
@media (forced-colors: active)should only be used when there is no simpler alternative. Write CSS for WHCM support from the get-go rather than with sweeping overrides.
forced-color-adjust: none. It compromises compatibility with a wide range of custom themes, and should only be needed if a component relies on a specific color hue to work (which is an anti-pattern).
Wagtail uses Stimulus as a lightweight framework to attach interactive behavior to DOM elements via
Stimulus is a lightweight framework that allows developers to create interactive UI elements in a simple way. It makes it easy to do small-scale reactivity via changes to data attributes and does not require developers to ‘init’ things everywhere, unlike JQuery. It also provides an alternative to using inline script tag usage and window globals which reduces complexity in the codebase.
When to use Stimulus¶
Stimulus is our preferred library for simple client-side interactivity. It’s a good fit when:
The interactivity is simple, and doesn’t require usage of more heavyweight libraries like React.
Wagtail’s admin interface also leverages jQuery for similar scenarios. This is considered legacy and will eventually be removed. For new features, carefully consider whether existing jQuery code should be reused, or whether a rebuild with Stimulus is more appropriate.
How to build a Stimulus controller¶
First think of how to name the controller. Keep it concise, one or two words ideally. Then,
Start with the HTML templates, build as much of the UI as you can in HTML alone. Ensure it is accessible and follows the CSS guidelines.
Create the controller file in our
client/src/controllersfolder, along with its tests (see Testing) and Storybook stories.
For initialization, consider which controller lifecycle methods to use, if any (
If relevant, also consider how to handle the controlled element being removed from the DOM
Document controller classes and methods with JSDoc annotations.
Use values to provide options and also reactive state, avoiding instance properties if possible. Prefer falsey or empty defaults and avoid too much usage of the Object type when using values.
Build the behavior around small, discrete, methods and use Stimulus actions declared in HTML to drive when they are called.
Prefer controllers that do a small amount of ‘work’ that is collected together, instead of lots of large or specific controllers.
Lean towards dispatching events for key behavior in the UI interaction as this provides a great way for custom code to hook into this without an explicit API, but be sure to document these.
Multiple controllers can be attached to one DOM element for composing behavior, where practical split out behavior to separate controllers.
Consider when to document controller usage for non-contributors.
When writing unit tests, note that DOM updates triggered by data attribute changes are completed async (next
microtick) so will require a await Promise or similar to check for the changes in JSDom.
Avoid hard-coding a controller’s identifier, instead reference it with
this.identifierif adjusting attributes. This way the controller can be used easily with a changed identifier or extended by other classes in the future.
This is an area of active improvement for Wagtail, with ongoing discussions.
Always use the
blocktranslatetags to prevent unnecessary whitespace from being added to the translation strings.
Right-to-left language support¶
We support right-to-left languages, and in particular viewing the Wagtail admin interface in a horizontally mirrored layout. Here are guidelines to guarantee support:
Write styles with logical properties and values whenever possible.
For styles that can only be written with physical properties (translations, background positions), use the
--w-direction-factorvariable equal to 1 or -1 so the value reverses based on the
dirattribute of the element or page.
As a last resort, use
[dir='rtl']style if there is no other way to write styles.
We use inline SVG elements for Wagtail’s icons, for performance and so icons can be styled with CSS. View Icons for information on how icons are set up for Wagtail users.
Icons are SVG files in the Wagtail admin template folder.
When adding or updating an icon,
Run it through SVGO with appropriate compression settings.
Manually remove any unnecessary attributes. Set the
viewBoxattribute, and remove
Manually add its
idattribute with a prefix of
icon-and the icon name matching the file name. Keep the icon as named from its source if possible.
Add or preserve licensing information as an HTML comment starting with an exclamation mark:
<!--! Icon license -->. For Font Awesome, we want:
<!--! [icon name] ([icon style]): Font Awesome [version] -->. For example,
<!--! triangle-exclamation (solid): Font Awesome Pro 6.4.0 -->.
Add the icon to Wagtail’s own implementation of the
register_iconshook, in alphabetical order.
Go to the styleguide and copy the Wagtail icons table according to instructions in the template, pasting the result in
If the icon requires right-to-left mirroring, add the