Enhancing Web Accessibility Through CSS

Published May 6, 2024

Written by: Abdulmumin Yaqeen

Last Edited: May 7, 2024

Introduction

Ensuring accessibility in web design is really important. It’s not just a matter of reaching a broader audience; it’s about creating an inclusive online environment where everyone, regardless of ability or disability, can access and interact with content seamlessly.

Cascading Style Sheets (CSS) serve as a powerful tool in achieving this goal, allowing you to tailor web content for accessibility without compromising aesthetics or functionality. In this article, we’ll look into several strategies for leveraging CSS to enhance web accessibility, ensuring a positive user experience for all.

1. Contrast Ratios:

One of the fundamental principles of accessible design is maintaining adequate color contrast between text and background elements. CSS give you the ability to to achieve this by adjusting color properties effectively. Adhering to established contrast ratio guidelines, such as those outlined by the Web Content Accessibility Guidelines (WCAG), You can ensure that text remains legible for users with visual impairments or those navigating in challenging viewing conditions. Tools like the WCAG Color Contrast Checker, colourcontrast.cc, provides invaluable assistance in determining compliant contrast ratios, facilitating informed design decisions.

/* Non-compliant contrast ratio */
body {
	color: #999;
	background-color: #eee;
}

/* Compliant contrast ratio */
body {
	color: #333;
	background-color: #fff;
}

2. Font Size and Scalability:

Flexibility in font size is essential for accommodating users with varying visual preferences and needs.

There are several important considerations to ensure accessibility and a consistent user experience across devices:

Relative font sizing: It’s crucial to use relative font sizing units like rem (root em) or em instead of absolute units like px. Relative units allow the text to scale proportionally based on the user’s browser or device settings, accommodating personal preferences, accessibility needs, or different screen sizes.

Flexible typography: Implement flexible typography by defining a typography scale that adjusts font sizes, line heights, and spacing based on the viewport size or breakpoints. This ensures that text remains legible and well-spaced across different screen dimensions.

Text resizing: Respect the user’s ability to resize text in their browser or device settings. Responsive websites should accommodate text resizing without breaking the layout or causing content to overflow or overlap.

/* Using relative units for scalable font sizes */
body {
	font-size: 16px; /* Base font size */
}

h1 {
	font-size: 2em; /* 2 times the base font size */
}

p {
	font-size: 1rem; /* 1 root em */
}

3. Responsive Design:

Responsive design is important for ensuring an accessible and user-friendly experience across various devices and screen sizes. Here are some key reasons why you don’t want to skip that:

  1. Accommodating diverse devices and screen sizes: Using smartphones, tablets, laptops, and desktops, users access websites from a wide range of devices with varying screen sizes and resolutions (Some even do on refrigerators 😅). Responsive design ensures that the website adapts and displays correctly on all these devices, providing an optimal viewing experience for everyone.

  2. Improving accessibility: Responsive design principles align with web accessibility guidelines. It ensures that content is easily readable, navigable, and operable on smaller screens, responsive websites become more accessible to users with disabilities or those using assistive technologies like screen readers or voice control.

  3. Enhancing user experience: Your website can provides a seamless and consistent user experience across devices if it responsive. Users don’t have to pinch, zoom, or scroll horizontally to view content, which can be frustrating and lead to a poor user experience. Responsive design eliminates these issues, ensuring a smooth and enjoyable experience for all users.

  4. Improving search engine optimization (SEO): Search engines like Google prioritize mobile-friendly websites in their rankings. By implementing responsive design, websites become more accessible to search engine crawlers, potentially improving their visibility and ranking in search results.

  5. Future-proofing: As new devices and screen sizes emerge, you can be rest assured that your website will adapt to these changes without requiring extensive redesigns or separate versions. This future-proofing aspect ensures that the website remains accessible and usable for years to come.

  6. Cost-effectiveness: Developing and maintaining a single responsive website is often more cost-effective than creating separate mobile and desktop versions. It streamlines the development process and reduces the overhead associated with managing multiple codebases.

/* Adjusting layout for smaller screens */
@media (max-width: 768px) {
	.container {
		flex-direction: column;
	}
}

4. Semantic HTML and ARIA Roles:

While CSS plays a pivotal role in styling web content, the foundation of accessibility lies in semantic HTML markup and ARIA roles. By employing semantic elements such as headings (h1 to h6) and leveraging ARIA roles appropriately, you can establish a robust accessibility framework that CSS can enhance further. For instance, using CSS to style semantic headings ensures proper document structure for screen readers, facilitating efficient navigation and comprehension for users with disabilities.

