Accessible Text on Images: Nail the Contrast Ratio
Tags: svelte, rust, javascript, a11y
Published: 2021-07-30 17:51:36

---
title: "Accessible Text on Images: Nail the Contrast Ratio"
published: "true"
description: "Accessible Text on Images: place your text on a semi-transparent background overlay but what alpha do you need for the text to be accessible?"
tags: "svelte, rust, javascript, a11y"
canonical_url: "https://rodneylab.com/accessible-text-on-images/"
cover_image: "https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ru0589lg52ynqgtc1ngv.png"
---

## 🤗 Accessible Images

Creating accessible text on images on a website page is not easy.  First there are all the usual accessibility concerns when adding an image your site.  For example, you need to make sure the image has an alt text which describes the image well for partially sighted visitors.  But then you need to remember to leave the alt text empty if the image is included for decorative purposes only.  OK not that difficult so far.  Next as with any other text on your site, you need to make sure that the contrast level between the text colour and the background colour is high enough.

The widely accepted <a aria-label="Read the W 3 C G 18 technique on text contrast ratio" href="https://www.w3.org/TR/WCAG20-TECHS/G18.html#G18-procedure">standard is for a 4.5 contrast ratio between the text and the background</a>.  That's simple enough for text on a plain background&hellip; modern browsers have tools built in to help here.  But what do you do when your text is overlaid on an image which had a whole range of colours?  The <a href="https://rainbow.rodneylab.com">Rainbow tool can help here</a>.  Here is an example of what we are aiming for. Although the text is a similar hue to the background, by adding an overlay background for the text with the right opacity level we can be confident the contrast ratio is in line with the standard.

![Accessible Text on Images: Example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rntle6ki12pu9cwalbdk.jpg)

### What are we doing today?

In this post we take a look at how to add accessible text to an image and also how to ensure you follow the latest best practise to make sure the image is responsive.  Although we use SvelteKit, you can easily adapt the code to work with React based frameworks or even plain HTML.  If you're keen to learn all about creating accessible text on images let's warm up by first taking a look at the Rainbow tool. After that we will crack open Vim or VSCode.

## 🌈 What is the Rainbow Tool?

The Rainbow Tool is nothing more than an accessibility aid for developers and UX designers.  To use it you just <a aria-label="Open the Rainbow Tool site" href="https://rainbow.rodneylab.com/">open up the Rainbow Tool site at rainbow.rodneylab.com</a>, add an image, customise the colours and text, hit the Get Alpha button and use the value returned in your site code.  Don't worry if you're not sure how to add overlay text in SvelteKit.  We will go over how to add text with overlay to a responsive image in a minute.  First though let's have a quick look under the hood.

The Rainbow tool was built using a static site generator.  It's power lies in <a aria-label="Read about how to use Serverless Rust functions on your Netlify hosted site" href="https://rodneylab.com/use-serverless-rust-with-netlify-functions/">a Rust serverless function</a> which does the maths.  It works by finding the pixel (in the image) with the lowest contrast to the text colour.  Then it uses a bit of High School Maths (the Newton-Raphson numerical technique) to work out the overlay transparency which satisfies the contrast ratio supplied (4.5 by default).  There are a few optimisations built in such as resizing the image before getting going to speed up the later calculations.  The code is open source and you can <a aria-label="Open the Rainbow rep on the Rodney Lab site" href="https://github.com/rodneylab/rainbow">view the Rainbow Site code on the Rodney Lab Git Hub page</a>.  Now you know what the Rainbow tool is shall we look at how to use it?


## 🔨 Accessible Text on Images How to Nail Contrast Ratio


### How to add accessible text to a website image">

1. <a aria-label="Open the Rainbow Tool" href="https://rainbow.rodneylab.com/">Open the Rainbow Accessibility tool</a> and click <strong>Browse</strong> to add your photo.

2.  Next, pick your text and overlay colours.  A common choice it white text on a black background, though you can be as creative as you like.  If there is not enough contrast between your chosen text and overlay colours, the tool will let you know.  You can also customise the text if you want to.

3. Next click or tap the <strong>Get Alpha</strong> button to fire up the Rust serverless function.  After a moment the computed alpha will be displayed.  The image is updated, using this value.

4. Finally you just head over to you favourite text editor and code up the image.  Here is example code for a responsive image.  Remember to make sure you update the alpha value for your own image.

