Don't Discriminate, Build Inclusive Websites!
The beauty of the World Wide Web is in its accessibility to all, regardless of ability or disability.
If a website isn't accessible, it can exclude a significant portion of the population, including those with disabilities or learning difficulties. By prioritising accessibility, you ensure that everyone can use your website.
Implementing accessibility features in HTML and CSS is essential for creating an inclusive website. Here's how to do it:
HTML
Semantic Elements - These elements provide meaning and structure to your content, making it easier for assistive technologies (e.g. screen readers) to interpret:
<header>, <nav>, <main>, <footer>, <article>, <section>, and <aside>
.Alt Text - Use
alt
for images. This describes the purpose of the image for those with visual impairments.<img src="img_girl.jpg" alt="Girl in a jacket">
Aria-Hidden - The
aria-hidden
attribute tells screen readers which HTML elements to read aloud and which ones to skip. This is helpful because it prevents the reading of elements that could complicate content flow and structure for those with visual impairments. For example:<div aria-hidden="true">
This text is hidden from screen readers.
</div>
<div aria-hidden="false">
This text is visible to screen readers.
</div>
Note: By default, HTML elements are visible to screen readers unless explicitly marked otherwise. So, you don't need to add
aria-hidden="false"
to your HTML elements if you want them to be visible. You only need to addaria-hidden="true"
when you want to hide an element and its content from screen readers."Form Labels -
<label>
provide meaningful descriptions for<form>
fields, enabling screen readers to convey the purpose of each<input>
to users with visual impairments. For example, consider a simple login form:<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username">
<label for="password">Password:</label>
<input type="password" id="password" name="password">
<button type="submit">Login</button>
</form>
In this example, each
<input>
element is associated with its corresponding<label>
using thefor=
attribute. The for attribute's value matches the id attribute of the input field, creating a link between the label and the input. This ensures that screen readers announce the label when users focus on the input field, providing clear and accessible form interactions for all users.Keyboard Shortcuts - Make sure your html elements, such as links, buttons, and forms, can be accessed using a keyboard. This is important for people who cannot use a mouse.
<button accesskey="s">Save</button>
The
accesskey
attribute is set to "s". Pressing the "s" key along with the appropriate modifier key (such as Alt or Ctrl) will activate the button. This provides a keyboard shortcut for users.<input type="text" tabindex="1" placeholder="First Name">
<input type="text" tabindex="2" placeholder="Last Name">
<button tabindex="3">Submit</button>
Say you have a form with a place to write your first name, last name, and a button to submit the form. The
tabindex
tells the computer which order to navigate these things when someone presses the "Tab" key. In this example, the first name box comes first because it hastabindex=1
, then the second name box withtabindex=2
, and finally the submit button comes last withtabindex=3
. This helps people using keyboards to move through your form in the right order.
CSS
Contrast - Ensure that
color
andbackground-color
have sufficient contrast to make it readable for users with low vision or colour blindness. Aim for a minimum contrast ratio of 4.5:1 for normal text and 3:1 for large text. You can use a Colour Contrast Checker and a Shade Generator to create lighter or darker versions of your brand colour.Motion - Use
prefers-reduced-motion
media query to accommodate users sensitive to moving elements:@media (prefers-reduced-motion: reduce) {
}
Media Queries - Use
@media
queries to adjust layouts and styles based on different devices and screen sizes.Hover and Active - Use
:hover
and:active
states to make elements more visible and interactive like links and buttons.button:active {
background-color: #002233;
}
HTML and CSS best practices ensure website accessibility for everyone, including those with disabilities or impairments. By following the guidelines, you create a more inclusive and user-friendly website.