Tips For Writing Good Alt Text In HTML
Tags: html, codenewbie, beginners, webdev
Published: 2021-05-22 23:50:24

Hello Fellow Codenewbies 👋,

Sometimes we forget or ignore writing an alt text.
One of the reasons I often hear is that we don't know what to write, skip it for later, and then forget to write it down. Or, we write something, but not descriptive enough. 

I hope after reading this article, we will start to get more aware of alt text and know how to write a good one.


## What is an alt text?

Alt text is an *alternative* text to describe an image.

`<img src="pancake.png" alt="stack of strawberry pancakes on white table">` 

Imagine when we open a page and an image fails to render. Without a descriptive alt text, we will only see a broken image without knowing what image we are seeing.

{% codepen https://codepen.io/adiati/pen/ExWNXjG %}

## The importance of alt text 

### Accessibility

A descriptive alt text will help *every internet user*.
For people with visuals impairment, the screen readers will read the image's alt text for them.
And when an image fails to render, alt text will be the description of the broken image for everyone to read.

{% codepen https://codepen.io/adiati/pen/eYvBRPo %}

### SEO

An image with alt text will optimize the image SEO in particular, and the website in general. It will help our image to get discovered by search engines and get ranked.
We need to think about writing a good alt text so that our image and product can reach a bigger audience.

## How to write a good alt text?

- Short and descriptive
- Don't use the word *image*, *picture*, or similar
- No need to include dashes or underscores between every word

Let's take an example from this image.
![woman holding teacup](https://cdn.hashnode.com/res/hashnode/image/upload/v1621639495412/v5o6jj42g.jpeg)
*Photo by Jarek Ceborski - [Unsplash](https://images.unsplash.com/photo-1491575509428-5cc7d3e4076b?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=750&q=80)*

### ⭐ Short and descriptive

A long descriptive alt text doesn't mean a good one. 

  `<img src="woman-holding-teacup.jpg" alt="woman in white long-sleeve shirt sitting on a gray chair in a living room while holding a saucer and a teacup">`

But also bear in mind, short alt text doesn't mean that we put a bunch of keywords as if we write hashtags. Doing this can make people, especially those who use screen readers, confuse. Search engines will also think that we have untrustworthy images and websites.

  `<img src="woman-holding-teacup.jpg" alt="woman hands long-sleeve shirt teacup chair">` 

✅ We need to write a short, specific, and descriptive alt text. Consider putting in the most important information in the image. 

`<img src="woman-holding-teacup.jpg" alt="woman in white long-sleeve shirt sitting and holding teacup">` 

### ⭐ Don't use the word *image*, *picture*, or similar

`<img src="woman-holding-teacup.jpg" alt="picture of woman in white long-sleeve shirt sitting and holding teacup">` 

### ⭐ No need to include dashes or underscores between every word

`<img src="woman-holding-teacup.jpg" alt="woman-in-white-long-sleeve-shirt-sitting-and-holding-teacup">` 

## Not every image need alt text

An image with [pure decoration](https://www.w3.org/WAI/WCAG21/Understanding/non-text-content#dfn-pure-decoration) purposes doesn't need alt text.
The best way is to [add this image with CSS](https://www.w3.org/WAI/WCAG21/Techniques/css/C9) as the example below.

{% codepen https://codepen.io/adiati/pen/dyvpgBb %}

Another alternative is to leave the alt text empty ([null alt text](https://www.w3.org/WAI/WCAG21/Techniques/html/H67.html)). Most screen readers will skip reading an image with empty alt text. 
When we don't provide alt text, some screen readers will read the image's file name. Thus, it is a good practice to name our file as a short description of the picture.

`<img src="woman-holding-teacup.jpg" alt="">` 

## Conclusion

- Alt text is important for accessibility purposes and to optimize image SEO.
- A good alt text:
  - Short and descriptive.
  - Not using the word *image* or similar.
  - Not including dashes or underscores between every word. 
- Images with pure decoration purposes don't need alt text. We can add these images with CSS or leave a blank alt text.

---

Thank you for reading!
Last but not least, you can find me on [Twitter](https://twitter.com/AdiatiAyu). Let's connect! 😊