{"componentChunkName":"component---src-components-blog-template-js","path":"/blog/2023-07-23-web-accessibility/","result":{"data":{"markdownRemark":{"frontmatter":{"title":"Web Accessibility","date":"2023-07-23"},"html":"<p>Web accessibility is about making sure that everyone can use our application, including people with disabilities. This includes people who use screen readers, people who navigate using only a keyboard, people with colour vision deficiency, and many others. It is something I did not pay much attention to early in my career, but I have learned that it is an important part of building good software.</p>\n<h3>Semantic HTML</h3>\n<p>The easiest thing we can do is use the correct HTML elements for their intended purpose. Use <code class=\"language-text\">&lt;button></code> for buttons instead of styling a <code class=\"language-text\">&lt;div></code> to look like a button. Use <code class=\"language-text\">&lt;nav></code> for navigation, <code class=\"language-text\">&lt;main></code> for the main content, <code class=\"language-text\">&lt;header></code> and <code class=\"language-text\">&lt;footer></code> for those sections. Screen readers use these elements to understand the structure of the page and help users navigate efficiently.</p>\n<p>A common mistake is using <code class=\"language-text\">&lt;div></code> with an <code class=\"language-text\">onClick</code> handler as a button. A real <code class=\"language-text\">&lt;button></code> element gives us keyboard support (Enter and Space to activate), focus management, and screen reader announcements for free. With a <code class=\"language-text\">&lt;div></code>, we would have to implement all of that manually.</p>\n<h3>Labels and Alt Text</h3>\n<p>Every form input should have a label associated with it. Screen readers read the label to tell the user what the input is for. We can use the <code class=\"language-text\">&lt;label></code> element with a <code class=\"language-text\">for</code> attribute that matches the input's <code class=\"language-text\">id</code>, or wrap the input inside the label.</p>\n<p>Images should have descriptive <code class=\"language-text\">alt</code> text that explains what the image shows. If the image is purely decorative and does not add information, we can set <code class=\"language-text\">alt=\"\"</code> so screen readers skip it.</p>\n<h3>Keyboard Navigation</h3>\n<p>Not everyone uses a mouse. Some users navigate entirely with a keyboard, using Tab to move between interactive elements and Enter or Space to activate them. We need to make sure all interactive elements are reachable and usable with the keyboard.</p>\n<p>The <code class=\"language-text\">:focus</code> CSS pseudo-class lets us style elements when they receive keyboard focus. It is tempting to remove the default focus outline because it does not match the design, but if we do, we should replace it with a custom focus style that is clearly visible.</p>\n<h3>Colour and Contrast</h3>\n<p>Text should have enough contrast against its background for people to read it comfortably. The Web Content Accessibility Guidelines (WCAG) recommend a contrast ratio of at least 4.5:1 for normal text and 3:1 for large text. There are browser extensions and online tools that can check contrast ratios.</p>\n<p>We should also avoid using colour alone to convey information. For example, if we show error messages only in red without any text or icon, someone with colour vision deficiency might not notice them. Adding a text label or an icon alongside the colour makes the message accessible to everyone.</p>\n<h3>ARIA Attributes</h3>\n<p>When semantic HTML is not enough, ARIA (Accessible Rich Internet Applications) attributes can help. For example, <code class=\"language-text\">aria-label</code> provides a text label for elements that do not have visible text. <code class=\"language-text\">aria-hidden=\"true\"</code> hides decorative elements from screen readers. <code class=\"language-text\">aria-live</code> tells screen readers to announce dynamic content changes.</p>\n<p>However, the first rule of ARIA is: do not use ARIA if native HTML can do the job. A <code class=\"language-text\">&lt;button></code> is always better than a <code class=\"language-text\">&lt;div role=\"button\"></code>.</p>"}},"pageContext":{"slug":"/2023-07-23-web-accessibility/"}},"staticQueryHashes":[]}