How to make your code blocks accessible on your website
Tags: css, a11y, webdev
Published: 2021-02-27 15:28:57

If you’re a developer or technical writer who publishes content on the internet, you’ll want to make sure your code examples are presented beautifully for your audience to consume. 

The good news is, there are plenty of tools available to adorn your website with fancy-looking code blocks in your blog posts, that mimic current and trendy IDE themes. For example, [whitep4nth3r.com](https://whitep4nth3r.com) uses the Okaidia theme from [Prism.js](https://prismjs.com/) to present code blocks.

The bad news is, most prebuilt themes (especially those with darker colour palettes) come loaded with accessibility issues such as failing colour contrast checks, meaning your audience may have difficulties consuming your code examples. Additionally, your website will be penalised in web search results for failing accessibility checks.

This means we need to do a little more work in CSS to make our code blocks accessible. Let’s take a look.

## How to check accessibility on your website

If you’re developing in Google Chrome or Microsoft Edge, you can access the [Google Lighthouse tools from your developer console](https://developers.google.com/web/tools/lighthouse#devtools).

*Quick tip — some Chrome plugins can affect the way Lighthouse runs in your browser — so it’s always recommended to run these checks in an incognito window without any plugins activated.*

![A screenshot showing the Google Lighthouse dev tools tab with the Accessibility checkbox checked](https://whitep4nth3r.com/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2F56dzm01z6lln%2F15wBp7QhJEhV6Gxo3C4rl6%2Ffd1b6be731ee8dee927d7a39605865ec%2Fgoogle_lighthouse_accessibility_check.png&w=1080&q=75)

You can use Google Lighthouse to run a variety of reports on your web pages. Check the 'Accessibility' checkbox and click ‘Generate report’.

![A screenshot showing a 100% Accessibility score on the Google Lighthouse dev tools tab](https://whitep4nth3r.com/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2F56dzm01z6lln%2F1dsOJmWA3jwtb9jyTHE400%2Fc6998b70ab32cbdf24ca33ee047d73c3%2Fgoogle_lighthouse_accessibility_report.png&w=1080&q=75)

Here’s an accessibility check for [this blog post](https://whitep4nth3r.com/blog/how-to-build-a-lightweight-blog) (including code blocks) which produces a satisfying score of 100%! 🎉  But remember — as the report shows — there are always areas of your website that automated tools might not be able to assess correctly — so you should always carry out manual checks as well.

### axe — Web Accessibility Testing

I’ve been using [this Chrome plugin](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd) for a number of years, and it has helped me incredibly in improving my knowledge of accessibility.

Install the plugin, open your Chrome dev tools, and navigate to the ‘axe’ tab. 

![A screenshot of the axe accessibility panel in Chrome dev tools with options to scan all of my page and scan part of my page](https://whitep4nth3r.com/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2F56dzm01z6lln%2F6DzX1VunnJUC4yLPZFxklp%2F188c1c465c400ef42228a2db779af994%2Faxe_screenshot.png&w=1080&q=75)

What’s great about axe is that it shows the HTML code for any issues that it has detected. For the same blog post we scanned with Lighthouse above, axe finds 11 issues. 

That's not so good! But let’s investigate.

![A screenshot showing an axe accessibility tool issue description of failing colour contrast in a YouTube iframe embed](https://whitep4nth3r.com/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2F56dzm01z6lln%2F7mY07makct51qA3J4Fc40d%2Fe683b599758d8915a93b720be946992c%2Faxe_iframe_issue.png&w=1080&q=75)

Axe is describing colour contrast issues inside a YouTube iframe embed. It’s not related to any CSS that's bundled with the web page, and it’s not ideal. We might be able to fix it if we can target the iframe CSS effectively, or use a different thumbnail on the YouTube video. But for now, it's good to know!

### WAVE Evaluation Tool

The third tool that is integral to my accessibility workflow is the [Wave Evaluation Tool Chrome extension](https://chrome.google.com/webstore/detail/wave-evaluation-tool/jbbplnpkjmmeebjpijfedlgcdilocofh). This tool is great for highlighting semantic HTML and ensuring you’ve included the necessary aria labels.

Here’s an example of Wave in action.

![A screenshot showing a Wave Evaluation Tool summary tab overlaid on the home page of whitep4nth3r.com](https://whitep4nth3r.com/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2F56dzm01z6lln%2F5vdeBAoGL2fICfJSK2sben%2F282504d1c4aca5f3bc2ecf7f473456d6%2Fwave_screenshot.png&w=1080&q=75)

Click on ‘View details’ to see a detailed breakdown of the summary.

![A screenshot showing a Wave Evaluation Tool details tab overlaid on the home page of whitep4nth3r.com](https://whitep4nth3r.com/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2F56dzm01z6lln%2F5zdxY4hoX6ewJaF7lbnQib%2F28527d25f9add7137beeec54e77e55af%2Fwave_screenshot_details.png&w=1080&q=75)

## Fixing code block accessibility issues

There are two main issues to look for in your code blocks, both of which axe picked up for me.

### Ensure the contrast between foreground and background colors meets WCAG 2 AA contrast ratio thresholds

The theme I chose to use from Prism.js is beautiful, but some of the colours failed the colour contrast checks.

Here is a code block that failed a colour contrast check:

![A screenshot of a code block that fails colour contrast](https://whitep4nth3r.com/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2F56dzm01z6lln%2F183DUhKK2VqLZ41bzLHKoZ%2F2ee139341717a5550bcf6386c199f5dc%2Ffailing_cc_code_block.png&w=1080&q=75)

This is described in axe alongside the underlying HTML:

![A screenshot of the axe accessibility panel in Chrome dev tools showing a failing colour contrast check](https://whitep4nth3r.com/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2F56dzm01z6lln%2F7v4h8mj3ieAwMts1aQSw9Z%2F0ec711f76de4718350a7e2bfd37ccb1c%2Faxe_code_block_cc.png&w=1080&q=75)

### Finding accessible colours

Another neat tool I use is the [Colour Contrast checker](https://colourcontrast.cc/) that’s also available as a [Chrome extension](https://chrome.google.com/webstore/detail/colour-contrast-checker/nmmjeclfkgjdomacpcflgdkgpphpmnfe?hl=en-GB). Activate the extension, and use the eyedropper to select background and foreground colours to view whether it passes [Web Content Accessibility Guidelines (WCAG)](https://www.w3.org/WAI/standards-guidelines/wcag/).

![A screenshot of the Colour Contrast Checker Chrome extension showing a failing colour contrast check](https://whitep4nth3r.com/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2F56dzm01z6lln%2F2uLUS8OwLYayW8bxfvblez%2Ff483413cb5dc03ad77927d13aa5aa559%2Fcc_checker_fail.png&w=1080&q=75)

The beautiful thing about this tool is you can move the hue, saturation and light sliders to find colours that pass accessibility guidelines without having to leave your browser page. Using the slider tools, I was able to find colours that passed colour contrast checks, and added the following overrides to my global CSS file:

```css
 /* accessibility fixes for prismjs */

  .token.comment {
    color: #adb8c2 !important;
  }

  .token.important {
    color: #f3a344 !important;
  }

  .token.tag,
  .token.property,
  .token.constant {
    color: #fc92b6 !important;
  }
```

### Elements that have scrollable content should be accessible by keyboard

This accessibility violation was a little trickier to solve, and is the primary reason for writing this blog post.

![A screenshot showing an axe accessibility tool issue description of scrollable content failure](https://whitep4nth3r.com/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2F56dzm01z6lln%2F78KRDXY1Mu0tA1miQZUj3t%2F3094b9c3dc12cbf3365e3f1bbe28b661%2Faxe_scrollable_fail.png&w=1080&q=75)

Prism.js ships with the following code that caused this violation, which causes overflow text to scroll rather than wrap:

```css
pre[class*="language-"] {
   padding: 1em;
   overflow: auto;
   border-radius: 0.3em;
}
```

Code blocks are not focusable by a keyboard (whereas anchor tags are focusable, for example). This means that if your audience is using only a keyboard to navigate the site, they will be unable to access the content that has overflowed the container, which would usually be scrolled with a mouse.

The way to fix this accessibility issue is to ensure the text will wrap when necessary inside the `<code>` tag.

Here’s the code I used to prevent the scrolling behaviour and make the text wrap onto a new line:

```css
code[class*="language-"] {
  white-space: pre-wrap;
  word-break: break-all;
}
```

## In conclusion

Prebuilt IDE-like code block themes like the ones available from Prism.js are beautiful, but if you want to use one, ensure you check for accessibility issues such as colour contrast and scrolling behaviour. This will ensure your audience can use your site effectively, and your site ranks well in search engines for accessibility. 

And remember: Build stuff, learn things, love what you do.