Welcome to your fifth lesson! In our last session, we set up a professional workflow using a feature branch and a pull request to add a placeholder file for our product card. This process ensures that our main codebase remains stable while we develop new features in isolation.
Today, we'll turn our attention to that placeholder file, client/product-card.html. Our goal is to build a semantic HTML product card that supports keyboard and screen-reader navigation. This means moving beyond just making things look right and focusing on creating a structure that is meaningful, robust, and usable by everyone, including people who rely on assistive technologies. We will explore the "what" and "why" of semantic HTML and web accessibility, then apply these principles to build a foundational component for our e-commerce store.
What is Accessibility?
At its core, web accessibility (often abbreviated as "a11y") is the practice of ensuring that your websites and applications can be used by everyone, regardless of their physical or cognitive abilities. This includes users who are visually impaired and rely on screen readers, users who cannot operate a mouse and depend on keyboard navigation, and many others.
A key concept in how browsers support this is the accessibility tree. It's a structure, similar to the DOM, that browsers create specifically for assistive technologies. Using the right HTML elements is the most direct way to provide correct and useful information to this tree.
Let's begin with a video that introduces the concept of accessible development and the role of the accessibility tree.
Learn Accessibility - Full a11y Tutorial
This video from the freeCodeCamp.org channel provides an excellent introduction to what accessible development means and the technical foundation that makes it possible.
Please watch the segment from the introduction. Pay close attention to the explanation of the accessibility tree and the example comparing a button built from a <div> to a proper <button> element. This highlights how our choice of HTML tags directly impacts accessibility.
Semantic HTML: The Foundation of Accessibility
As you just saw, the single most effective thing you can do to improve accessibility is to use semantic HTML. This simply means using HTML elements for their intended purpose. An <h1> is for the main heading, a <p> is for a paragraph, and a <button> is for an interactive button.
When you use semantic HTML, you get a huge amount of accessibility for free. The browser already knows how a <button> should behave with a keyboard, and it knows how to announce a list (<ul>) to a screen reader.
The following reading from the MDN Web Docs, the definitive resource for web developers, explains this principle further.
MDN Web Docs HTML: A good basis for accessibility - Learn web development | MDN
This article from MDN provides a great overview of why semantic HTML is crucial.
Please read the introduction, from the start of the page down to the end of the "HTML and accessibility" section. Focus on the comparison between a <div> and a <button> and the list of benefits: easier development, better mobile performance, and good SEO. You'll find it under the heading HTML and accessibility.
Building the Product Card: Element by Element
Now, let's apply these principles to build our product card. We'll go through each part of the card and choose the most appropriate semantic element.
A product card is a self-contained piece of content, so the <article> tag is a perfect choice for the main container. It signals that the content inside forms a complete, standalone item in the document.
1. The Product Image
For the product image, we'll use the <img> tag. Crucially, we must include the alt attribute. This attribute provides a text alternative for the image, which is read aloud by screen readers. It's also displayed if the image fails to load. A good alt text is descriptive but concise.
MDN Web Docs HTML: A good basis for accessibility - Learn web development | MDN
Let's dive deeper into providing text alternatives for images.
In the same MDN article, find the section titled "Text alternatives". Read from the beginning of that section to the sentence describing the output. This explains the importance of the alt attribute. Also, read the brief subsection on empty alt attributes, which covers how to handle purely decorative images.
2. The Product Name and Price
The product name is the title of our article, so a heading tag is appropriate. Since the main page title will be an <h1>, we should use an <h2> for the product card title to create a logical document outline. The price can be a simple <p> (paragraph) element.
This structure is vital for screen reader users, who can navigate a page by jumping between headings.
MDN Web Docs HTML: A good basis for accessibility - Learn web development | MDN
A well-structured document is easy to navigate.
Read the subsection on text structure in the MDN article. Notice how screen readers can generate a "table of contents" from headings, making it easy to find specific information.
3. The "Add to Cart" Button
This is perhaps the most critical element for accessibility. An "Add to Cart" action must be performed by a <button> element. It is tempting to use a <div> or an <a> tag and style it to look like a button, but this is an accessibility anti-pattern.
- A native
<button>is focusable with theTabkey by default. - It can be activated with the
EnterorSpacekey by default. - Screen readers announce it as a "button," telling the user it's an interactive element.
If you use a non-semantic element like a <div>, you lose all of this built-in functionality and have to add it back manually with extra attributes and JavaScript.
Accessibility Dos and Don'ts for Interactive Cards | Livefront
This article from Livefront offers clear, practical advice on making cards interactive.
Read the section on simple cards. It gives a direct and compelling reason to always prefer <button> over a <div> for actions.
To see a practical demonstration of the problems you create by not using a native button, watch the following video segment.
Web Accessibility Tutorial (A11y) - Keyboard Navigation, Aria Tags, Contrast, Semantics and more!
This video from RoadsideCoder gives a hands-on look at what happens when you build a custom interactive component.
Watch from this segment on keyboard navigation. The presenter shows how a native HTML element works correctly with the keyboard out of the box, but a custom-built rating component fails. Notice all the extra code (tabindex, onkeydown) needed to replicate the browser's default behavior. This is why we stick to semantic elements whenever possible.
Your Task: Building the Card
It's time to put it all together. Open your ecommerce-store project in VS Code and replace the entire contents of the client/product-card.html file with the following code.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Product Card</title>
</head>
<body>
<!--
The <article> tag is perfect for a self-contained component
like a product card.
-->
<article class="product-card">
<!--
The product image. The alt text describes the image for screen readers
and for situations where the image cannot load.
-->
<img src="https://via.placeholder.com/300" alt="A gray placeholder image for a product.">
<!--
An <h2> is a good choice for the product name, creating a clear
document structure.
-->
<h2>Classic Tee</h2>
<!-- A simple paragraph for the price. -->
<p>$24.99</p>
<!--
A <button> is essential for actions. It's keyboard-accessible
and announced correctly by screen readers by default.
-->
<button type="button">Add to Cart</button>
</article>
</body>
</html>
After pasting this code, save the file. You can open product-card.html in your browser to see the unstyled result. Try pressing the Tab key; you should see the browser's focus outline move to the "Add to Cart" button. You can then press Enter or Space to "click" it. This is the native accessibility you get for free.
A Look Ahead: A List of Cards
While we've focused on a single card, our storefront will eventually display a grid of many products. When you have a collection of similar items, the most semantic way to group them is with an unordered list (<ul>), where each product card is a list item (<li>). This gives screen readers important context, allowing them to announce "List, 10 items" and letting the user know where they are in the collection.
Accessibility Dos and Don'ts for Interactive Cards | Livefront
Let's briefly look at how to handle multiple cards.
Read the short section "Cards, plural" in the Livefront article. It shows how a list provides a much better experience than just stacking <div> elements.
We will implement this list structure in a future lesson when we build the full catalog page.
Conclusion
In this lesson, you've taken a significant step toward writing professional-grade HTML. You've learned that building for the web is about more than just visual appearance; it's about creating a meaningful structure that is robust and accessible to all users.
Here are the key takeaways:
- Semantic HTML is the practice of using elements for their intended meaning, not just their style.
- It is the foundation of web accessibility, providing a rich experience for users of assistive technologies like screen readers.
- For a product card, use
<article>for the container,<img>withalttext for the image, a heading like<h2>for the name, and—most importantly—a<button>for interactive actions. - Using native HTML elements gives you crucial features like keyboard navigation for free.
You now have a well-structured, accessible product card. It may not look like much yet, but you've built a solid foundation. In our next lesson, we will bring it to life by using CSS Flexbox and Grid to arrange the storefront navigation, product cards, and a responsive catalog.