AD
Episode
342
Interview
Web News

Stop Using Divs for Everything! Master Semantic HTML, Custom Attributes, and Accessibility

Recorded:
December 10, 2024
Released:
December 17, 2024
Episode Number:
342

In this episode of the HTML All The Things podcast, Matt and Mike dive into why good HTML practices are essential for building better, more accessible, and maintainable websites. They kick things off by explaining the importance of semantic HTML for readability, SEO, and accessibility—covering useful tags like <header>, <footer>, and <article>.

Matt and Mike also discuss how developers can properly create and use custom attributes—like data-* attributes—to store extra information cleanly without relying on fragile class naming conventions. Finally, they emphasize HTML's critical role in accessibility, highlighting best practices such as using ARIA attributes appropriately and providing meaningful alt text for images.

To cap off the episode, the hosts share some lighthearted updates about their holiday plans and give a shout-out to this episode’s sponsor, Magic Mind.

Listen

Also available on...
...and many more, check your podcast app!

Who’s in This Episode?

Show Notes

Episode Sponsor - Magic Mind & Wix Studio

Limited time Magic Mind deal for our listeners!

https://magicmind.com/HTMLPOD20 - 20% off for one-time purchases and subscriptions (Use the link and code!)

Code: HTMLPOD20

Wix Studio: The Web Platform for Agencies and Enterprises

Wix Studio is the new web platform tailored to designers, developers and marketers who build websites for others or for large organizations. The magic of Wix Studio is its advanced design capabilities which makes website creation efficient and intuitive.

Check out Wix Studio today.




How to support the show

Patreon

Prices subject to change and are listed in USD

  • Support the show from as little as ~$1/month
  • Get a shoutout at the end of the episode (while supplies last) for just ~$3/month
  • Help support the HTML All The Things Podcast: Click Here



Show Notes

Introduction

HTML is often viewed as the simplest component of a website, leading some developers to prioritize CSS or JavaScript instead. However, HTML serves as the foundational structure of every webpage, making it crucial for creating accessible and maintainable web experiences. HTML acts as the skeleton of a webpage—the foundation on which every website or web app starts its rendering journey. Foundations may not be the most exciting part of development, but they are critically important.

In this episode, we'll cover:

  • The importance of using semantic HTML instead of overusing <div> tags to improve readability, SEO, and accessibility.
  • How to properly create and use data-* custom attributes for clean, dynamic web development.
  • HTML's role in web accessibility, including key practices and tools like screen readers.

1. Why Semantic HTML Matters

  • Definition of Semantic HTML: Tags that convey meaning about the content inside them.
    • <header>: Defines the top section of a page, often containing navigational links or introductory content.
    • <article>: Used for self-contained content that could stand alone, like a blog post or news article.
    • <footer>: Marks the end section of a page or a content section, typically for copyright or contact information.
    • <nav>: Represents a set of navigational links for a website.
    • <section>: Groups related content under a common theme or purpose.
    • <aside>: Contains tangentially related information, like sidebars or pull quotes.

These semantic tags provide structure and meaning, unlike generic <div> elements.

  • Benefits of Semantic HTML:
    • Enhances SEO by giving search engines context about content.
    • Provides a better user experience for assistive technologies (e.g., screen readers).
    • Improves website structure and readability for developers.
  • Common Misuses of <div>:
    • Overusing <div> for structure or styling instead of semantic elements.
    • Example of bad practice:
1<div class="header">  
2	<div class="title">Welcome to My Website</div>
3</div>
  • Corrected example:
1<header>  
2	<h1>Welcome to My Website</h1>
3</header>

2. Custom Attributes: Proper Creation and Use

  • What Are Custom Attributes?
    • Developers can define attributes starting with data- to store extra information in elements. This approach keeps the HTML structure clean and allows easy access to data without relying on fragile class naming conventions or external scripts.
    • Example: data-user-id, data-theme.
  • Best Practices for Custom Attributes:
    • Always start with data- to avoid conflicts with standard attributes.
    • Use meaningful names that are self-explanatory.
    • Avoid overloading attributes with unnecessary or overly complex data.
    • Example:
