HTML Basics

HTML Basic Structure

Master the fundamental building blocks of every HTML document - from DOCTYPE declarations to semantic structure.

What is the basic structure of an HTML document?

Every HTML page starts with <!DOCTYPE html>, then an <html> root element containing a <head> and a <body>. The <head> holds information about the page (charset, viewport, title, CSS links) that isn't shown directly, and the <body> holds everything visible.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Page title shown in the tab</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <!-- visible content -->
  <script src="app.js" defer></script>
</body>
</html>

Try It: Basic HTML Structure

Experiment with the essential HTML structure. This example shows the minimal required elements for a valid HTML document.

Code Editor

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>My Page</title>
</head>
<body>
  <h1>Visible content goes in the body</h1>
  <p>Everything in &lt;head&gt; is information about the page.</p>
</body>
</html>

Preview

DOCTYPE Declaration

The DOCTYPE declaration tells the browser which version of HTML the document is using. For HTML5, it's simple and clean.

HTML5 DOCTYPE:

<!DOCTYPE html>

Why DOCTYPE Matters:

  • Ensures browsers render pages in standards mode
  • Prevents quirks mode rendering issues
  • Must be the first line in your HTML document
  • HTML5 simplified it from complex older versions

The HTML Root Element

The <html> element is the root container for all content on your webpage. It wraps everything except the DOCTYPE.

HTML Element Structure:

<html lang="en">
  <!-- All page content goes here -->
  <head>...</head>
  <body>...</body>
</html>

Language Attribute

Always include the lang attribute for accessibility and SEO:

lang="en"

Common Languages

  • en - English
  • es - Spanish
  • fr - French
  • de - German

The Head Section

The <head> element contains metadata about the document - information that isn't displayed on the page but is essential for browsers and search engines.

Essential Head Elements:

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Page Title - Shows in Browser Tab</title>
  <meta name="description" content="Page description for SEO">
  <link rel="stylesheet" href="styles.css">
</head>

Required Elements:

  • charset

    Character encoding (UTF-8)

  • viewport

    Mobile responsiveness

  • title

    Page title (browser tab)

Optional Elements:

  • description

    SEO meta description

  • link

    External resources (CSS)

  • script

    JavaScript files

The Body Element

The <body> element contains all the visible content of your webpage - everything users see and interact with.

Semantic Body Structure:

<body>
  <header>
    <h1>Site Title</h1>
    <nav>Navigation</nav>
  </header>
  
  <main>
    <section>Main Content</section>
    <aside>Sidebar</aside>
  </main>
  
  <footer>
    Footer Information
  </footer>
</body>

Semantic Elements in Body:

  • header

    Site/page header

  • nav

    Navigation links

  • main

    Primary content

  • section

    Content sections

  • aside

    Sidebar content

  • footer

    Page footer

Best Practices & Tips

Do These:

  • Always start with DOCTYPE html
  • Include charset and viewport meta tags
  • Use semantic HTML5 elements
  • Write descriptive page titles
  • Validate your HTML regularly

Avoid These:

  • Missing DOCTYPE declaration
  • Multiple main elements per page
  • Using div for everything
  • Empty or generic page titles
  • Inline styles in production

HTML Basic Structure — step by step

1 / 4
1

Start with the DOCTYPE declaration

Every HTML5 document begins with DOCTYPE html. This tells browsers to use standards mode, ensuring consistent rendering across different browsers.

1 of 4
🔍The Viewport Meta Tag
Always include meta name='viewport' content='width=device-width, initial-scale=1' in the head. Without it, mobile browsers zoom out to show a desktop-width page, breaking your responsive layout.

🎯 Quick Check

Where should you place the page title and stylesheet links in an HTML document?

HTML Document Structure: summary

Key points

  • <!DOCTYPE html> tells the browser to use standards mode instead of the old quirks mode.
  • lang on <html> helps screen readers pronounce text and helps search engines.
  • <meta charset="UTF-8"> should be the first item in <head> so all characters display correctly.
  • The viewport meta tag is essential for pages to display properly on phones.

Frequently asked questions

›What is the purpose of <!DOCTYPE html>?

It tells the browser the page is modern HTML and should be rendered in standards mode. Without it, browsers fall back to quirks mode, which imitates old, inconsistent behaviour.

›What goes in the head vs the body?

The <head> contains metadata: title, charset, viewport, description, stylesheets and scripts. The <body> contains everything the user sees.