Lesson 2.1: HTML Document Structure

Building Your First Complete Webpage - The Blueprint of Every Website

🕐 60 minutes 🟢 Beginner

🚀 Welcome to Your First HTML Document!

Congratulations! You've made it to the fun part - now we're going to create your very first complete HTML webpage. Don't worry if you've never written code before. We'll go step by step, and by the end of this lesson, you'll have a real webpage that you can open in any browser.

Every HTML webpage follows the same basic structure, just like every house has a foundation, walls, and a roof. We're going to build that structure together.

🏗️ The Anatomy of an HTML Document

Every HTML document has the same basic parts. Think of it like a sandwich - there's the bread (DOCTYPE), the filling (content), and more bread (closing tags). Here's what we'll be building:

HTML Document Structure

1. DOCTYPE Declaration

Tells the browser: "This is an HTML5 document"
2. HTML Element
...
Contains everything in your webpage
3. HEAD Section
...
Information about the page (title, metadata)
4. BODY Section
...
The actual content that appears in the browser

💻 Let's Build Your First Webpage!

Ready to code? Here's your step-by-step guide to creating your first HTML document:

Open Your Text Editor

Open any text editor you have available. This could be:

  • Notepad (Windows)
  • TextEdit (Mac) - make sure it's in plain text mode
  • Any code editor like Visual Studio Code, Sublime Text, or Atom

Don't worry about fancy features - even basic Notepad works perfectly for learning HTML!

Start with DOCTYPE

Type this at the very top of your file:

This tells the browser to expect HTML5 code. It's like saying "Hey browser, I'm going to write HTML now!"

Add the HTML Wrapper

Now add the opening and closing html tags:

Everything in your webpage will go between these two tags.

Create the HEAD Section

Add the head section with a title:

The head section contains information about the page. The title will show up in the browser tab!

Add the BODY Section

Now let's add some visible content:

The body contains everything that will actually appear on the webpage.

Save Your File

Save your file as myfirstpage.html. Make sure to use the .html extension!

⚠️ Important!

When saving, make sure you change "Save as type" to "All files" (not .txt), and always use the .html extension at the end of your filename.

Open in Your Browser

Double-click your saved file. It should open in your default web browser and display "Hello, World!" as a heading and your welcome message below it.

🎉 Expected Result:

Your browser should display a very simple webpage with:

  • A heading that says "Hello, World!"
  • A paragraph that says "Welcome to my very first webpage!"
  • The page title in the browser tab

🔍 Understanding What We Built

Let's break down exactly what each part does and why it's important:

Complete Code Explanation

The DOCTYPE Declaration

This tells the browser to use HTML5 rules. Without this, the browser might guess incorrectly about how to display your page.

The HTML Element

This is the root element that contains everything. Everything else goes inside these tags.

The HEAD Section

Contains metadata (data about the page). The title appears in browser tabs and search results.

The BODY Section

Contains all the visible content.

creates a heading,

creates a paragraph.

🎯 Try It Yourself!

Now that you understand the structure, try creating a few variations:

Challenge 1: Change the Content

Replace "Hello, World!" with your name and change the paragraph to something about yourself.

Challenge 2: Add Another Paragraph

Add another

element with a different message after the first one.

Challenge 3: Change the Title

