5 accessibility principles you can start implementing now
Tags: a11y, html, webdev
Published: 2021-06-10 09:10:58


<p>The topic of web accessibility is huge, and it can be hard to know where to start. In this blogpost I will cover 5 principles that you can, and should, start to implement straight away. I have selected these 5 topics, not because they are more important, but because they should be quite easy for a web developer to add to their existing toolbox. </p>

> **Prerequesites** For this blogpost I will assume that you have some basic knowledge and understanding about web accessibility. If you don't I recommend reading [Part 1: Introduction](https://dev.to/elischei/change-your-approach-to-accessibilty-and-get-a-better-developer-experience-and-create-more-accessible-websites-b6b) before diving in to this.

If you want the TL;DR (too long, didn't read) version you can <a href="#summary">jump right to the summary</a>.

## 1. Use the correct semantic elements
In html we use elements to structure our content. An element concists of two tags, one opening- and one closing tag. Like this:
```html
<div> </div>
```
**Semantic elemetents** are elements that represents a logical section of content, and have a clear meaning. 

In the example above I used a div element. This is **not a semantic element**. The div doesn't tell you anything about what kind of content you should expect to find inside it.
One semantic element you've probably used before is the h1 element (or h2 - h6). Maybe you thought it was just a simple way to style all headers consistently? It is actually a way to structure your content.

### Elements for overall site structure
![The image shows a layout of a website. There is one large container body. From the top there is a header and a nav. Underneath there is a aside to the left, and a main to the right. Inside the aside is another nav and a section. Inside the main is another section and an article.
Beneath all of this is a footer.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wv74lvxta9o6nkkftsqo.jpg)

<!-- wp:paragraph -->
<p>The above image illustrates how you can structure your site using semantic elements. This will of course depend on what kind of content you are presenting on your site. For example the upper &lt;nav&gt; could also go inside the header. And there could be an aditional &lt;header&gt; inside a &lt;section&gt;. But I wanted to keep the image simple to give you an overview of how it could look. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>&lt;header&gt;</strong><br>The header element represents introductory content. This typicalle contains the site logo, some links, a search form and maybe the author name.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>&lt;nav&gt;</strong><br>The nav element represents a collection of links which purpose is to be used for navigation. There can be more than one &lt;nav&gt; element on a page. One could represent the site-menu, and another could represent table of contents for the current page. Its important to give them different identifications so its clear for non-visual users what they represent. You can still usa an unordered list to organise your navigation links - but put the whole list inside the &lt;nav&gt; element. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>&lt;main&gt;</strong><br>The main element represents the dominant content of the page.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>&lt;aside&gt;</strong><br>The aside element represents content that is indirectly related to the main content</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>&lt;section&gt;</strong><br>The section element represent content that does not have any other tags to represent it. Typically genereic or standalone content.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>&lt;article&gt;</strong><br>The article content represents content that is independent from the rest of the site. If a user was presented this content outside of the webpage it would still make sence.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>&lt;footer&gt;</strong><br>When inside the body the footer element represent the website footer. If it is placed inside an article element it represents the least important information about that article. </p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Other elements worth noticing</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><code><strong>&lt;hr&gt;</strong></code><br>It's so easy to throw in a &lt;hr&gt; when you need a nice line on your page. But it's worth noticing that the &lt;hr&gt; tag actually have semantic meaning. It represents a thematic break in the content - and it is anounced by screen readers.<br>So if you just want a visual horizontal line for decoration, you should not use a &lt;hr&gt;, but use a css border instead. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>&lt;button&gt; vs &lt;a&gt;</strong><br>The button should be used when you want the user to trigger some javascript by clicking on it. While the a-element should be used for links that takes the user to another page. </p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>2. Using color</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><strong>Contrast</strong><br>A lot of people have problems with their sight. It could be color-blindness, or other challenges. But either way it is important that we use enough contrast between colors to ensure that the content is clearly visible for all users.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To be WCAG AA compliable the colors you use together should have a contrast ratio of at least 4.5:1 for normal text, and 3:1 for large text. Dont trust your eyes! Even though you think it looks nice, and that the contrast is good enough you should always check. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>And don't trust that the designer either. Some designers are up to date about accessibility and WCAG, but theres also some that are not. But if you find that the colors you got from a designer is not WCAG compliable remember to be nice about it.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>There are a lot of free tools you can use to see if the contrast is good enough, <a href="https://contrastchecker.com/" target="_blank" rel="noreferrer noopener" title="https://contrastchecker.com/">I like this contrast checker</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Don't use color alone to convey meaning</strong><br>On the same topic, don't use color alone to convey meaning. As an example links should always be marked with something more than a different color, like an underline. Or if you display an "error"/"success" message that are set to be red/green based on some state you should make sure that the state is still clear for people who can't see the color. You can do this by being explicit in the wording of the message. </p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>3. Always add type to input fields</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The HTML type attribute is important for screenreader users. The screenreader will anounce what kind of input type that is expected.  Especially important is it to set the type="password" for password input fields.  If the type is missing on a password field the screenreader will speak the users password aloud as they enter it. </p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>4. Don't use pixels for font-size</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Different browsers will display content a little differently. And it is also possible that your users have browser-extensions that changes the font-family or basic font-size to make it easier for them to read. Thats why WCAG recomends working with proportion and not pixel-sizing. If you use the em unit to set font-size instead of pixels. Em means that the text-size is set relative to the default font-size. (2em means 2 times the size of the default). By using em the text on your page will scale nicely no mather the users default settings. </p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>5. Always add styling for focus-state</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>If a user is navigating your solution with a keyboard they schould be able yo see where the focus is right away. Focus-style is very often displayed as a border around the conent in focus. Unfortunatley a lot of developers (and designers) tend to remove this because it doesn't look nice. You can of cource style the focus-state however you want, but make sure it is clear enough so the users don't have to wonder where on the page the focus is. </p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 id="summary">Summary</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Lets summarize what you have learned in this blogpost.</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Always use the proper semantic element in html</li><li>Make sure that the colors you use have the proper contrast ratio. And also, don't use color alone to convey meaning.</li><li>Remember to add the type property to input fields. Especially important on password fields.</li><li>Use a relative measurment for font-size. Like the em-unit.</li><li>Make it easy to see where the focus-state on your page is, by always adding styling for :focus. </li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>If you start implementing these five principles right away you are allready on your way to help create a more accessible internet. In the next part of this series I will cover some more complex topics. If you don't want to miss it make sure to <a href="https://twitter.com/acupof_dev" target="_blank" rel="noreferrer noopener" title="https://twitter.com/acupof_dev">follow me on twitter</a>. :)</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 id="summary">Resources</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li><a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element" target="_blank" rel="noreferrer noopener" title="https://developer.mozilla.org/en-US/docs/Web/HTML/Element">HTML element reference</a> (MDN Web docs)</li><li><a href="https://learn-the-web.algonquindesign.ca/topics/html-semantics-cheat-sheet/" target="_blank" rel="noreferrer noopener" title="https://learn-the-web.algonquindesign.ca/topics/html-semantics-cheat-sheet/">HTML seamantic cheat sheet</a> (Learn the web)</li><li><a href="https://contrastchecker.com/" target="_blank" rel="noreferrer noopener" title="https://contrastchecker.com/">Contrast checker</a> </li><li><a href="https://syntax.fm/show/354/the-surprisingly-exciting-world-of-html-elements" target="_blank" rel="noreferrer noopener" title="https://syntax.fm/show/354/the-surprisingly-exciting-world-of-html-elements">Syntax.fm have a podcast episodes where they talk about html semantics</a></li></ul>
<!-- /wp:list -->

***
<!-- wp:paragraph -->
<p>Did you find this article usefull?&nbsp;<a href="https://twitter.com/acupof_dev" target="_blank" rel="noreferrer noopener">Follow me on twitter</a>&nbsp;to be notified when I publish something new!</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Also, if you have any feedback or questions, please let me know in the comments below.&nbsp;:) </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Thank you for reading, and happy coding!</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>/Eli</p>
<!-- /wp:paragraph -->