Section 38: Accessibility, Forms and Cross-Browser Testing
Accessibility means designing interfaces that people with different abilities and interaction methods can use. CSS contributes through readable text, visible focus, adequate contrast, and layouts that work at different sizes and zoom levels.
1. Style forms accessibly
.form-field {
display: grid;
gap: .4rem;
margin-block: 1rem;
}
.form-field input,
.form-field select,
.form-field textarea {
width: 100%;
max-width: 100%;
padding: .75rem;
border: 1px solid #64748b;
border-radius: .4rem;
font: inherit;
}
:focus-visible {
outline: 3px solid #2563eb;
outline-offset: 3px;
}Keep labels visible, use clear instructions, and ensure keyboard focus can be seen. Do not remove outlines without providing a suitable replacement.
2. Example form
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Accessible Contact Form</title>
<style>
* { box-sizing: border-box; }
body { margin: 0; padding: 24px; font-family: system-ui, sans-serif; color: #172033; line-height: 1.5; }
form { max-width: 540px; margin-inline: auto; }
.field { display: grid; gap: 6px; margin-block: 16px; }
input, textarea, select, button { font: inherit; }
input, textarea, select { width: 100%; padding: 12px; border: 1px solid #64748b; border-radius: 6px; }
:focus-visible { outline: 3px solid #2563eb; outline-offset: 3px; }
.help { color: #475569; font-size: .9rem; }
.required { font-weight: bold; }
button { padding: 12px 18px; border: 0; border-radius: 6px; background: #1d4ed8; color: white; cursor: pointer; }
@media (max-width: 400px) { body { padding: 14px; } }
</style>
</head>
<body>
<main>
<h1>Contact us</h1>
<p>Fields marked required must be completed.</p>
<form action="#" method="get">
<div class="field">
<label for="full-name">Full name <span class="required">(required)</span></label>
<input id="full-name" name="full_name" autocomplete="name" required>
</div>
<div class="field">
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email" required aria-describedby="email-help">
<small id="email-help" class="help">Enter an address such as you@example.com.</small>
</div>
<div class="field">
<label for="message">Message</label>
<textarea id="message" name="message" rows="4" required></textarea>
</div>
<button type="submit">Send message</button>
</form>
</main>
</body>
</html>This example demonstrates styling and native form validation. The form uses a placeholder action and does not send data to a server. A real application must implement appropriate submission handling.
3. Accessibility checks
- Ensure text and control boundaries have sufficient contrast.
- Use a visible focus indicator for keyboard navigation.
- Do not rely on color alone to convey status or errors.
- Check layouts at 200% zoom and at narrow viewport widths.
- Use associated labels, meaningful headings, and useful error messages.
- Support reduced-motion preferences for nonessential animation.
4. Cross-browser testing
Test the site in the browsers and devices your users actually use. Check layout, form controls, font rendering, scrolling, focus, and modern CSS features. Use feature queries and fallbacks when required by your browser support policy.
Practice exercise
Improve the form by adding a clear error state, a success message, and a focus style. Test it using only a keyboard and at narrow widths.
Knowledge check: 10 MCQs
- Why should form labels be associated with inputs? Answer: To identify each control clearly. Explanation: Labels improve usability and assistive technology support.
- What does :focus-visible help with? Answer: Visible keyboard focus indication. Explanation: It helps users understand which control is active.
- Should focus outlines simply be removed? Answer: No. Explanation: Provide an equally visible alternative if changing the default.
- Should errors be communicated only with red color? Answer: No. Explanation: Include text or other cues so the error is not color-dependent.
- What does aria-describedby associate with a control? Answer: Additional descriptive text. Explanation: It can connect help or instruction text to a field.
- Why test at 200% zoom? Answer: To check that content remains usable when enlarged. Explanation: Zoom can reveal clipping and layout problems.
- What does the required attribute do? Answer: Enables native required-field validation. Explanation: The browser can prevent submission when the field is empty.
- Does CSS alone guarantee accessibility? Answer: No. Explanation: HTML semantics, behavior, content, and testing are also essential.
- Why test different browsers? Answer: To identify compatibility and rendering differences. Explanation: Browser support and implementation behavior can vary.
- What is a reduced-motion rule for? Answer: Adapting nonessential motion to user preferences. Explanation: It can reduce distracting or uncomfortable movement.