All articles
Formatters
8 min readBy DevUtilX Team

HTML Formatting and Semantic Markup Explained

Learn HTML formatting conventions and semantic elements like nav, main and article, so your markup is readable, accessible and search-friendly.

HTML Formatting and Semantic Markup Explained

HTML is the first language most developers learn and the one they revisit least. It is forgiving, so pages "work" even when the markup is messy. But messy markup has a cost: it is harder to read, harder to maintain, worse for accessibility and weaker for search engines. This guide covers two linked skills. First, how to format HTML so people can read it. Second, how to write semantic markup so browsers, screen readers and search engines understand it. You will learn the conventions, the elements that matter, the common mistakes and how to automate the routine parts.

What is HTML formatting, and what is semantic markup?

HTML, HyperText Markup Language, defines the structure of web pages. Its living specification is maintained by the WHATWG, and practical documentation lives in the MDN HTML reference.

The two ideas in this article are easy to confuse, so keep them apart:

  • Formatting is layout: indentation, line breaks, attribute order and quoting. It affects how readable the source is to humans. It has almost no effect on how the page renders.
  • Semantics is meaning: choosing <nav> for navigation and <button> for a button instead of a <div> for everything. It affects accessibility, SEO, default behaviour and maintainability.

Compare these two snippets. Both look similar in a browser once styled, but only one describes what its parts are:

<div class="top">
  <div class="menu"><div class="item">Home</div><div class="item">Blog</div></div>
</div>
<header>
  <nav aria-label="Main">
    <ul>
      <li><a href="/">Home</a></li>
      <li><a href="/blog">Blog</a></li>
    </ul>
  </nav>
</header>

The second version gives assistive technology a landmark to jump to, gives crawlers a clear structure, and gives developers meaningful names.

Core formatting conventions

Teams vary on details, but these habits are widely shared.

Indent nested elements. Use two spaces (or four, or tabs) consistently. Each child sits one level deeper than its parent.

One block-level element per line. Put <section>, <p>, <ul> and similar on their own lines so structure is visible.

Lowercase tags and attributes. HTML is case-insensitive, but lowercase is the universal convention.

Quote attribute values. Write class="card", with double quotes. Unquoted values work in simple cases and fail with spaces or special characters.

Declare the doctype and language. Start with <!DOCTYPE html> and set <html lang="en">. The language attribute helps screen readers pronounce text correctly.

Close what you open. Some elements, such as <p> and <li>, may omit closing tags under the specification, but explicit closing tags make the structure obvious and avoid surprises.

Wrap long attribute lists. When a tag has many attributes, put one per line:

<img
  src="/images/hero.jpg"
  alt="A developer reviewing formatted code on a laptop"
  width="1200"
  height="630"
  loading="lazy"
/>

Mind inline whitespace. Whitespace between inline elements is meaningful: a space or line break between two <a> tags renders as a visible gap. Be careful when a formatter reflows inline content. The MDN page on whitespace in the DOM explains the rules.

The semantic elements that matter most

HTML5 introduced a set of elements that describe page regions. Use them in place of generic containers wherever they fit.

Element Use it for
<header> Introductory content for a page or section
<nav> A block of navigation links
<main> The unique, primary content (one per page)
<article> A self-contained piece, such as a post or comment
<section> A thematic group of content, usually with a heading
<aside> Tangential content such as a sidebar
<footer> Footer information for a page or section
<figure> / <figcaption> An image or diagram with a caption
<time> A date or time in machine-readable form

Beyond the layout elements, semantics also covers the everyday ones:

  • Headings <h1> to <h6> form an outline. Use them in order, with one <h1> per page, and never choose a heading level just for its size. Style the size with CSS.
  • Lists <ul>, <ol> and <dl> for lists, not line breaks with dashes.
  • Buttons and links. Use <button> for actions and <a href> for navigation. A clickable <div> has no keyboard support, focus handling or role unless you rebuild all of it.
  • Tables for tabular data only, with <th> headers and a <caption>.
  • Forms. Pair every input with a <label>, and group related fields with <fieldset> and <legend>.

A short rule of thumb: pick the element whose meaning matches the content, and use <div> and <span> only when nothing else fits.

Why semantics pay off

Accessibility. Screen readers expose landmarks, headings and lists, so users can jump around a page quickly. The W3C's WAI tutorials show how structure maps to assistive technology. Semantic elements also bring keyboard behaviour for free: a <button> is focusable and responds to Enter and Space with no extra code.

SEO. Search engines parse structure to understand a page. Clear headings, lists and article boundaries help them identify the main content and its topics. Semantics will not rescue thin content, but it removes ambiguity.

Maintainability. Names like <nav> and <article> document themselves, so new team members find their way faster. Fewer wrapper <div> elements also means less CSS fighting.

Resilience. Browsers apply sensible default styles and behaviour to semantic elements. Reader modes, translation tools and other user agents rely on them.

Real-world use cases

Inspecting minified or generated HTML. Server-rendered output, email templates and copied page sources are often a single line. Formatting them is the first step in debugging.

Reviewing templates. Before a code review, normalise indentation so the diff shows real changes instead of whitespace.

Auditing accessibility. Reading well-indented markup makes it easier to spot a missing label, a skipped heading level or a <div> doing a <button>'s job.

