Semantic HTML means using elements for what they mean, such as <nav> for navigation, <button> for actions and <h2> for a section heading, instead of generic <div> and <span> tags styled to look right. Astro is a good fit for semantic HTML because components render straight to HTML with no wrapper elements, so the markup you write is exactly what browsers, screen readers, search engines and AI crawlers receive.
This guide covers the elements that matter most on content sites, the Astro-specific patterns that keep components semantic (typed props, as props, slots), and how to check your output.
Key Takeaways
- Astro components add no wrapper markup, so semantics are entirely in your hands.
- Use one
<main>, plus<header>,<nav>,<footer>and<aside>landmarks in your layout.- Headings form an outline: one
<h1>per page, no skipped levels. Let components accept a heading level prop.- Links go somewhere (
<a href>); buttons do something (<button>). Never use a<div>with a click handler.- Type props with
HTMLAttributesandPolymorphicfromastro/typesto keep components flexible and correct.
Why semantic HTML matters
Semantic markup does three jobs at once:
- Accessibility. Screen readers announce landmarks, headings and lists, and let users jump between them. A
<button>is focusable and works with Enter and Space for free; a<div>is not. - SEO and AI answers. Search engines and AI crawlers use headings, lists, tables and
<article>boundaries to understand what a page is about and which parts to quote. - Maintainability. Code that says
<nav>and<article>is easier to read and style than nested anonymous<div>elements.
Astro does not ship a virtual DOM or a client-side renderer by default, so there is no framework layer adding markup. That makes it easy to get semantics right and easy to get them wrong: whatever you write in a .astro template is the final HTML.
Landmarks in your base layout
Landmarks let assistive technology users jump to the main parts of a page. Put them in your layout once, and every page inherits them:
---
// src/layouts/BaseLayout.astro
import Header from '../components/Header.astro';
import Footer from '../components/Footer.astro';
interface Props { title: string }
const { title } = Astro.props;
---
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>{title}</title>
</head>
<body>
<a class="skip-link" href="#main">Skip to content</a>
<Header />
<main id="main">
<slot />
</main>
<Footer />
</body>
</html>
---
// src/components/Header.astro
const links = [
{ href: '/', label: 'Home' },
{ href: '/work/', label: 'Work' },
{ href: '/blog/', label: 'Blog' },
];
const path = Astro.url.pathname;
---
<header>
<a href="/">Studio</a>
<nav aria-label="Main">
<ul>
{links.map((link) => (
<li>
<a href={link.href} aria-current={path === link.href ? 'page' : undefined}>
{link.label}
</a>
</li>
))}
</ul>
</nav>
</header>
A few details worth copying: lang on <html>, a skip link pointing at <main>, a label on <nav> (important when a page has more than one nav), navigation as a list, and aria-current="page" on the active link. Use exactly one <main> per page.
Headings as a document outline
Headings are how people and machines skim a page. Each page should have one <h1> and nested levels that do not skip (an <h2> followed by an <h4> breaks the outline).
Reusable components cause most heading problems, because a card might sit under an <h2> on one page and an <h3> on another. Let the parent choose the level:
---
// src/components/Card.astro
interface Props {
title: string;
href: string;
level?: 2 | 3 | 4;
}
const { title, href, level = 3 } = Astro.props;
const Heading = `h${level}` as const;
---
<article class="card">
<Heading><a href={href}>{title}</a></Heading>
<slot />
</article>
---
// src/pages/index.astro
import Card from '../components/Card.astro';
---
<h2>Recent work</h2>
<Card title="Regional bank rebrand" href="/work/bank/" level={3}>
<p>A new identity for a bank modernizing its branches.</p>
</Card>
Astro allows a dynamic tag name stored in a capitalized variable, which is what Heading is here. Pick heading levels for structure, then style them with classes; never choose <h4> because it looks smaller.
Article, section and aside
These three are often confused:
<article>: content that makes sense on its own, such as a blog post, a case study, a product card or a comment.<section>: a thematic group within a page, usually with its own heading. If you cannot think of a heading for it, it is probably a<div>.<aside>: content related to the main content but separate from it, like a sidebar, a pull quote or related links.
A blog post page might look like this:
---
// src/pages/blog/[id].astro (template excerpt)
const { post } = Astro.props;
---
<article>
<header>
<h1>{post.data.title}</h1>
<p>
By {post.data.author} on
<time datetime={post.data.pubDate.toISOString()}>
{post.data.pubDate.toLocaleDateString('en-US', { dateStyle: 'long' })}
</time>
</p>
</header>
<slot />
<footer>
<p>Filed under {post.data.tags.join(', ')}</p>
</footer>
</article>
<aside aria-label="Related posts">
<h2>Related posts</h2>
<!-- related list -->
</aside>
<time datetime> gives machines an unambiguous date while people read a friendly one. <header> and <footer> inside an <article> belong to that article, not the page.
Buttons vs links
This rule fixes a large share of accessibility bugs:
- If it navigates to a URL, use
<a href="...">. - If it does something on the page (opens a menu, submits a form, toggles a theme), use
<button type="button">.
---
// src/components/ThemeToggle.astro
---
<button type="button" class="theme-toggle" aria-pressed="false">
Dark mode
</button>
<script>
const btn = document.querySelector<HTMLButtonElement>('.theme-toggle');
btn?.addEventListener('click', () => {
const on = document.documentElement.classList.toggle('dark');
btn.setAttribute('aria-pressed', String(on));
});
</script>
A <div onclick> is not focusable, has no keyboard support and is announced as plain text. A link styled as a button is fine if it navigates; a button styled as a link is fine if it acts.
Lists, tables, figures and native widgets
- Lists (
<ul>,<ol>) for any group of similar items: nav links, features, steps, tags. Screen readers announce the count. - Tables for tabular data, with
<th scope="col">headers and a<caption>. Never for layout. <figure>and<figcaption>for images, code samples or charts with a caption.<details>and<summary>for a native accordion that works with no JavaScript.<dialog>for modals;showModal()handles focus trapping and the Escape key.
---
// src/components/Faq.astro
interface Props { items: { q: string; a: string }[] }
const { items } = Astro.props;
---
<section aria-labelledby="faq-title">
<h2 id="faq-title">Questions</h2>
{items.map((item) => (
<details>
<summary>{item.q}</summary>
<p>{item.a}</p>
</details>
))}
</section>
Native elements come with keyboard handling and accessible roles built in, which means less JavaScript and fewer bugs. That matches Astro’s default of shipping none. The accordion component guide builds on <details>, and the modal dialog guide uses <dialog>.
Forms with real labels
Every input needs a <label> connected by for and id, or by wrapping. Placeholder text is not a label.
---
// src/components/ContactForm.astro
---
<form method="post" action="/api/contact/">
<label for="email">Email</label>
<input id="email" name="email" type="email" autocomplete="email" required />
<fieldset>
<legend>Budget</legend>
<label><input type="radio" name="budget" value="small" /> Under $5,000</label>
<label><input type="radio" name="budget" value="large" /> $5,000 or more</label>
</fieldset>
<button type="submit">Send</button>
</form>
Use the right type (email, tel, url) so mobile keyboards adapt, autocomplete so browsers can fill fields, and <fieldset> with <legend> for groups of radios or checkboxes.
Typed, flexible components with astro/types
Semantic components often need to render different elements depending on context, or pass through native attributes. Astro exports types for both:
---
// src/components/Button.astro
import type { HTMLAttributes } from 'astro/types';
type Props = HTMLAttributes<'button'> & { variant?: 'primary' | 'ghost' };
const { variant = 'primary', type = 'button', class: className, ...attrs } = Astro.props;
---
<button type={type} class:list={['btn', `btn-${variant}`, className]} {...attrs}>
<slot />
</button>
HTMLAttributes<'button'> gives you every valid button attribute (including aria-* and disabled) with type checking. For a component that can render as different tags, use Polymorphic:
---
// src/components/Box.astro
import type { Polymorphic } from 'astro/types';
type Props = Polymorphic<{ as: 'div' | 'section' | 'article' | 'aside' }>;
const { as: Tag = 'div', ...attrs } = Astro.props;
---
<Tag {...attrs}><slot /></Tag>
Now <Box as="section" aria-labelledby="x"> renders a real <section>, and TypeScript only allows attributes valid for the chosen element.
How to check your markup
- Astro dev toolbar. In
npm run dev, the toolbar’s audit app flags common accessibility issues such as missing alt text and missing labels on the current page. - View the built HTML. Run
npm run buildand open a page indist/to see exactly what ships. - Browser accessibility tree. Chrome and Firefox DevTools show the accessibility tree, with the role and name of each element.
- Keyboard test. Tab through the page. Every interactive element should be reachable, visible when focused and usable with Enter or Space.
- Headings outline. A browser extension or a quick script listing
h1toh6shows whether levels skip.
Semantic HTML checklist
langon<html>, one<main>, labeled<nav>elements- One
<h1>, no skipped heading levels, heading level as a component prop <article>for standalone content,<section>only with a heading- Links navigate, buttons act
- Lists for groups, tables for data,
<figure>for captioned media - Native
<details>and<dialog>before custom widgets - Every form control has a label
- Images have meaningful
alttext, oralt=""if decorative
Every Komal theme is plain Astro components you can open and edit, so these patterns are easy to check and adjust. For references, see MDN’s HTML elements reference and the components guide for props and slots.
FAQ
What is semantic HTML?
Semantic HTML uses elements according to their meaning, such as <nav> for navigation, <article> for standalone content, <button> for actions and heading tags for structure. It helps screen readers, search engines and other tools understand a page without relying on visual styling.
Does semantic HTML help SEO?
Yes, indirectly. Clear headings, lists, tables and article boundaries help search engines and AI crawlers understand and extract content. Semantic markup also improves accessibility, which overlaps with many usability signals search engines care about.
Do Astro components add extra wrapper divs?
No. An Astro component renders only the markup in its template, with no wrapper element. If a component’s template is a single <article>, the page gets exactly that <article>, which makes semantic HTML easy to control.
How do I change the heading level of a reusable Astro component?
Accept a level prop, build the tag name with something like const Heading = `h${level}`, and render <Heading>. The parent page then passes the right level for its outline, such as level={3} under an <h2> section.
Should I use section or div in Astro components?
Use <section> when the block is a thematic part of the page with its own heading. Use <div> for grouping purely for layout or styling. A <section> without a heading usually adds no useful meaning.