Creating A Custom Drupal Theme For High-Contrast Accessibility
A high-contrast mode can make a Drupal website easier to use for people with low vision, colour-vision differences, light sensitivity, or fatigue caused by dense interfaces. The strongest implementation is more than a black background and yellow text. It is a deliberate visual system that preserves readable typography, visible focus indicators, usable forms, meaningful images, and clear status messages across the entire theme.
Creating a Custom Drupal Theme for a High-Contrast Accessibility Mode also gives developers control over how the feature behaves on content pages, search results, navigation menus, media listings, and editorial screens. For an Australian website, this work should sit alongside the Disability Discrimination Act 1992, WCAG guidance, responsive design expectations, and practical testing on common phones, laptops, and variable network connections.
Plan The Contrast Mode Before Writing Code
Begin by defining what the mode changes and what it leaves alone. A useful high-contrast theme variant usually changes page backgrounds, text, borders, links, buttons, form controls, icons, focus outlines, tables, alerts, and selected states. It should not remove meaningful information, flatten every visual distinction, or change the content structure.
Use a small set of CSS custom properties as the visual contract. The default theme can use a restrained colour palette, while the contrast mode replaces those values with combinations that have strong contrast and clear state differences.
:root {
--page-bg: #ffffff;
--surface-bg: #f5f5f5;
--text-colour: #222222;
--muted-colour: #555555;
--link-colour: #005a9c;
--border-colour: #767676;
--focus-colour: #d14900;
}
.high-contrast {
--page-bg: #000000;
--surface-bg: #111111;
--text-colour: #ffffff;
--muted-colour: #f0f0f0;
--link-colour: #ffff00;
--border-colour: #ffffff;
--focus-colour: #00ffff;
}
Apply these variables to components rather than scattering colour values through many selectors. Text should use var(--text-colour), cards should use var(--surface-bg), and borders should use var(--border-colour). This makes the mode predictable and simplifies future maintenance when a client changes brand colours.
Avoid assuming that every user wants the same setting. Respect prefers-contrast where browser support is available, while retaining a visible manual control for users who need a reliable switch. The manual preference should override an automatic suggestion, and the selected state should remain understandable when JavaScript is unavailable.
Build The Drupal Theme Foundation
A custom Drupal theme needs a clear separation between theme metadata, CSS, JavaScript, Twig templates, and component-specific styling. A minimal theme might contain contrast_theme.info.yml, contrast_theme.libraries.yml, a css directory, a js directory, and Twig templates under templates.
The theme definition can attach the base library to every rendered page:
name: Contrast Theme
type: theme
base theme: stable9
core_version_requirement: ^10 || ^11
libraries:
- contrast_theme/global
regions:
header: Header
primary_menu: Primary menu
content: Content
footer: Footer
The library file should load the shared stylesheet and behaviour once:
global:
css:
theme:
css/base.css: {}
css/contrast.css: {}
js:
js/contrast-toggle.js: {}
dependencies:
- core/drupal
- core/once
In base.css, assign the variables to semantic elements. Keep the high-contrast rules in a separate file so the feature can be inspected and adjusted without making the default theme difficult to read.
body {
background: var(--page-bg);
color: var(--text-colour);
}
a {
color: var(--link-colour);
text-decoration-thickness: 0.12em;
text-underline-offset: 0.15em;
}
button,
input,
select,
textarea {
background: var(--page-bg);
border: 2px solid var(--border-colour);
color: var(--text-colour);
}
:focus-visible {
outline: 3px solid var(--focus-colour);
outline-offset: 3px;
}
The selector for the mode can be placed on the html element, which allows the browser to apply the correct colours before the page body is fully painted. A class such as contrast-mode is easy to inspect in browser developer tools and does not conflict with Drupal’s own body classes.
Add A Usable Toggle And Saved Preference
Place the control in a consistent location, such as the utility navigation near the skip link. It needs an accessible name, keyboard support, a visible focus state, and an accurate aria-pressed value. Avoid making the control look like an ordinary link if it changes a setting.
A Twig example is straightforward:
<button
type="button"
class="contrast-toggle"
data-contrast-toggle
aria-pressed="false">
High contrast
</button>
Drupal behaviours are preferable to a one-off DOMContentLoaded handler because parts of a page can be rendered or replaced through AJAX. The once utility prevents duplicate event handlers when Drupal attaches behaviours again.
(function (Drupal, once) {
Drupal.behaviors.contrastToggle = {
attach(context) {
once('contrast-toggle', '[data-contrast-toggle]', context).forEach((button) => {
const root = document.documentElement;
const saved = window.localStorage.getItem('contrast-mode') === 'on';
root.classList.toggle('contrast-mode', saved);
button.setAttribute('aria-pressed', String(saved));
button.addEventListener('click', () => {
const enabled = root.classList.toggle('contrast-mode');
button.setAttribute('aria-pressed', String(enabled));
window.localStorage.setItem('contrast-mode', enabled ? 'on' : 'off');
});
});
}
};
})(Drupal, once);
Consider privacy and resilience when using localStorage. A browser may block storage in private browsing, strict enterprise environments, or embedded contexts, so the script should fail gracefully. The page must remain usable if storage is unavailable. A short inline preference script in the document head can reduce a flash of the default palette, although it should be kept small and carefully tested with Drupal caching.
The control should communicate its purpose without requiring the user to interpret colour changes. “High contrast on” and “High contrast off” can be announced through a live region if the state change is otherwise ambiguous. If the button text changes, retain a stable accessible name and ensure the new label accurately describes the action rather than the current state.
Cover Content, Components, And Media
A contrast mode often fails in places that were not part of the original visual design. Check breadcrumbs, exposed filters, pagination, tabs, messages, field labels, help text, date pickers, autocomplete suggestions, modal dialogs, tables, code blocks, and disabled controls. Drupal’s rendered markup can vary by content type and contributed module, so test real pages rather than a single hand-built template.
Images need their own treatment. Do not place white text over an image merely because the overlay looks acceptable in the default palette. In the contrast mode, provide a solid background behind text, preserve a readable caption, and keep alternative text available. Drupal editors managing large image collections may benefit from this media library guide, especially when checking whether image metadata and editorial labels remain understandable.
Icons should have a text equivalent or an accessible label. A coloured status dot should be paired with words such as “Published” or “Draft”. Links must remain recognisable through more than colour, with underlines or another persistent visual treatment. Error messages should use text, an icon with an appropriate label, and a clear border or heading instead of relying on red alone.
Check typography as carefully as colour. High contrast does not compensate for tiny text, cramped line spacing, or a layout that clips content at increased browser zoom. Use relative units, allow text to reflow, and test at 200 percent zoom. Australian users may access public and commercial websites from older laptops, budget Android phones, or small screens while travelling between suburbs, so a mode that works only on a large desktop monitor is incomplete.
Fast Accessibility Checks
Use a short developer checklist before handing the theme to editors:
- Verify normal text and large text against WCAG contrast requirements.
- Test keyboard navigation, focus visibility, and skip-link behaviour.
- Zoom to 200 percent and check for clipping or horizontal scrolling.
- Inspect menus, forms, tables, alerts, dialogs, and pagination.
Run an editor-focused pass on representative Drupal content:
- Add an image with missing, short, and long alternative text.
- Publish a node containing links, a table, a block quote, and an embedded video.
- Check unpublished, warning, error, and success messages.
- Toggle the mode, reload the page, and confirm the preference persists.
Automated tools can identify many contrast failures, but they cannot decide whether a link is understandable or whether an image caption remains readable over a complex photograph. A small command-line audit can support repeatable checks; the arithmetic and scripting approach shown in this Python calculator example is a useful reminder to keep test utilities deterministic and easy to run.
Test Australian Delivery And Ongoing Maintenance
Australian projects often need to account for the Disability Discrimination Act 1992 and organisational accessibility policies, while government work may have additional procurement or WCAG obligations. Legal compliance is not established by a single automated scan. Record manual keyboard tests, screen reader checks, zoom checks, and content-editor reviews as part of the project’s acceptance criteria.
Test on devices and conditions that reflect the local audience. A site used in Sydney or Melbourne may be opened on a large office monitor, while a visitor in regional Queensland, Western Australia, or Tasmania may rely on a mobile connection with higher latency. Check the toggle on Safari, Chrome, and Firefox, and test it with browser extensions that alter colours or enforce dark mode. The feature should coexist with user-agent preferences rather than fighting them.
Drupal caching also deserves attention. If the contrast state is controlled by a class and local storage, the page can usually remain cache-friendly because the server sends the same markup to everyone. Avoid varying the entire page response by cookie unless there is a strong requirement. Clear Drupal caches after changing libraries, and use asset aggregation in staging so the production CSS order matches what users will receive.
Treat the mode as part of the theme’s component system, not as a once-off visual patch. When a new module introduces a carousel, date widget, media field, or editorial dashboard element, add it to the contrast test set. Keep colour tokens documented, review focus styles after every major redesign, and include the high-contrast state in visual regression testing. This approach helps the accessibility mode remain dependable as the Drupal website grows.