Semantic HTML and forms
The right tag tells the browser, the screen reader and the search engine what each part is. The button, a and form tags give you keyboard behaviour and form submit for free. A div with a click handler has none of this.
Author: bezzad
Contents
The problem: a page that works only with a mouse
The cart page of our online shop is built like this:
<div class="header">
<div class="logo" onclick="goHome()">Online Shop</div>
</div>
<div class="content">
<div class="title">Your cart</div>
<div class="btn" onclick="checkout()">Checkout</div>
</div>
With a mouse, everything works. But:
- A keyboard user cannot reach the “Checkout” button with the Tab key. So they cannot buy anything.
- A screen reader reads plain text only. It does not say this is a button.
- There are no headings. A screen reader user cannot jump between the parts of the page.
- A search engine cannot tell where the main content of the page is.
All these problems have one cause: the tags have no meaning.
Why the right tag matters
Each HTML tag has two things: a role and a default behaviour.
- The browser builds an accessibility tree from the tags. The screen reader reads this tree, not the look of the page.
- The button tag has the role “button”. So the screen reader says “Checkout, button”.
- The same tag also gives you keyboard behaviour for free: it gets focus with Tab and is clicked with Enter and Space.
- Search engines also use the page structure to understand the title and the main content.
So the rule is simple: first find the tag that already has this meaning. Use div and span only when no such tag exists.
Document basics
Every page starts with a few fixed lines:
<!doctype html>
<html lang="fa" dir="rtl">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Cart | Online Shop</title>
</head>
- The doctype line keeps the browser in standards mode.
- The lang attribute says the language of the page. The screen reader uses it to pick the right pronunciation, and the browser uses it to offer translation. For Persian, the dir attribute with the value rtl makes the text right to left.
- The meta tag named viewport tells a phone to show the page at the real screen width. Without it, the phone shows the page like a small, zoomed-out desktop page.
- The title tag shows in the browser tab and in search results. Write a different title for each page.
Landmarks
A few tags mark the big parts of the page. A screen reader shows a list of these parts, and the user jumps straight to any of them.
<body>
<header>
<a href="/">Online Shop</a>
<nav aria-label="Main">
<ul>
<li><a href="/products">Products</a></li>
<li><a href="/cart">Cart</a></li>
</ul>
</nav>
</header>
<main>
<h1>Your cart</h1>
<!-- cart items, total, checkout form -->
</main>
<footer>
<a href="/contact">Contact us</a>
</footer>
</body>
- The header tag is the top of the page: logo, search and menu.
- The nav tag is a group of main navigation links. If you have more than one nav, give each one a name with the aria-label attribute.
- The main tag is the main content of the page. Each page shows only one main.
- The footer tag is the bottom of the page: contact, terms, copyright.
Heading order
- Headings (the h1 to h6 tags) build the structure of the page, not the text size. Use CSS for size.
- Usually each page has one h1: the product name or “Your cart”.
- Do not skip levels. Under an h2, the next heading is an h3, not an h4.
- Many screen reader users first scan a page by its headings. A bad order means a bad table of contents.
Button, link, or div with a click?
One simple question gives the answer: does the user go somewhere else, or does something happen on this page?
<!-- Goes to another page: a link -->
<a href="/cart">View cart</a>
<!-- Does an action on this page: a button -->
<button type="button" id="add-to-cart">Add to cart</button>
- A link (the a tag with an href attribute) has an address. The user can open it in a new tab or copy its address. It works with Enter.
- A button (the button tag) does an action: add to cart, open a menu, submit a form. It works with Enter and Space.
- A div with a click has none of this. It does not get focus, has no role, and does not answer the keyboard.
To make a div act like a button, you need all this code, and it is still not complete:
<!-- Bad: rebuilding a button by hand -->
<div role="button" tabindex="0" class="btn" id="checkout">Checkout</div>
<script>
const el = document.getElementById('checkout');
el.addEventListener('click', checkout);
el.addEventListener('keydown', (e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
checkout();
}
});
</script>
Instead, just write:
<button type="button" onclick="checkout()">Checkout</button>
Forms
Let us build the checkout form of the shop with the right tags:
<form id="checkout-form" action="/checkout" method="post">
<label for="email">Email</label>
<input id="email" name="email" type="email" autocomplete="email" required>
<label for="phone">Phone</label>
<input id="phone" name="phone" type="tel" autocomplete="tel">
<label for="qty">Quantity</label>
<input id="qty" name="qty" type="number" min="1" max="10" value="1">
<button type="button" id="apply-coupon">Apply coupon</button>
<button>Place order</button>
</form>
- The label tag connects to the id of the field with its for attribute. The screen reader reads the field name. A click on the label text also moves focus to the field.
- Placeholder text does not replace a label. It disappears when the user types.
- The type attribute changes the phone keyboard. The value email shows a keyboard with the @ sign, and the value tel shows a number pad. The value email also checks the address format.
- The required attribute means an empty field must not be sent. The browser shows an error message before it submits.
- The name attribute is the key of the value in the form data. A field without a name is not sent at all.
- The autocomplete attribute helps the browser offer the saved email and phone.
The button type trap
- The default type of a button tag inside a form is submit.
- So if you do not write a type for the “Apply coupon” button, a click on it submits the whole form.
- The fix: write type with the value button on every button that must not submit the form.
The Enter key and the submit event
- When the form has a submit button, pressing Enter in a text field submits the form. You get this for free.
- That is why in JavaScript you listen to the submit event of the form, not to the click of the button. The submit event runs for a click and for Enter.
- The submit event runs only after the browser’s own checks (like required) pass.
const form = document.getElementById('checkout-form');
form.addEventListener('submit', async (event) => {
event.preventDefault(); // stay on the page, send with fetch
const data = new FormData(form); // uses the name attributes
await fetch('/checkout', { method: 'POST', body: data });
});
Images
<img src="/img/shoe-800.jpg" alt="Blue running shoe, side view"
width="800" height="600">
<img src="/img/shoe-back.jpg" alt="Blue running shoe, back view"
width="800" height="600" loading="lazy">
<img src="/img/divider.svg" alt="">
- The alt attribute is the text a screen reader reads instead of the image. If the image does not load, this text is shown. Write what the image says, not “image”.
- An empty alt is for a decorative image. The screen reader skips it. If you leave out the alt attribute completely, some screen readers read the file name.
- The width and height attributes tell the browser the image ratio before the download. The browser keeps the space from the start, and the text below does not jump.
- The loading attribute with the value lazy downloads the image only when it comes near the screen. It is good for images lower on the page. Do not use it on the main image at the top, because it then loads later.
With this CSS, the image gets smaller on a phone and keeps its ratio:
img {
max-width: 100%;
height: auto;
}
Common mistakes
| Mistake | Result | Better way |
|---|---|---|
| A button made of a div with a click handler | Does not work with the keyboard. The screen reader does not say it is a button. | The button tag. |
| A link with an empty href for an action | The screen reader says “link”, but the user goes nowhere. | A button tag for actions, an a tag for going somewhere. |
| A button inside a form with no type | A click on it submits the form. | The type attribute with the value button. |
| Placeholder text instead of a label | The field has no name. The hint is gone once the user types. | A label tag with the for attribute. |
| A field without a name | Its value is not sent. | A name attribute on every field. |
| Listening to the click of the submit button | Your code runs even when the browser checks fail (for example, an empty required field). | The submit event on the form. |
| Picking a heading by its size | The page structure breaks. | The right h1 to h6 order. Size with CSS. |
| An image without width and height | Content jumps and CLS gets worse. | The width and height attributes. |
| The lazy value on the main image | The main image shows later. | The lazy value only for images lower on the page. |
| A page without lang and viewport | Wrong screen reader pronunciation. A tiny page on phones. | The lang attribute and the meta tag named viewport. |
Summary in six lines
- Each tag has a role and a behaviour. First find the tag with the right meaning, then think about a div.
- Mark the parts with header, nav, main and footer, and write headings in order.
- Use a link to go to another page and a button to do an action. Never a div with a click.
- Every field has a label and a name. Make the user’s job easier with type, required and autocomplete.
- A button inside a form submits by default. Listen to the submit event: it runs for Enter too, and only after the browser checks pass.
- Every image has alt, width and height. The lazy value is only for images lower on the page.