Library / Frontend
HTML Head and Body Layout Essentials
This lesson teaches you how to structure a complete HTML page, focusing on the essential elements within the `<head>` and `<body>` tags for proper document setup and content organization.
Want your own version of this lesson, tailored to your level?
⑂ Generate my versionWhat this lesson teaches, step by step
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Awesome Page</title>
</head>
<body>
<!-- Page content will go here -->
</body>
</html>We begin with the fundamental structure of any HTML document. The `<!DOCTYPE html>` declaration ensures the browser renders the page in standard mode, and the `<html>` tag serves as the root. Inside, `<head>` holds metadata about the page, while `<body>` contains all the visible content. The `<title>` tag sets the text displayed in the browser's tab.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Awesome Page</title>
</head>
<body>
<!-- Page content will go here -->
</body>
</html>Next, we add crucial `meta` tags to the `<head>`. The `charset="UTF-8"` declaration ensures correct display of various characters, preventing garbled text. The `viewport` meta tag is essential for responsive design, instructing browsers how to control the page's dimensions and scaling on different devices.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Awesome Page</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<!-- Page content will go here -->
</body>
</html>To style our page, we use the `<link>` tag within the `<head>` to connect an external stylesheet. The `rel="stylesheet"` attribute specifies the relationship to the document, and `href="style.css"` points to the CSS file's location. Placing it in the head ensures styles are loaded before the page content, preventing a flash of unstyled content.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Awesome Page</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<h1>Welcome to My Page!</h1>
</header>
<main>
<section>
<h2>About This Lesson</h2>
<p>This section provides an overview of the HTML layout.</p>
</section>
</main>
</body>
</html>Inside the `<body>`, we start structuring the visible content using semantic HTML5 tags. The `<header>` element is ideal for introductory content, often containing navigation or main headings like our `<h1>`. The `<main>` element encapsulates the dominant content of the `<body>`, and here we use a `<section>` to group related content with an `<h2>` and a `<p>` tag.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Awesome Page</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<h1>Welcome to My Page!</h1>
</header>
<main>
<section>
<h2>About This Lesson</h2>
<p>This section provides an overview of the HTML layout.</p>
</section>
</main>
<footer>
<p>© 2023 My Awesome Page. All rights reserved.</p>
</footer>
</body>
</html>To complete our basic page layout, we add a `<footer>` element. This semantic tag is typically used for boilerplate content such as copyright information, author details, or related links. Placing it at the end of the `<body>` visually and semantically concludes the main content, making the document structure clear and accessible.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Awesome Page</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<h1>Welcome to My Page!</h1>
</header>
<main>
<section>
<h2>About This Lesson</h2>
<p>This section provides an overview of the HTML layout.</p>
</section>
</main>
<footer>
<p>© 2023 My Awesome Page. All rights reserved.</p>
</footer>
<script src="script.js"></script>
</body>
</html>Finally, we integrate a JavaScript file using the `<script>` tag. While `<script>` tags can go in the `<head>` with the `defer` attribute, placing them just before the closing `</body>` tag is a common practice. This ensures all HTML content is parsed and available in the DOM before the JavaScript attempts to interact with it, improving performance and avoiding errors.
Comments
- codewithfaith00121d ago
Nice