HTML
HTML Document Structure
The foundation every web page needs - understanding how HTML documents are organized from top to bottom
Every HTML document follows the same basic structure. Think of it like building a house - you need a solid foundation, walls, and a roof before you can add furniture and decorations. HTML works the same way.The Anatomy of an HTML Document
Every HTML document starts with a **document type declaration** - this tells the browser which version of HTML you're using. Modern websites use HTML5, which has the simplest declaration possible.<!-- Alex's portfolio homepage structure -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Alex Johnson - Web Developer Portfolio</title>
</head>
<body>
<h1>Welcome to Alex's Portfolio</h1>
<p>I'm a passionate web developer creating amazing digital experiences.</p>
</body>
</html>Welcome to Alex's Portfolio
I'm a passionate web developer creating amazing digital experiences.
What just happened?
The browser ignored everything in the head section and only displayed the body content. The h1 appeared large and bold, while the paragraph used normal text styling. Try this: Change the title text and refresh - notice it appears in the browser tab, not on the page.
The Document Head - Invisible but Essential
The **head section** contains **metadata** - information about your webpage that browsers and search engines need, but visitors never see directly. Think of it like the ingredients list on food packaging - crucial information that's there when you need it. Here's what goes in the head:Character Encoding
Tells the browser how to interpret text characters
Page Title
Shows in browser tabs and search results
Viewport Settings
Controls how the page displays on mobile devices
External Resources
Links to CSS stylesheets and other files
<!-- Complete head section for Alex's portfolio -->
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Alex Johnson's web development portfolio showcasing modern websites and applications">
<meta name="keywords" content="web developer, portfolio, HTML, CSS, JavaScript">
<meta name="author" content="Alex Johnson">
<title>Alex Johnson - Web Developer Portfolio</title>
</head>What just happened?
None of the meta tags appeared on the page, but they're working behind the scenes. The title shows in the browser tab, the description helps with Google search results, and the viewport meta tag ensures the site works on phones. Try this: Add your own description and see how it might appear in search results.
Understanding Meta Tags
**Meta tags** provide structured information about your webpage. The name "meta" means "about" - these tags are about your page rather than part of its content. The most important meta tags for any website:| Meta Tag | Purpose | Example |
|---|---|---|
| charset | Character encoding | UTF-8 supports all languages |
| viewport | Mobile display control | Makes sites mobile-friendly |
| description | Search engine snippet | Appears in Google results |
| keywords | Search optimization | Less important than before |
| author | Content creator | Your name or company |
The Body Section - Where Content Lives
The **body element** contains everything visitors actually see and interact with. Every piece of visible content - text, images, links, forms - goes inside the body tags. Alex wants to create a complete homepage structure:<!-- Full homepage structure for Alex's portfolio -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Alex Johnson - Web Developer</title>
</head>
<body>
<h1>Alex Johnson</h1>
<h2>Web Developer & Designer</h2>
<p>Creating beautiful, functional websites that users love.</p>
<p>View my projects below or get in touch to discuss your next project.</p>
</body>
</html>Alex Johnson
Web Developer & Designer
Creating beautiful, functional websites that users love.
View my projects below or get in touch to discuss your next project.
What just happened?
The browser displayed all body content with default styling - h1 tags appear largest and boldest, h2 tags smaller, and paragraphs in normal text with automatic spacing. Notice how the browser added margins between elements automatically. Try this: Add another heading or paragraph and see how it affects the layout.
Language and Accessibility
The `lang` attribute on the HTML element tells screen readers and translation tools what language your content is written in. This helps people with visual impairments and makes your site more accessible globally.Best Practice
Always include the lang attribute. Use "en" for English, "es" for Spanish, "fr" for French. Search engines use this information to serve your content to the right audiences.
<!-- Alex's portfolio with proper language declaration -->
<html lang="en"> <!-- English -->
<html lang="es"> <!-- Spanish -->
<html lang="fr"> <!-- French -->
<html lang="de"> <!-- German -->
<html lang="ja"> <!-- Japanese -->Common Structure Mistakes
New developers often make these structural errors that break their pages:Avoid These Mistakes
• Missing DOCTYPE - causes unpredictable browser behavior
• Multiple head or body sections - HTML only allows one of each
• Putting visible content in the head - it won't display
• Forgetting to close html, head, or body tags
Quick Knowledge Check
1. What character encoding should modern HTML documents use?
2. Where should the page title element be placed?
3. What does <!DOCTYPE html> declare?
Up Next: HTML Elements and Tags
Now that you understand document structure, we'll dive into the building blocks of HTML - elements and tags. Learn how to create headings, paragraphs, links, and more as Alex builds out their portfolio content.