Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST

HTML and CSS Document Structure


HTML & CSS: Document Structure

Understand how an HTML document is organized, how the <head> and <body> interact, and where CSS fits into the structure.


HTML Boilerplate

Every page starts with:

  1. the !DOCTYPE
  2. <html> element
  3. metadata in the <head>
  4. visible content in the <body>

Note: Doctype tells the browser to use modern standards, metadata is information about the page, and the body holds what people see.

A boilerplate is a template that you can copy and paste to save time.

Here is a boilerplate for an HTML page:

Template

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Document Title</title>
</head>
<body>
  <header>...</header>
  <main>...</main>
  <footer>...</footer>
</body>
</html>
Try it Yourself »

If you want to read more or get an in-depth understanding, see HTML Basic and HTML Head in the HTML tutorial.


Head metadata

  • <meta charset="UTF-8"> ensures proper character rendering.
  • <meta name="viewport"> makes layouts responsive.
  • <link rel="stylesheet"> attaches external CSS.
  • <script> tags typically load at the end of <body> to avoid blocking rendering.
  • Use <title> for the browser tab and SEO, plus <meta name="description"> for search snippets.


Layout containers

Use semantic wrappers to provide meaning:

  • <header> - branding or navigation at the top.
  • <nav> - primary site navigation links.
  • <main> - unique content for the page (one per page).
  • <section>, <article>, <aside> - subsections with their own headings.
  • <footer> - closing information, links, copyright.

If you want to read more or get an in-depth understanding, see HTML Layout and HTML Semantic Elements in the HTML tutorial.


Organizing CSS

Link your CSS file in the head so styles load before the page renders.

For critical CSS, you can inline minimal styles directly, but prefer external files for maintainability.

Note: "Critical CSS" means tiny pieces of CSS that must load immediately to avoid flashes of unstyled content.

Example

<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">
<link rel="stylesheet" href="css/styles.css">

These tags optimize performance by preconnecting to the font host, loading a web font stylesheet, and linking your main CSS file.

Preconnecting to the font host and loading a web font stylesheet is important because without it, the browser has to wait for the font to load before rendering the page.


Accessibility hooks

  • Add lang on <html> for screen readers.
  • Ensure each <section> has a heading; otherwise use aria-label.
  • Keep heading levels in order (h1 → h2 → h3) to describe hierarchy.

If you want to read more about HTML Accessibility or get an in-depth understanding, go to HTML Accessibility in the HTML tutorial.


Semantic layout demo

This demo shows a complete page shell with header, nav, main sections, and footer using semantic HTML, paired with basic page styles.

Syntax highlights: metadata in the head, semantic containers in the body, and an external stylesheet linked via <link rel="stylesheet">.

Example

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Structured Page</title>
  <link rel="stylesheet" href="styles.css" type="text/css">
</head>
<body>
  <header>
    <h1>Sky Labs</h1>
    <nav>
      <a href="#services">Services</a>
      <a href="#team">Team</a>
      <a href="#contact">Contact</a>
    </nav>
  </header>
  <main>
    <section id="services">
      <h2>Services</h2>
      <p>We craft responsive websites and design systems.</p>
    </section>
    <section id="team">
      <h2>Team</h2>
      <p>Meet the engineers and designers who make it happen.</p>
    </section>
  </main>
  <footer>
    <p>&copy; 2025 Sky Labs. Built with HTML & CSS.</p>
  </footer>
</body>
</html>
body {
  font-family: "Source Sans Pro", sans-serif;
  margin:0;
  background:#f1f5f9;
}
header, footer {
  background:#0b1120;
  color:#fff;
  padding:32px;
}
nav a {
  color:#7dd3fc;
  margin-right:16px;
  text-decoration:none;
}
main {
  max-width:960px;
  margin:0 auto;
  padding:40px 24px;
}
section {
  background:#fff;
  padding:24px;
  border-radius:12px;
  margin-bottom:24px;
}
Try it Yourself »

The CSS sets consistent spacing, colors the header/footer, and constrains the content width for readability while each section remains clearly labeled.


Checklist

  • Document has a valid doctype and language attribute.
  • Head contains viewport, charset, title, and stylesheet links.
  • Body uses semantic wrappers with meaningful headings.
  • CSS is external (unless purposefully inlined).


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.