Improving Web Accessibility with CSS

Creating accessible websites is essential for ensuring that everyone, including users with disabilities, can access and navigate your content effectively. By prioritizing accessibility, you not only comply with legal requirements but also enhance the overall user experience, reaching a broader audience and improving your site's usability.

Fundamentals of Accessibility

Accessibility in web development refers to designing and creating websites that can be used by everyone, regardless of their physical, cognitive, or sensory abilities. This includes ensuring that people with disabilities can perceive, understand, navigate, and interact with your site effectively. Accessibility is not just about compliance with laws and regulations; it’s about providing an equitable and inclusive user experience for all visitors.

Why is Accessibility Important?

  • Ensuring that your website is accessible means that you are considering the needs of all users, including those with disabilities. This promotes equality and inclusiveness.
  • Many countries have laws and regulations that require websites to be accessible. Non-compliance can lead to legal consequences.
  • Accessible websites tend to be easier to use for everyone. Enhancements like clearer navigation, better contrast, and more descriptive labels improve the experience for all users.
  • Search engines favour websites that provide a good user experience. Accessible websites often rank higher in search engine results due to better structured content.

Common Accessibility Issues

  • Missing Alt Text on Images
    Alt text provides a textual alternative to images, which is crucial for screen readers used by visually impaired users.
  • Insufficient Color Contrast
    Poor colour contrast can make text difficult to read for users with visual impairments. Ensuring a high contrast ratio between text and background colors is essential.
  • Lack of Keyboard Navigation
    Some users rely on keyboards instead of a mouse. Ensuring that all interactive elements can be navigated and operated using a keyboard is vital.
  • Unlabeled Form Elements
    Forms without proper labels can be confusing for users with screen readers, making it difficult to understand the purpose of each field.
  • Inaccessible PDFs
    PDFs and other documents embedded on websites need to be accessible, with proper tagging and structure.

CSS Basics for Accessibility

Semantic HTML involves using HTML tags that convey the meaning and structure of the content. Examples include using <header> for headers, <article> for articles, and <footer> for footers. 

  • Improved Screen Reader Compatibility
    Semantic HTML provides screen readers with clear information about the content structure, making it easier for visually impaired users to navigate the site.
  • Enhanced SEO
    Search engines can better understand and index content when it is structured semantically, which can improve your site's search ranking.
  • Consistency
    Semantic HTML ensures a consistent user experience across different browsers and devices.

For instance, instead of using <div> tags for all elements, use <nav> for navigation menus, <main> for main content, and <aside> for sidebars. This helps in creating a more accessible and meaningful structure.

CSS and Enhancing Accessibility

CSS is not just about styling; it also plays a significant role in enhancing accessibility. 

Focus Indicators

When users navigate through your site using a keyboard, it’s important to have clear focus indicators. The :focus and :focus-visible pseudo-classes can be used to style elements that receive focus.

button:focus {
    outline: 2px solid blue;
}

button:focus-visible {
    outline: 2px solid red;
}

High Contrast Colours

Ensure sufficient contrast between text and background colours. This is vital for users with visual impairments. Tools like the WebAIM Color Contrast Checker can help you verify that your colours meet the WCAG guidelines.

body {
    color: #000;
    background-color: #fff;
}

Responsive Design

A responsive design ensures your website is accessible on various devices, including desktops, tablets, and smartphones. Media queries can help adjust the layout based on screen size.

@media (max-width: 600px) {
    .container {
        width: 100%;
    }
}

Readable Fonts

Use readable font sizes and line heights to make your text easy to read. Avoid using small font sizes and ensure there is enough space between lines.

body {
    font-size: 16px;
    line-height: 1.5;
}

Clear and Consistent Layouts

Consistent layouts help users understand the structure of your website. CSS Grid and Flexbox can be used to create well-structured, consistent layouts.

.container {
    display: grid;
    grid-template-columns: 1fr 2fr;
    gap: 20px;
}

Key CSS Techniques for Accessibility

Using Pseudo-Class

The :focus-visible pseudo-class is essential for improving keyboard navigation. It styles elements that receive focus through keyboard interactions, making it easier for users to see where they are on the page.

button:focus-visible {
    outline: 2px solid blue;
}

This technique highlights buttons and other interactive elements when they are focused, providing a clear visual indicator for users navigating with keyboards.

Ensuring High Contrast

High contrast between text and background colors is crucial for users with visual impairments. It enhances readability and reduces eye strain.

body {
    color: #000;
    background-color: #fff;
}

By using black text on a white background, you ensure that the content is easily readable by everyone, including those with color blindness or low vision.

Responsive Design for All Devices

Responsive design ensures that your website is accessible on various devices, including desktops, tablets, and smartphones. Media queries can be used to adjust the layout based on screen size.

@media (max-width: 600px) {
    .container {
        width: 100%;
    }
}

This code snippet adjusts the container's width to be 100% on devices with a screen width of 600 pixels or less, ensuring that the content remains accessible and easy to navigate on smaller screens.

Font Size and Readability

Readable fonts and adequate line spacing are essential for users with visual impairments. Avoid using small font sizes and ensure there is enough space between lines to make the text easier to read.

body {
    font-size: 16px;
    line-height: 1.5;
}

