Fix Accessibility Issues in HTML: 12 Before/After Code Examples
Published · 10 min read
You can fix most automatically detected accessibility issues in HTML and CSS, without a framework, a plugin or an overlay. Each fix is usually a few attributes, a native element in place of a <div>, or a CSS rule. This article gives 12 fixes for hand-built and static sites; for WordPress, see remediating WordPress theme code. Each one names the WCAG 2.2 criterion, shows minimal before and after code, and tells you how to check it.
Why HTML and CSS fixes go so far
The WebAIM Million 2026 report tested the home pages of the top one million websites with WebAIM's WAVE tool. The six most common failure types were:
| Failure type | Home pages affected (2026) | Fixed in |
|---|---|---|
| Low contrast text | 83.9% | CSS |
| Missing image alternative text | 53.1% | HTML |
| Missing form input labels | 51.0% | HTML |
| Empty links | 46.3% | HTML |
| Empty buttons | 30.6% | HTML |
| Missing document language | 13.5% | HTML |
WebAIM is a non-profit at Utah State University, and these numbers cover only what automated tools can detect. Still, all six are markup or stylesheet problems. Fixes 1 to 6 below cover them; fixes 7 to 12 cover issues that are just as common in hand-built sites but harder to detect automatically.
One rule runs through all 12. The W3C's Using ARIA guide calls it the first rule of ARIA: if a native HTML element or attribute already has the meaning and behavior you need, use it instead of adding an ARIA role, state or property to something else. A real <button> beats <div role="button"> every time.
1. Images without alt text, or with the wrong kind
Problem: Screen readers announce the file name, or nothing, for images without alt. Decorative images with descriptive alt add noise.
WCAG: 1.1.1 Non-text Content (Level A)
<!-- Before -->
<img src="team.jpg">
<img src="divider.svg">
<a href="/"><img src="logo.svg"></a>
<!-- After -->
<img src="team.jpg" alt="Our four support staff at the front desk">
<img src="divider.svg" alt="">
<a href="/"><img src="logo.svg" alt="Example Bakery home"></a>
Empty alt="" tells assistive technology to skip a decorative image. An image that is the only content of a link needs alt that describes where the link goes. For writing good alt text, see how to write alt text for product images.
Verify: Every <img> has an alt attribute, and reading the page with images turned off still makes sense.
2. Missing page language and page title
Problem: Without lang, screen readers guess the pronunciation rules. Without a useful <title>, users can't tell tabs or search results apart.
WCAG: 3.1.1 Language of Page (Level A), 2.4.2 Page Titled (Level A)
<!-- Before -->
<html>
<head>
<title>Untitled</title>
</head>
<!-- After -->
<html lang="en">
<head>
<title>Contact us – Example Bakery</title>
</head>
Put the page-specific part first in the title. If a passage is in another language, mark it too: <p lang="fr">Bon appétit</p> (3.1.2 Language of Parts, Level AA).
Verify: View source and check <html lang> on every template; check that each page's title is unique and descriptive.
3. Form inputs without labels or autocomplete
Problem: Placeholder text is not a label. It disappears when you type and is often not announced reliably. Fields that ask for personal data should also say what they collect, so browsers can autofill them.
WCAG: 1.3.1 Info and Relationships (Level A), 3.3.2 Labels or Instructions (Level A), 1.3.5 Identify Input Purpose (Level AA)
<!-- Before -->
<input type="text" placeholder="Your name">
<input type="text" placeholder="Email">
<!-- After -->
<label for="name">Full name</label>
<input id="name" name="name" type="text" autocomplete="name">
<label for="email">Email</label>
<input id="email" name="email" type="email" autocomplete="email">
The autocomplete values come from a fixed list in the HTML standard (name, email, tel, street-address, postal-code and so on).
Verify: Click each label: focus should move to its field. A screen reader should read the label when the field gets focus.
4. Icon-only links and buttons with no name
Problem: A link or button that contains only an SVG or an icon font has no accessible name. Screen readers announce "link" or "button" and nothing else.
WCAG: 2.4.4 Link Purpose (In Context) (Level A), 4.1.2 Name, Role, Value (Level A)
<!-- Before -->
<a href="https://www.instagram.com/examplebakery">
<svg viewBox="0 0 24 24"><path d="…"/></svg>
</a>
<button><i class="icon-search"></i></button>
<!-- After -->
<a href="https://www.instagram.com/examplebakery">
<svg viewBox="0 0 24 24" aria-hidden="true"><path d="…"/></svg>
<span class="visually-hidden">Example Bakery on Instagram</span>
</a>
<button type="submit" aria-label="Search">
<i class="icon-search" aria-hidden="true"></i>
</button>
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
margin: -1px;
padding: 0;
border: 0;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}
Hidden text and aria-label both work. Hidden text is also translated by browser translation tools, which is one reason to prefer it.
Verify: In browser DevTools, open the Accessibility pane for each icon link and button and check that "Name" is filled in.
5. <div> and <span> "buttons"
Problem: A <div> with a click handler can't be reached with Tab, doesn't respond to Enter or Space, and is announced as plain text.
WCAG: 2.1.1 Keyboard (Level A), 4.1.2 Name, Role, Value (Level A)
<!-- Before -->
<div class="menu-toggle" onclick="toggleMenu()">Menu</div>
<ul id="main-menu" class="hidden">…</ul>
<!-- After -->
<button type="button" class="menu-toggle"
aria-expanded="false" aria-controls="main-menu">Menu</button>
<ul id="main-menu" hidden>…</ul>
<script>
const toggle = document.querySelector('.menu-toggle');
const menu = document.getElementById('main-menu');
toggle.addEventListener('click', () => {
const isOpen = toggle.getAttribute('aria-expanded') === 'true';
toggle.setAttribute('aria-expanded', String(!isOpen));
menu.hidden = isOpen;
});
</script>
A <button> gets focus, Enter and Space, and the button role for free. Rebuilding that on a <div> takes role="button", tabindex="0" and key handlers, which is exactly what the first rule of ARIA tells you to avoid. Use <a href> for things that go somewhere and <button> for things that do something.
Verify: Tab to the control, press Enter and then Space; both should work. A screen reader should say "Menu, button, collapsed".
6. Low contrast text
Problem: Light gray text and placeholders are hard to read for many people, especially on phones outdoors.
WCAG: 1.4.3 Contrast (Minimum) (Level AA): at least 4.5:1 for normal text and 3:1 for large text (about 24 px regular or 18.66 px bold).
/* Before */
body { color: #999999; background: #ffffff; } /* 2.85:1 */
::placeholder { color: #aaaaaa; } /* 2.32:1 */
/* After */
body { color: #595959; background: #ffffff; } /* 7.0:1 */
::placeholder { color: #6b6b6b; } /* 5.33:1 */
Fix it once in your color variables rather than element by element. Icons and input borders have their own 3:1 requirement under 1.4.11 Non-text Contrast (Level AA).
Verify: Check each text and background pair with the WebAIM Contrast Checker or the contrast readout in your browser's color picker. Check text on images by hand.
7. Invisible focus, or focus hidden under a sticky header
Problem: outline: none removes the only sign of where keyboard focus is. A sticky header can also cover the focused link as you Tab down the page.
WCAG: 2.4.7 Focus Visible (Level AA), 2.4.11 Focus Not Obscured (Minimum) (Level AA)
/* Before */
*:focus { outline: none; }
.site-header { position: sticky; top: 0; height: 4.5rem; }
/* After */
:focus-visible {
outline: 3px solid #1a5fb4; /* 6.29:1 on white */
outline-offset: 2px;
}
.site-header { position: sticky; top: 0; height: 4.5rem; }
html { scroll-padding-top: 5rem; } /* header height plus a little space */
:focus-visible shows the ring for keyboard users without showing it on every mouse click. scroll-padding-top keeps elements that scroll into view below the header; the W3C describes this approach in technique C43.
Verify: Tab through the whole page from top to bottom. You should see where focus is at every step, and no focused element should be completely hidden behind the header or a cookie banner.
8. Headings that are styled, not structured
Problem: Screen reader users often move around a page by headings. Text that only looks like a heading is skipped, and headings chosen for their size give a confusing outline.
WCAG: 1.3.1 Info and Relationships (Level A), 2.4.6 Headings and Labels (Level AA)
<!-- Before -->
<div class="big-title">Our services</div>
<h4>Wedding cakes</h4>
<h2>Catering</h2>
<!-- After -->
<h1>Our services</h1>
<h2>Wedding cakes</h2>
<h2>Catering</h2>
Choose the heading level for the structure, then style it with CSS. Skipping levels is not by itself a WCAG failure, but a logical outline is good practice and easier to use. Headings must also describe the section that follows.
Verify: List the headings with a browser extension or a screen reader's headings list (in NVDA, Insert+F7). The list should read like a table of contents.
9. No skip link or landmarks
Problem: Keyboard users have to Tab through the whole header and menu on every page before reaching the content.
WCAG: 2.4.1 Bypass Blocks (Level A); landmarks also support 1.3.1
<!-- Before -->
<body>
<div class="header">…</div>
<div class="nav">…</div>
<div class="content">…</div>
<div class="footer">…</div>
</body>
<!-- After -->
<body>
<a class="skip-link" href="#main">Skip to main content</a>
<header>…</header>
<nav aria-label="Main">…</nav>
<main id="main">…</main>
<footer>…</footer>
</body>
.skip-link {
position: absolute;
top: -3rem;
left: 1rem;
padding: 0.5rem 1rem;
background: #ffffff;
color: #1a1a1a;
z-index: 100;
}
.skip-link:focus { top: 1rem; }
Use one <main> per page. Give each <nav> a label if you have more than one.
Verify: Load the page and press Tab once. The skip link should appear; press Enter, then Tab again, and focus should land on the first link inside the main content.
10. Error messages that only use color
Problem: A red border or a vague "Invalid" doesn't tell anyone what went wrong, and screen readers don't connect the message to the field.
WCAG: 3.3.1 Error Identification (Level A), 3.3.3 Error Suggestion (Level AA); color alone also fails 1.4.1 Use of Color (Level A)
<!-- Before -->
<label for="email">Email</label>
<input id="email" type="email" class="red-border">
<span class="error">Invalid</span>
<!-- After -->
<label for="email">Email</label>
<input id="email" name="email" type="email" autocomplete="email"
aria-invalid="true" aria-describedby="email-error">
<p id="email-error" class="error">
Error: enter an email address like name@example.com
</p>
Add aria-invalid and the message only after validation fails, and remove them when the value is fixed. On submit, move focus to the first field with an error, or to an error summary at the top of the form. Error text needs 4.5:1 contrast too (for example #b3261e on white is 6.54:1).
Verify: Submit the form with a bad value. A screen reader should read the label, "invalid entry" (wording varies) and the message when the field gets focus.
11. Tiny icon buttons
Problem: Small close, arrow and social icons are hard to hit for people with tremors, and for everyone on a phone.
WCAG: 2.5.8 Target Size (Minimum) (Level AA): targets at least 24 by 24 CSS pixels, unless an exception applies (enough spacing, an equivalent control, links inside a sentence, browser default controls, or an essential size).
<button type="button" class="icon-btn" aria-label="Close">
<svg width="16" height="16" aria-hidden="true">…</svg>
</button>
/* Before */
.icon-btn { width: 16px; height: 16px; padding: 0; }
/* After */
.icon-btn {
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 24px; /* WCAG 2.2 AA minimum; 44px is easier to use */
min-height: 24px;
}
The icon can stay 16 px. The clickable area is what counts.
Verify: In DevTools, inspect each small control and check its box is at least 24 × 24 px, or that there is enough space around it.
12. Status messages nobody hears
Problem: "Message sent", "3 results" or "Added to basket" appears on screen but focus doesn't move, so screen reader users never find out.
WCAG: 4.1.3 Status Messages (Level AA)
<!-- Before: element created by JavaScript after submit -->
<p class="toast">Message sent.</p>
<!-- After: empty region in the page from the start -->
<div id="form-status" role="status"></div>
<script>
// after the form is sent successfully:
document.getElementById('form-status').textContent =
'Thanks, your message was sent.';
</script>
role="status" is a polite live region: it is announced when the screen reader is idle. The region must exist before you change its text, or many screen readers won't announce it. Keep role="alert" for urgent messages only.
Verify: With a screen reader on, trigger the message. It should be announced without focus moving.
How to test after fixing
- Keyboard. Unplug the mouse or ignore it. Use Tab, Shift+Tab, Enter, Space, the arrow keys and Escape to get through every menu, form and dialog. Watch for lost focus, traps and hidden focus.
- Screen reader. NVDA is free on Windows; VoiceOver is built into macOS and iOS. Listen to the page title, headings, links, buttons, form labels and errors.
- Zoom and reflow. Zoom to 200% (1.4.4 Resize Text) and set the window to 320 px wide (1.4.10 Reflow). Nothing should be cut off or need sideways scrolling.
- An automated scan. Run a tool built on a rules engine such as axe-core, or WAVE, to catch regressions in the six common failure types above. Sentinela's scans, including the free scan, also work on custom and static HTML sites, using the same automated checks described on our method page.
Automated tools can't confirm everything. Several WCAG success criteria turn on judgment calls, which is why an automated check isn't an audit. Tools can tell you an alt attribute exists, not whether it describes the image. They can find headings, not whether the outline makes sense. They can't tell whether focus order is logical or whether an error message is helpful. A "no issues detected" result means no issues were detected by automated checks, not that the page meets WCAG. See what a free scan finds, and what it can't and how our testing works.
Where Sentinela fits
We fix the code, not the view on top of it, and we don't sell overlay widgets (why an overlay doesn't fix your theme). These are the kinds of changes we make on the platforms we fix today, Shopify and WooCommerce stores.
For custom and static HTML sites, Sentinela offers scans, including the free scan, and a human-reviewed audit against WCAG 2.2 A and AA. We don't offer fixes or Care Plans for custom-coded sites: the audit report lists the findings for your developer to fix, and the examples above show the usual changes. Prices are as published on pricing.
Sources
- WebAIM: The WebAIM Million, 2026 report (WebAIM is a non-profit at Utah State University and also makes the WAVE tool used in the study)
- W3C: Web Content Accessibility Guidelines (WCAG) 2.2
- W3C: Understanding WCAG 2.2 (individual criteria linked in each section)
- W3C: Using ARIA, first rule of ARIA use
- W3C: Technique C43, Using CSS scroll-padding to un-obscure content
- WHATWG HTML Standard: Autofill
- WebAIM: Contrast Checker
- NV Access: NVDA download
- Deque: axe-core on GitHub (vendor open-source project)