Let create a custom tab system and we will explain the accessibility decisions step by step:

HTML:

<div class="custom-tabs">
	<div role="tablist" aria-label="Tab Navigation">
		<button id="tab1" role="tab" aria-controls="panel1" aria-selected="true" tabindex="0">
			Tab 1
		</button>
		<button id="tab2" role="tab" aria-controls="panel2" tabindex="-1">Tab 2</button>
		<button id="tab3" role="tab" aria-controls="panel3" tabindex="-1">Tab 3</button>
	</div>
	<div id="panel1" role="tabpanel" aria-labelledby="tab1" tabindex="0" hidden>
		Content for Tab 1
	</div>
	<div id="panel2" role="tabpanel" aria-labelledby="tab2" tabindex="0" hidden>
		Content for Tab 2
	</div>
	<div id="panel3" role="tabpanel" aria-labelledby="tab3" tabindex="0" hidden>
		Content for Tab 3
	</div>
</div>

Above is our full html code.

<div role="tablist" aria-label="Tab Navigation"></div>
  • We added the role="tablist" attribute to identify this container as a group of tab elements.
  • We used the aria-label="Tab Navigation" attribute to provide an accessible name for the tablist, which screen readers can announce to convey the purpose of the interface.
<button id="tab1" role="tab" aria-controls="panel1" aria-selected="true" tabindex="0">Tab 1</button>
<button id="tab2" role="tab" aria-controls="panel2" tabindex="-1">Tab 2</button>
<button id="tab3" role="tab" aria-controls="panel3" tabindex="-1">Tab 3</button>
  • We used <button> elements to represent the tabs, as they are inherently interactive elements.
  • We added the role="tab" attribute to identify each button as a tab.
  • We used the aria-controls attribute to associate each tab with its corresponding panel, allowing screen readers to provide context.
  • We set aria-selected="true" on the first tab to indicate that it is initially selected.
  • We assigned tabindex="0" to the selected tab and tabindex="-1" to the unselected tabs to ensure proper keyboard navigation, allowing users to focus on the selected tab and skip over the unselected tabs.
<div id="panel1" role="tabpanel" aria-labelledby="tab1" tabindex="0" hidden>Content for Tab 1</div>
<div id="panel2" role="tabpanel" aria-labelledby="tab2" tabindex="0" hidden>Content for Tab 2</div>
<div id="panel3" role="tabpanel" aria-labelledby="tab3" tabindex="0" hidden>Content for Tab 3</div>
  • We used <div> elements to represent the content panels.
  • We added the role="tabpanel" attribute to identify each <div> as a panel associated with a tab.
  • We used the aria-labelledby attribute to associate each panel with its corresponding tab, allowing screen readers to announce the tab label when navigating to the panel.
  • We assigned tabindex="0" to make the panels focusable by keyboard, allowing users to navigate to and interact with the content.

CSS:

.custom-tabs {
	border: 1px solid #ccc;
	border-radius: 4px;
}
  • We styled the container with a border and rounded corners to provide visual separation and grouping for the tabs.
.custom-tabs button {
	background-color: transparent;
	border: none;
	cursor: pointer;
	padding: 10px 20px;
	font-size: 16px;
}
  • We removed the default button styles and added custom padding and font size to the tab buttons.
  • We used cursor: pointer; to indicate that the buttons are interactive.
.custom-tabs button:focus {
	outline: none;
}
  • We removed the default focus outline on the buttons, as we plan to replace it with a more accessible focus style in the next step.
.custom-tabs button[aria-selected='true'] {
	font-weight: bold;
}
  • We styled the selected tab button with a bold font weight to provide a visual cue for which tab is currently active.
.custom-tabs [role='tabpanel'] {
	padding: 20px;
}
  • We added padding to the content panels to improve readability and provide some visual separation from the tabs.

By following these accessibility decisions, we created a tabbed interface that is usable for a wide range of users, including those who rely on keyboard navigation, screen readers, or other assistive technologies. We used semantic HTML elements, ARIA roles and attributes, and carefully crafted CSS styles to ensure an accessible user experience.

5. Focus Styles:

Interactive elements like links and form fields require clear visual cues to indicate keyboard focus, particularly for users navigating via assistive technologies. CSS enables you to customize focus styles, ensuring that keyboard and screen reader users receive intuitive feedback when interacting with these elements. Distinct and visible focus styles enhance usability, empowering users to navigate the interface with confidence and ease.

/* Customizing focus styles for better visibility */
a:focus {
	outline: 2px solid #000;
	outline-offset: 2px;
}

