Day 56: Semantic HTML
Tags: webdev, html, 100daysofcode, typescript
Published: 2023-09-27 15:01:28


## What is Semantic HTML? 🤔

Semantic HTML is the practice of using HTML elements to represent the meaning and structure of content on a web page, rather than just its presentation. It's like giving your content a map 🗺️ that both humans and machines can understand. This not only improves accessibility but also helps search engines index your site accurately, potentially boosting your SEO rankings.

## Why Semantic HTML Matters 🚀

### Accessibility for All ♿

Semantic HTML is the foundation of web accessibility. Screen readers, used by individuals with visual impairments, rely heavily on the semantic structure of a webpage to provide a meaningful experience. By using semantic elements, you ensure that everyone, regardless of their abilities, can access your content.

### Improved SEO 📈

Search engines like Google use semantic HTML to understand the content and hierarchy of your webpage. When you use semantic tags correctly, search engines can better index your site, leading to higher search rankings. 

```html
<!-- Non-semantic HTML -->
<div id="article">
  <span class="title">The Power of Semantic HTML</span>
  <div class="content">
    <!-- Content goes here -->
  </div>
</div>
```

In this non-semantic example, search engines have to guess the importance of each element. Now, compare it to the semantic version:

```html
<!-- Semantic HTML -->
<article>
  <h1>The Power of Semantic HTML</h1>
  <!-- Content goes here -->
</article>
```

Here, it's clear that the title is the main heading of an article, which is more informative for search engines.

### Easier Maintenance 🧰

Semantic HTML also makes your codebase more maintainable. When you or your colleagues revisit the code, it's easier to understand the structure and purpose of each element, reducing the chances of introducing bugs or making incorrect changes.

## How to Implement Semantic HTML 🛠️

Implementing semantic HTML is straightforward. You just need to use the appropriate HTML tags to reflect the structure and meaning of your content. Here's a quick checklist:

1. **Choose the Right Element**: Select the HTML element that best describes the purpose of your content. Use `<header>` for headers, `<nav>` for navigation menus, `<article>` for articles, and so on.

2. **Avoid Overusing `<div>`**: While `<div>` elements are generic containers, try to use more specific semantic elements when they fit the context.

3. **Use Headings Properly**: Organize your content with heading tags (`<h1>` to `<h6>`). `<h1>` should be the main heading of the page, followed by `<h2>` for subsections, and so on.

4. **Label Forms and Inputs**: When creating forms, use `<label>` elements to associate labels with form fields. It improves accessibility and user experience.

## Examples

Let's explore some of the most commonly used semantic HTML elements 

### 1. `<header>` and `<footer>`
Clearly defines the header and footer of your page, aiding screen readers and search engines.
```html
<header>
   <h1>Your Website</h1>
</header>
<footer>
   <p>&copy; 2023 Your Website</p>
</footer>
```

### 2. **`<nav>`**
Identifies navigation links, providing a structural context for navigation menus.
```html
<nav>
  <ul>
     <li><a href="#home">Home</a></li>
     <li><a href="#about">About</a></li>
     <li><a href="#contact">Contact</a></li>
  </ul>
</nav>
```

### 3. **`<main>`**
Encloses the main content of the page, aiding assistive technologies in content identification.
```html
<main>
    <article>
        <h2>Article Title</h2>
        <p>Article content goes here.</p>
    </article>
</main>
```

### 4. **`<section>` and `<article>`**
Divides content into thematic sections, with `<article>` representing standalone content.
```html
<section>
<article>
    <h2>Section Title</h2>
    <p>Section content goes here.</p>
</article>
</section>
```

### 5. **`<aside>`**
Identifies content tangentially related to the content around it, such as sidebars or pull quotes.
```html
<aside>
<h3>Related Links</h3>
<ul>
    <li><a href="#link1">Link 1</a></li>
    <li><a href="#link2">Link 2</a></li>
</ul>
</aside>
```


### 3. `<figure>` 📷
`<figure>` and `<figcaption>` are used for displaying images with associated captions.
```html
<figure>
    <img src="awesome-image.jpg" alt="An awesome image">
    <figcaption>Caption for the awesome image</figcaption>
</figure>
```


### 4. `<time>` ⏰

The `<time>` element helps in marking up dates and times, providing machine-readable data to browsers and assistive technologies.

```html
<p>Article posted on <time datetime="2023-09-27">September 27, 2023</time></p>
```


### 5. `<mark>` and `<abbr>` 🖍️

`<mark>` highlights text within a paragraph, and `<abbr>` provides a tooltip with the full expansion of an abbreviation.

```html
<p>Search for <mark>semantic HTML</mark> on your favorite search engine.</p>
<p><abbr title="World Wide Web Consortium">W3C</abbr> sets web standards.</p>
```

### 6. `<details>` and `<summary>` 📚
`<details>` and `<summary>` create interactive disclosure widgets, useful for FAQs and collapsible content.

```html
<details>
    <summary>Expand for more information</summary>
    <p>Hidden content that can be revealed by the user.</p>
</details>
```

**Edge Cases 🤔**

### 1. **Multiple `<main>` Elements**
Use when dealing with multiple sections on a page, each with its main content.
```html
<main>
    <!-- Main content of the page -->
</main>
<main>
    <!-- Main content of another section -->
</main>
```

### 2. **Empty Semantic Elements**
Even if an element doesn't have content initially, it's still beneficial for structure.
```html
<section></section>
```

### 3. **Nested Semantic Elements**
Nesting can provide more context but be cautious not to overuse.
```html
<article>
    <section>
        <!-- Nested section within an article -->
    </section>
</article>
```