```html
<script>
  const alt = 'flower in rainbow colours';
  const src = '/assets/rainbow-flower.jpg';
  const sizes = '(max-width: 672px) calc(100vw - 32px), 672px';
  const srcsetAvif = \`
/assets/rainbow-flower-1344.avif 1344w,
/assets/rainbow-flower-672.avif 672w,
/assets/rainbow-flower-336.avif 336w
\`;
  const srcsetWebP = \`
/assets/rainbow-flower-1344.webp 1344w,
/assets/rainbow-flower-672.webp 672w,
/assets/rainbow-flower-336.webp 336w
\`;
  const srcset = \`
/assets/rainbow-flower-1344.jpg 1344w,
/assets/rainbow-flower-672.jpg 672w,
/assets/rainbow-flower-336.jpg 336w
\`;
</script>
 
<section class="image-container">
  <picture>
    <source {sizes} srcset={srcsetAvif} type="image/avif" />
    <source {sizes} srcset={srcsetWebP} type="image/webp" />
    <source {sizes} {srcset} type="image/jpeg" />
    <img loading="lazy" decoding="async" {src} {alt} width="672" height="672" />
  </picture>
  <div class="overlay-text">Love is love ♥︎</div>
</section>
 
<style lang="scss">
  .image-container {
    position: relative;
  }
  img {
    border-radius: $spacing-3;
    background-size: cover;
    background-color: $color-theme-4;
    max-width: 100%;
    height: auto;
  }
  .overlay-text {
    position: absolute;
    top: $spacing-16;
    left: $spacing-16;
    background: rgba($color: #262322, $alpha: 0.78);
    color: #f2e3bc;
    padding: $spacing-0 $spacing-4;
    border-radius: $spacing-2;
    font-size: $font-size-7;
    font-family: 'Asap';
  }
```

The code shows how to overlay text on an image with a semi-transparent background.  The opacity (or alpha) is specified in line <code>47</code>.  Notice how we <a aria-label="Read more about Next-Gen image formats" href="https://web.dev/uses-webp-images/">embed Next-Gen image formats</a>, but include fall backs for browsers which do not support them.  We also <a aria-label="Read about embedding responsive images from M D N" href="https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images">make the image responsive by including different widths</a> and specifying when each should be used.

## 🙌🏽 What Are Your Thoughts?

In this post we looked at:
  - how to use a semi-transparent overlay to make text added to an image accessible,
  - using the Rainbow tool to get the opacity or alpha level,
  - how to embed a responsive image using Next-Gen formats with a fallback.

I hope you found this post useful and you can use something from this post in one of your projects.  I'm keen to hear how you have the Rainbow tool in your work.  Also it is still early in development so I am keen to hear your suggestions on how it could be improved.  You can leave a comment below, <a aria-label="tweet Rodney Lab" href="https://twitter.com/intent/user?screen_name=askRodney">@ me on twitter</a> or try one of the other contact methods listed below.

## 🙏🏽 Accessible Text on Images: Feedback

As I say, I hope you enjoyed the post and learned something new.  I also hope you will use this code in your own projects.  Be sure to share your work on Twitter, giving me a mention so I can see what you did.  Finally be sure to let me know ideas for other posts you would like to see. Read on to find ways to get in touch, further below.  If you have found this post useful, even though you can only afford even a tiny contribution, please <a aria-label="Support Rodney Lab via Buy me a Coffee" href="https://rodneylab.com/giving/">consider supporting me through Buy me a Coffee</a>. 

Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via <a aria-label="Reach out to me on Twitter" href="https://twitter.com/messages/compose?recipient_id=1323579817258831875">@askRodney</a> on Twitter and also <a aria-label="Contact Rodney Lab via Telegram" href="https://t.me/askRodney">askRodney on Telegram</a>. Also, see <Link aria-label="Get in touch with Rodney Lab" to="/contact">further ways to get in touch with Rodney Lab</Link>. I post regularly on <Link aria-label="See posts on svelte kit" href="https://rodneylab.com/tags/sveltekit/">SvelteKit</Link> as well as <a href="https://rodneylab.com/tags/gatsby" aria-label="See posts on Gatsby">Gatsby JS</a> among other topics.  Also <a aria-label="Subscribe to the Rodney Lab newsletter" href="https://rodneylab.com/about/#newsletter">subscribe to the newsletter to keep up-to-date</a> with our latest projects.