button:focus {
	box-shadow: 0 0 0 3px rgba(0, 0, 0, 0.3);
}

6. Avoiding CSS-Only Navigation:

While CSS can enhance the visual appeal of navigation menus, relying solely on CSS for navigation may pose challenges for users, especially those reliant on screen readers. You should prioritize semantic HTML markup for navigation elements, ensuring accessibility even in the absence of CSS. By maintaining a balance between visual enhancements and semantic structure, you can create navigational experiences that cater to diverse user needs.

7. Hidden Content and Screen Readers:

CSS offers the capability to visually hide content while retaining its accessibility to screen readers, a technique valuable for providing context or additional information without cluttering the visual layout. However, it’s crucial to exercise caution and ensure that hidden content remains meaningful and relevant to all users, aligning with the principles of inclusive design.

/* Visually hiding content while keeping it accessible */
.sr-only {
	position: absolute;
	width: 1px;
	height: 1px;
	padding: 0;
	margin: -1px;
	overflow: hidden;
	clip: rect(0, 0, 0, 0);
	white-space: nowrap;
	border: 0;
}

8. Keyboard Accessibility:

Keyboard accessibility is a crucial aspect of responsive design and web accessibility. It ensures that users who rely on keyboard navigation, such as those with motor disabilities or users of assistive technologies like screen readers, can effectively interact with and navigate through web content. Here are some important considerations for ensuring keyboard accessibility in responsive design:

  1. Focus management: Ensure that all interactive elements, such as links, buttons, form controls, and navigation menus, are focusable and receive a visible focus indicator. This helps users understand where they are on the page and which element currently has focus.

  2. Logical focus order: The tab order should follow a logical and intuitive sequence, allowing users to navigate through the content in a meaningful way. This includes navigating through menus, forms, and other interactive components.

  3. Keyboard operability: All functionality available through mouse or touch interactions should also be accessible via keyboard. This includes actions like opening/closing menus, toggling UI components, and triggering interactive elements.

  4. Skip links: Provide skip links or skip navigation options that allow keyboard users to bypass repetitive content and quickly access the main content area. This is particularly important for responsive designs with complex navigation menus or repeated components.

  5. Keyboard shortcuts: Consider providing keyboard shortcuts for common actions or accessing critical functionality, especially for complex interfaces or applications.

  6. Accessible modals and overlays: If your responsive design includes modals, lightboxes, or overlays, ensure that they are focusable, can be closed with the keyboard, and maintain proper focus management when opened or closed.

  7. Accessible form controls: Ensure that form controls, such as input fields, dropdowns, and checkboxes, are focusable and provide appropriate labels, instructions, and error handling accessible via keyboard navigation.

9. Testing and Feedback:

The journey towards accessibility doesn’t end with implementation; it requires ongoing evaluation and refinement. You should regularly test their websites for accessibility using tools like WAVE or browser extensions like Axe. Additionally, soliciting feedback from users with disabilities provides invaluable insights into potential accessibility barriers, enabling you to address them promptly through CSS modifications and other adjustments.

Conclusion:

Incorporating accessibility considerations into web design isn’t just a moral imperative; it’s a fundamental aspect of creating digital experiences that prioritize inclusivity and diversity. You can 100% achieve accessiblity without compromising on style or functionality. Embracing the strategies outlined in this article and prioritizing accessibility in their design process, you can contribute to a more inclusive web environment where everyone has equal access to information and services.

Lots of us might be tempted with decisions like:

“I really like this fancy cursive font. It looks super elegant, and I think we should use it for all our headings.”

👎🏾 Overly decorative or cursive fonts can be challenging to read for users with dyslexia or cognitive disabilities.

“Instead of using descriptive alt text for images, I think we should just put something nice and simple like ‘Image’ or ‘Pic.’ It not very important anyways.”

👎🏾 Providing inadequate or uninformative alternative text for images can make content inaccessible for users who rely on screen readers or have visual impairments.

“Let’s remove the skip navigation link. I don’t think it’s super necessary, and it looks really cluttered on the page.”

👎🏾 Skip navigation links are crucial for keyboard users and screen reader users to efficiently navigate through website content.

“I really like the idea of having automatic video playback on our homepage. It looks so nice and engaging!”

👎🏾 Automatically playing videos with sound can be disruptive and inaccessible for users with hearing impairments or those who prefer to browse silently.

Make sure to resist these decisions and - Stay Cautious on Accessibility

Tagged with: CSS Accessibility A11y
Love it? Share it!