This ensures that the text is large enough to read comfortably and that there is sufficient space between lines, improving the overall readability of your content.

Practical Examples and Code Snippets

These examples focus on forms and navigation menus, which are critical components for user interaction.

Examples of Accessible Forms

Forms are essential for user input and interaction. Making forms accessible ensures that all users can fill out and submit information easily. Here’s how you can style form elements to enhance accessibility:

label {
    display: block;
    margin-bottom: 0.5em;
}

input[type="text"] {
    width: 100%;
    padding: 0.5em;
}

Explanation

  • label { display: block; margin-bottom: 0.5em; }: Ensures that labels are clearly associated with their corresponding input fields and provides adequate spacing.
  • input[type="text"] { width: 100%; padding: 0.5em; }: Styles the text input fields to be full width with sufficient padding, making them easier to interact with.

HTML Example

<form>
  <label for="name">Name</label>
  <input type="text" id="name" name="name">
</form>

This setup makes sure that the form is easy to navigate and use, especially for users relying on screen readers.

Accessible Navigation Menus

Navigation menus are a critical part of any website. They should be easy to navigate for all users, including those using keyboards and screen readers. Here's how you can style your navigation menus to improve accessibility:

nav ul {
    list-style-type: none;
}

nav a {
    display: block;
    padding: 0.5em;
    text-decoration: none;
}

nav a:focus-visible {
    background-color: #ddd;
}

Explanation

  • nav ul { list-style-type: none; }: Removes default list styling to create a cleaner, more accessible navigation menu.
  • nav a { display: block; padding: 0.5em; text-decoration: none; }: Ensures that links are displayed as block elements with adequate padding and no underlines, making them easier to click.
  • nav a:focus-visible { background-color: #ddd; }: Provides a visual indicator for links when they are focused, improving keyboard navigation.

HTML Example

<nav>
  <ul>
    <li><a href="#home">Home</a></li>
    <li><a href="#about">About</a></li>
    <li><a href="#services">Services</a></li>
    <li><a href="#contact">Contact</a></li>
  </ul>
</nav>

This configuration makes the navigation menu more accessible by ensuring that links are easy to identify and navigate using a keyboard.

Testing and Improving Accessibility

Ensuring your website is accessible is an ongoing process. Regular testing and using the right tools can help you identify and fix accessibility issues, making your site more inclusive for all users. Here’s how you can effectively test and improve accessibility.

Tools for Accessibility Testing

  • WAVE:
    WAVE is a browser extension that helps identify accessibility issues directly on your web page. It highlights errors and provides suggestions for improvements.
  • Axe Accessibility Checker:
    Axe is another powerful tool for checking accessibility issues. It's available as a browser extension and integrates well with developer tools.
  • Lighthouse
    Lighthouse is an open-source, automated tool for improving the quality of web pages. It includes accessibility checks and is available in Chrome DevTools.
  • Screen Readers:
    Testing your website with screen readers like NVDA (NonVisual Desktop Access) or VoiceOver can help you understand how accessible your content is for visually impaired users.

Using Browser Developer Tools

Browser developer tools offer built-in features that can help you test and improve accessibility. Here’s how to use these tools effectively:

  1. Inspect Element:
    • Right-click on an element and select “Inspect” to view its HTML and CSS. Check for semantic HTML and ARIA attributes.
    • Ensure that elements like headings, buttons, and links have appropriate tags and attributes.
  2. Accessibility Tree:
    • Use the Accessibility Tree panel in Chrome DevTools to see how assistive technologies interact with your webpage.
    • This panel shows how your page’s DOM is perceived by screen readers and other assistive devices.
  3. Simulate Focus:
    • Use the “Tab” key to navigate through your site and ensure all interactive elements are focusable and provide clear visual indicators.
    • Check for focus styles and ensure that elements like buttons and links have visible outlines or backgrounds when focused.
  4. Colour Contrast:
    • Use the color picker tool in DevTools to check the contrast ratio of text and background colours. Ensure they meet the WCAG guidelines for contrast.
  5. Run Accessibility Audits:
    • In Chrome DevTools, go to the “Audits” panel, select “Accessibility,” and run an audit. This will provide a detailed report on accessibility issues and suggestions for improvements.

Example of Running an Accessibility Audit in Chrome DevTools:

  1. Open Chrome DevTools (right-click on the page and select “Inspect” or press Ctrl+Shift+I).
  2. Navigate to the “Lighthouse” or “Audits” tab.
  3. Select “Accessibility” and click on “Generate Report” or “Run Audits.”

Example Output

Accessibility Audit Results:
- [ ] Low contrast text (fix contrast ratio)
- [ ] Missing alt attributes on images (add descriptive alt text)
- [ ] Inadequate focus indicators (use :focus-visible pseudo-class)

Using these tools and techniques, you can identify and address accessibility issues more effectively. Regular testing and improvements ensure that your website remains accessible to all users, providing a better and more inclusive user experience.

Need a Helping Hand with Your Project?

Whether you need continuous support through our Flexible Retainer Plans or a custom quote, we're dedicated to delivering services that align perfectly with your business goals.

Please enter your name

Please enter your email address

Contact by email or phone?

Please enter your company name.

Please enter your phone number

What is your deadline?

Please tell us a little about your project