1<button data-user-id="123" data-action="delete">Delete</button>
  • Common Use Cases:
    • Storing user-specific or action-specific data for JavaScript to process.
    • Example:
1<div data-category="electronics" data-stock="25">  
2	Laptops
3</div>
  • Using data-* attributes to enable dynamic interactions without hardcoding values.

3. HTML’s Role in Accessibility

  • Why Accessibility Matters:
    • Ensures websites are usable by people with disabilities.
    • Improves overall usability and compliance with legal standards (e.g., WCAG).
    • Example: Screen readers like NVDA (NonVisual Desktop Access) and JAWS (Job Access With Speech) rely heavily on well-structured HTML to convey information to users effectively. For instance, a <nav> element helps these tools provide a clear outline of a website’s main sections, while proper use of headings allows users to navigate content more efficiently.
  • Key Accessibility Practices with HTML:
    • Use appropriate ARIA roles only when necessary; rely on native HTML elements first.
    • Example:
1<!-- Avoid unnecessary ARIA roles -->
2<button aria-role="button">Click Me</button> 
3
4<!-- Redundant --><!-- Use the native button element -->
5<button>Click Me</button>
  • Ensure form elements have associated labels.
1<label for="email">Email:</label>
2<input type="email" id="email" />
  • Provide descriptive alt text for images:
1<img src="photo.jpg" alt="A scenic mountain view at sunset." />
  • Using Landmarks:
    • Examples of landmarks for better navigation:
1<nav>...</nav>
2<main>...</main>
3<footer>...</footer>

4. Combining Best Practices

  • Real-World Example:
    • Building a blog post layout using semantic HTML, custom attributes, and accessibility features addresses challenges like maintaining consistent styling, enabling dynamic interactivity, and ensuring accessibility for all users. For example, semantic elements provide a clear structure for styling, while custom attributes allow developers to dynamically manage actions such as tracking likes or shares. Accessibility features ensure that the content is navigable and usable by assistive technologies, creating a better overall user experience.
    • Example layout:
1<article data-post-id="45">  
2	<header>    
3    	<h2>How to Write Better HTML</h2>    
4        <p>Published on <time datetime="2024-12-12">December 12, 2024</time></p>  
5	</header>  
6    <section>    
7    	<p>Using semantic HTML can make your website easier to maintain...</p>  
8	</section>  
9    <footer>    
10    	<button data-action="like">Like</button>    
11        <button data-action="share">Share</button>  
12	</footer>
13</article>

5. Key Takeaways

  • Semantic HTML enhances readability, SEO, and accessibility.
  • Custom attributes enable dynamic behavior and efficient data handling.
  • Accessible HTML practices ensure inclusivity and improve user experience for everyone.
  • Small improvements in HTML can have a significant impact on website quality and usability.

Timestamps

Timestamps are machine generated - there may be some errors.

  • 00:00 Introduction and Episode Overview
  • 01:42 The HTML Only Podcast Debate
  • 07:20 The Importance of Semantic HTML
  • 10:09 Common Semantic HTML Tags
  • 15:24 Functional HTML Tags and Their Uses
  • 19:54 Benefits of Semantic HTML
  • 23:38 Common Misuses of Div Tags
  • 25:19 Forced Misuse of HTML and Solutions
  • 29:23 Magic Mind Sponsorship and Testimonials
  • 30:48 Boosting Productivity with Magic Mind
  • 31:18 The Taste of Magic Mind
  • 31:51 Reflecting on Magic Mind's Impact
  • 33:10 HTML Custom Attributes
  • 35:25 Best Practices for Custom Attributes
  • 40:45 HTML's Role in Accessibility
  • 46:19 The Importance of Image Alt Tags
  • 53:46 Upcoming Christmas Episode Teaser