# Blog Table of Contents Best Practices Guide

**Last Updated:** 2026-01-18

Comprehensive guide for creating optimal table of contents (TOC) structures for blog posts, including heading hierarchy, length recommendations, and UX best practices.

## Overview

The blog TOC component automatically generates a table of contents from post headings. This guide covers best practices for heading structure, TOC length, and when to use different TOC modes.

## Heading Structure Guidelines

### Recommended Structure

**H1:** Page title (one per page, handled by template)

**H2:** Major sections/chapters (5-10 per post ideal)
- Each H2 should represent a distinct topic or section
- Use descriptive, keyword-rich headings
- Examples: "Was sind Arbeitsstunden pro Monat?", "Wie berechnet man Arbeitsstunden pro Monat?"

**H3:** Subsections under H2 (0–3 per H2 depending on need)
- Use when H2 section has distinct subtopics, steps, or comparisons
- H2 with paragraphs only is valid—don't force H3s for decoration
- Examples: "Berechnung mit 4,35 Wochen", "Berechnung mit Arbeitstagen"

**H4:** Sub-points under H3 (use sparingly)
- Only when H3 section is very long or complex
- Most posts don't need H4 headings

### Heading Hierarchy Rules

1. **Never skip levels:** H1 → H2 → H3 → H4 (don't go H2 → H4)
2. **One H1 per page:** H1 is the page title (handled by template)
3. **Logical nesting:** Each heading level should logically belong to its parent
4. **Consistent structure:** Maintain similar heading patterns across posts

### Ideal Heading Distribution

| Post Length | H2 Count | H3 Count | Total TOC Items | Structure |
|-------------|----------|----------|-----------------|-----------|
| Short (<1,000 words) | 3-5 | 0-5 | 3-10 | Flat (H2 only or H2+H3) |
| Medium (1,000-2,500 words) | 5-8 | 0-15 | 5-23 | Flat or Grouped |
| Long (>2,500 words) | 8-12 | 5-30 | 13-42 | Grouped (H3s collapsed) |

*H3 counts are ranges, not minimums. H2+paragraphs is valid for many sections.*

## TOC Length Recommendations

### UX Best Practices (Based on Research)

**Ideal:** 5-7 primary items (Miller's Law: 7±2 cognitive limit)
- Easy to scan and navigate
- Low cognitive load
- Fast decision-making

**Acceptable:** 10-15 items
- Still manageable for users
- Flat structure works well
- Good for medium-length content

**Needs Optimization:** >15 items
- Exceeds cognitive load for flat lists
- Should use collapsible grouping
- H3s grouped under H2s, collapsed by default

**Too Long:** >30 items
- Consider content restructuring
- Split into multiple posts
- Or use H2-only TOC mode

### Current Implementation

The TOC component automatically applies grouping when:
- Total TOC items >15 (H2 + H3 count)
- H3s are grouped under parent H2s
- H3 groups are collapsed by default
- "Expand all" / "Collapse all" toggle available

**Example:**
- Post with 12 H2 + 26 H3 = 38 items → **Grouped mode**
- Post with 5 H2 + 8 H3 = 13 items → **Flat mode**

## When to Use Different TOC Modes

### Flat Structure (Default for ≤15 items)

**Use when:**
- TOC has ≤15 total items
- Content is relatively short (<2,500 words)
- All sections are equally important
- Users need to see all sections at once

**Benefits:**
- Simple, clean interface
- All navigation visible immediately
- Faster scanning
- Less interaction required

### Grouped Structure (Auto-applied for >15 items)

**Use when:**
- TOC has >15 total items
- Content is long (>2,500 words)
- H2 sections have multiple H3 subsections
- Reducing initial cognitive load is important

**Benefits:**
- Reduced initial visual clutter
- Better organization
- Easier to find major sections (H2s)
- Maintains full navigation (H3s available on expand)

**Behavior:**
- H2 sections always visible
- H3 sections collapsed by default
- Click H2 or chevron to expand/collapse
- "Expand all" / "Collapse all" toggle
- Auto-expands parent H2 when scrolling to H3

### H2-Only Mode (Manual override)

**Use when:**
- TOC has >30 items even with grouping
- H3s are not critical for navigation
- Content structure is very flat (many H2s, few H3s)
- Simpler navigation preferred

**Configuration:**
```php
$toc_items = build_toc_structure($headings_data, [
    'max_depth' => 2  // H2 only
]);
```

## Content Structure Best Practices

### For Content Creators

**Planning Your Post Structure:**

1. **Outline major topics first** (H2s)
   - Aim for 5-10 major sections
   - Each H2 should be a distinct topic
   - Use question format for SEO: "Was ist...?", "Wie berechnet man...?"

2. **Add subsections only when needed** (H3s)
   - Use H3s to break down complex H2 sections
   - Aim for 2-3 H3s per H2 (not more than 5)
   - Each H3 should add specific detail to H2 topic
   - When H3s are used: Optional intro paragraph between H2 and first H3 for smoother flow. Content-driven.

3. **Avoid excessive H3s**
   - If H2 has >5 H3s, consider splitting H2 into multiple H2s
   - Or restructure content to reduce nesting depth
   - Too many H3s create long, overwhelming TOC

4. **Monitor TOC length**
   - Check TOC length during content creation
   - If approaching 15+ items, consider restructuring
   - Use analysis script: `python3 v2/scripts/blog/analyze-toc-structure.py`

### Restructuring vs Grouping

**When to Restructure Content:**
- TOC has >30 items even with grouping
- Many H2s with only 1-2 H3s each (flatten structure)
- H2s are too granular (combine related H2s)
- Content feels disorganized

**When Grouping is Sufficient:**
- TOC has 16-30 items
- Clear H2/H3 hierarchy exists
- Content structure is logical
- Grouping improves UX without content changes

## Accessibility Requirements

### ARIA Attributes

**Required for Collapsible TOC:**
- `aria-expanded` - State of expand/collapse (true/false)
- `aria-controls` - ID of controlled element (H3 container)
- `aria-label` - Descriptive label for expand/collapse button
- `role="button"` - For expand/collapse buttons

**Example:**
```html
<button
    aria-expanded="false"
    aria-controls="toc-children-section-id"
    aria-label="Abschnitt ausklappen"
    role="button">
```

### Keyboard Navigation

**Required Behaviors:**
- Tab through all TOC links and buttons
- Enter/Space on expand/collapse buttons toggles state
- Focus visible on all interactive elements
- Focus management when expanding/collapsing

### Screen Reader Support

- All headings announced correctly
- Expand/collapse state announced
- Active section announced
- Structure hierarchy clear (H2 → H3 nesting)

## SEO Considerations

### Heading Structure Impact

**H2+H3 Structure (Recommended):**
- ✅ SEO-friendly: Clear content hierarchy
- ✅ Helps search engines understand structure
- ✅ Supports featured snippets
- ✅ Improves content scannability

**Collapsible Grouping Impact:**
- ✅ **No negative SEO impact:** All headings remain in DOM
- ✅ **Anchor links preserved:** All H2 and H3 links functional
- ✅ **Content structure maintained:** HTML hierarchy unchanged
- ✅ **Search engines can crawl:** All content accessible

**Best Practices:**
- Use descriptive, keyword-rich headings
- Maintain logical hierarchy (H2 → H3)
- Include primary keywords in H2s
- Use question format for H2s when appropriate

### Anchor Link Value

**H2 Links:**
- Higher value for main navigation
- Appear in search result snippets
- Used for internal linking

**H3 Links:**
- Lower value but still useful
- Help with page navigation
- Support long-form content structure
- Important for user experience

## Examples

### Example 1: Short Post (Flat Structure)

**Post:** "Urlaubsantrag stellen"
- 5 H2 headings
- 3 H3 headings
- **Total: 8 items** → Flat structure

**TOC Display:**
```
1. Was ist ein Urlaubsantrag?
2. Wie stelle ich einen Urlaubsantrag?
3. Urlaubsantrag Muster
4. Fristen und Regelungen
5. Häufige Fragen
```

### Example 2: Long Post (Grouped Structure)

**Post:** "Arbeitsstunden pro Monat"
- 12 H2 headings
- 26 H3 headings
- **Total: 38 items** → Grouped structure

**TOC Display (Collapsed):**
```
1. Was sind Arbeitsstunden pro Monat? [+]
2. Wie berechnet man Arbeitsstunden pro Monat? [+]
3. Wie viele Arbeitsstunden hat ein Monat...? [+]
...
```

**TOC Display (Expanded H2):**
```
1. Was sind Arbeitsstunden pro Monat? [−]
   1.1. Definition
   1.2. Bedeutung
2. Wie berechnet man Arbeitsstunden pro Monat? [+]
...
```

## Testing Checklist

### Before Publishing

- [ ] Check TOC length (run analysis script)
- [ ] Verify heading hierarchy (no skipped levels)
- [ ] Test expand/collapse functionality (if grouped)
- [ ] Verify all links work (click each TOC item)
- [ ] Check active section highlighting
- [ ] Test keyboard navigation
- [ ] Verify screen reader compatibility
- [ ] Check mobile behavior (TOC hidden on mobile)

### For Long Posts (>15 items)

- [ ] Verify grouping is applied automatically
- [ ] Test "Expand all" / "Collapse all" toggle
- [ ] Verify H3s are collapsed by default
- [ ] Test auto-expand when scrolling to H3
- [ ] Verify parent H2 expands when H3 active
- [ ] Check visual hierarchy (indentation, icons)

## Troubleshooting

### TOC Not Grouping When Expected

**Problem:** Post has >15 items but TOC is flat

**Solution:**
1. Check `build_toc_structure()` configuration in `post.php`
2. Verify `max_items_before_grouping` is set to 15
3. Check that `group_h3s` is not explicitly set to `false`
4. Verify headings are H2 and H3 (not H4+)

### H3s Not Showing After Expand

**Problem:** Clicking expand button doesn't show H3s

**Solution:**
1. Check browser console for JavaScript errors
2. Verify `blog-toc.js` is loaded
3. Check that `isGrouped` is `true` in component
4. Verify Alpine.js is initialized correctly

### Active Section Not Highlighting

**Problem:** Active section not updating on scroll

**Solution:**
1. Verify heading IDs match TOC item IDs
2. Check Intersection Observer is working
3. Verify `flatSections` array is populated (grouped mode)
4. Check browser console for errors

## Related Documentation

- [Blog TOC Component Documentation](components/BLOG_TOC_COMPONENT.md) - Complete component API
- [Blog TOC Analysis Report](TOC_ANALYSIS_REPORT.md) - Statistics on all blog posts
- [Content Writing Guidelines](CONTENT_WRITING_GUIDELINES.md) - Heading structure, natural flow, 2026 best practices