Cleaning up CMS or WYSIWYG output. Editors often produce deeply nested wrapper elements and inline styles. Formatting reveals the clutter, and you can then simplify it.

Teaching and documentation. Clear examples help learners focus on structure.

Common mistakes

  • Div soup. Wrapping everything in <div> loses all meaning. Replace wrappers with real elements.
  • Skipped or misused headings. Going from <h1> to <h4>, or using <h3> because it looks right, breaks the outline.
  • Missing alt text. Meaningful images need a description, and decorative ones need an empty alt="" so screen readers skip them.
  • Clickable non-interactive elements. A <div onclick> is invisible to keyboards and assistive tools.
  • Using tables for layout. Reserve tables for data. Use CSS grid or flexbox for layout.
  • Inline styles and presentational tags. Keep style in CSS. Elements such as <b> and <i> should carry meaning, or be replaced by <strong> and <em> or CSS.
  • Duplicate IDs. An id must be unique in a document, or scripts, links and labels misbehave.
  • Overusing ARIA. ARIA attributes patch gaps in semantics, but native elements are better. The first rule of ARIA is to avoid it when a native element exists.

Step-by-step: format HTML with DevUtilX

For a quick clean-up with no setup, use the DevUtilX HTML Formatter. It runs in your browser.

  1. Open the HTML formatter tool.
  2. Paste your messy or minified HTML into the input editor.
  3. Choose your indentation.
  4. Run the formatter and review the structured output.
  5. Copy the result into your project.

Your markup is processed locally and not uploaded. Once the code is readable, check it with the HTML Validator, and when you are ready for production use the HTML Minifier to reduce the size.

Validating and automating

A formatter will not tell you that a tag is unclosed or an attribute is invalid. A validator will. The W3C Nu Html Checker checks markup against the standard, and our guide on HTML validation goes deeper.

For a project, automate the routine work:

Format with Prettier. Prettier formats HTML as well as JavaScript and CSS. A short configuration keeps style consistent. Our guide to JavaScript code formatting covers the tooling.

Lint accessibility. Linters such as eslint-plugin-jsx-a11y (for JSX) or HTMLHint (for plain HTML) flag missing alt text, invalid roles and other issues as you type.

Share editor settings. An .editorconfig file keeps indentation consistent across editors.

Check in CI. Run the formatter in check mode, and a validator or accessibility scanner, on every pull request.

Keep the usual split of duties: the formatter owns layout, and linters and validators own correctness.

Best practices

  • Start from the outline. Decide the page's headings and landmarks first, then fill in content.
  • Prefer native elements. Choose <button>, <nav>, <label> and friends before reaching for ARIA or scripts.
  • One <h1> and one <main> per page. Keep heading levels in order.
  • Always set lang and a descriptive <title>.
  • Give every image and form control a text alternative.
  • Keep markup lean. Remove wrapper elements that serve no purpose.
  • Separate concerns. Structure in HTML, presentation in CSS, behaviour in JavaScript.
  • Format in source, minify in the build. Keep readable files in version control and let the build compress them.
  • Test with a keyboard and a screen reader. Five minutes of tabbing through a page reveals many problems.

Comparison and alternatives

HTML is not the only way to write markup, but all alternatives compile to it. If you use a template language, the same semantic rules apply to the output.

Approach Strength Trade-off
Hand-written HTML Full control, no build step Repetition on large sites
Template engines (Pug, Haml, Nunjucks) Less typing, includes and logic Build step; output still needs review
Markdown Fast for content Limited structure; relies on a renderer
JSX and component frameworks Reusable components Easy to produce div soup without discipline
WYSIWYG editors Easy for non-developers Often noisy, non-semantic output

Whatever produces your HTML, view the final output regularly. The browser reads the output, not your source template.

FAQ

Does HTML formatting affect SEO or page speed?
Not in any meaningful way. Whitespace is a tiny part of the payload, and search engines ignore layout. Semantics and content quality matter far more. Minify for production if you want to save bytes.

Should I use <section> or <div>?
Use <section> when the content is a thematic group that would have a heading. Use <div> when you only need a styling or scripting hook.

Can I have more than one <h1>?
The specification allows it in some contexts, but one <h1> per page gives the clearest outline for screen readers and search engines.

Do I need to close <p> and <li> tags?
The parser can infer them, but explicit closing tags make the structure obvious and avoid subtle bugs. Most style guides require them.

Is ARIA better than semantic HTML?
No. Native elements come with built-in roles and keyboard behaviour. Use ARIA only to fill gaps that native HTML cannot.

Will a formatter change how my page looks?
Usually not, but inline elements are sensitive to whitespace, so a reflow can add or remove a visible space. Review changes around inline content.

Conclusion

Good HTML has two qualities: it is readable to developers and meaningful to machines. Format it consistently, automate that formatting, and choose elements by their meaning rather than their default appearance. Landmarks, proper headings, real buttons and labelled inputs make a page accessible, easier to maintain and clearer to search engines. For a fast clean-up, try the HTML Formatter. To continue, read our guide on CSS formatting and architecture, which pairs naturally with clean markup.

Further reading

Try the tools

Further reading

Related articles