Update the to something descriptive of your webpage.</p> <p>Remember to save your file and refresh the browser to see your changes!</p> <script> // HTML Validator Functionality document.getElementById('validate-btn').addEventListener('click', function() { const htmlInput = document.getElementById('html-input').value; const resultDiv = document.getElementById('validation-result'); // Basic validation checks let errors = []; let warnings = []; // Check for DOCTYPE if (!htmlInput.includes('<!DOCTYPE html>') && !htmlInput.includes('<!doctype html>')) { errors.push('Missing DOCTYPE declaration'); } // Check for html tags if (!htmlInput.includes('<html>') || !htmlInput.includes('</html>')) { errors.push('Missing opening and/or closing <html> tags'); } // Check for head tags if (!htmlInput.includes('<head>') || !htmlInput.includes('</head>')) { warnings.push('Missing <head> section (recommended)'); } // Check for body tags if (!htmlInput.includes('<body>') || !htmlInput.includes('</body>')) { errors.push('Missing opening and/or closing <body> tags'); } // Check for title in head if (htmlInput.includes('<head>') && !htmlInput.includes('<title>')) { warnings.push('Consider adding a <title> in the <head> section'); } // Display results if (errors.length === 0 && warnings.length === 0) { resultDiv.className = 'validation-result validation-success'; resultDiv.innerHTML = '<h4>✅ Valid HTML Structure!</h4><p>Your HTML code follows proper structure. Great job!</p>'; } else { resultDiv.className = 'validation-result validation-error'; let resultHTML = '<h4>⚠️ Issues Found:</h4><ul>'; errors.forEach(error => { resultHTML += `<li><strong>Error:</strong> ${error}</li>`; }); warnings.forEach(warning => { resultHTML += `<li><strong>Warning:</strong> ${warning}</li>`; }); resultHTML += '</ul>'; resultDiv.innerHTML = resultHTML; } }); document.getElementById('clear-btn').addEventListener('click', function() { document.getElementById('html-input').value = ''; document.getElementById('validation-result').innerHTML = '<p>Click "Validate" to check your HTML code!</p>'; document.getElementById('validation-result').className = 'validation-result'; }); // Live HTML Preview Functionality document.getElementById('preview-btn').addEventListener('click', function() { const htmlCode = document.getElementById('preview-input').value; const iframe = document.getElementById('html-preview'); // Create a data URL for the iframe const dataUrl = 'data:text/html;charset=utf-8,' + encodeURIComponent(htmlCode); iframe.src = dataUrl; }); document.getElementById('reset-preview-btn').addEventListener('click', function() { document.getElementById('preview-input').value = `<!DOCTYPE html> <html> <head> <title>Live Preview

Welcome to HTML!

This is a bold paragraph with some italic text.

  • First item
  • Second item
  • Third item
`; document.getElementById('html-preview').src = ''; }); // Quiz Functionality document.getElementById('check-quiz-btn').addEventListener('click', function() { const resultDiv = document.getElementById('quiz-result'); let score = 0; let totalQuestions = 3; // Check answers const q1Answer = document.querySelector('input[name="q1"]:checked'); const q2Answer = document.querySelector('input[name="q2"]:checked'); const q3Answer = document.querySelector('input[name="q3"]:checked'); if (q1Answer && q1Answer.value === 'b') score++; if (q2Answer && q2Answer.value === 'b') score++; if (q3Answer && q3Answer.value === 'b') score++; // Display results let resultHTML = `

Quiz Results: ${score}/${totalQuestions}

`; if (score === totalQuestions) { resultHTML += '

🎉 Perfect! You have excellent understanding of HTML structure.

'; resultDiv.className = 'quiz-result validation-success'; } else if (score >= totalQuestions * 0.7) { resultHTML += '

👍 Good job! You understand most HTML structure concepts.

'; resultDiv.className = 'quiz-result validation-success'; } else { resultHTML += '

📚 Keep learning! Review the lesson content and try again.

'; resultDiv.className = 'quiz-result validation-error'; } // Show correct answers resultHTML += '
Correct Answers:
'; resultHTML += '
    '; resultHTML += '
  • Q1: The HTML version being used (DOCTYPE)
  • '; resultHTML += '
  • Q2: contains the visible content
  • '; resultHTML += '
  • Q3: The content appears in browser tabs</li>'; resultHTML += '</ul>'; resultDiv.innerHTML = resultHTML; }); // Initialize preview on page load document.addEventListener('DOMContentLoaded', function() { document.getElementById('preview-btn').click(); }); </script> <div class="lesson-section"> <h2>🛠️ Interactive HTML Validator</h2> <p>Test your HTML knowledge with this interactive validator! Type some HTML code below and click "Validate" to check if it's properly structured.</p> <div class="interactive-tool"> <div class="code-input-section"> <label for="html-input">Your HTML Code:</label> <textarea id="html-input" class="code-example" readonly placeholder="Type your HTML code here..."><!DOCTYPE html> <html> <head> <title>My Test Page

    Hello World

    This is a test paragraph.

Click "Validate" to check your HTML code!

👀 Live HTML Preview

See your HTML code come to life! Type or paste HTML code in the editor and click "Preview" to see how it renders in a browser.

Live Preview:

❓ HTML Structure Quiz

Test your understanding of HTML document structure with this quick quiz!

Question 1: What does DOCTYPE declare?

Question 2: Which element contains the visible content of a webpage?

Question 3: What appears in the browser tab?