HTML Tutorial
The Words of a Web Language
If the basic structure of an HTML document is the sentence structure (subject-verb-object), then **HTML elements** are the **nouns and verbs** themselves. They are the individual commands that tell a web browser what each piece of content *is*. When you look at a webpage, you see a cohesive page. But a browser sees a collection of elements: "This is a heading, that is a paragraph, this is an image, that is a link." Mastering HTML elements and tags is learning the vocabulary you need to "speak" to a browser and build your vision. This guide will demystify the syntax of HTML, clarify the critical difference between *tags* and *elements*, and introduce you to the essential concepts you need to start writing meaningful HTML.
Anatomy of an HTML Element
Let's break down a more complex element to understand all its parts.
1<a href="https://www.example.com" target="_blank" class="link">Visit Example</a>Component Parts
Opening Tag- <aAttribute Names- href, target, and classAttribute Values- "https://www.example.com", "_blank", "link"Content- Visit ExampleClosing Tag- </a>
HTML Attributes: Adding Extra Information
**Attributes** provide additional information about an element. They are always specified in the opening tag and usually come in name/value pairs: `name="value"`. * **Purpose:** Attributes configure elements or adjust their behavior. For example, the `href` attribute specifies the URL for a link (`<a>`), and the `src` attribute specifies the source file for an image (`<img>`). * **Syntax:** The attribute name is followed by an equals sign (`=`) and the attribute value is enclosed in quotation marks (`" "`). * **Multiple Attributes:** An element can have multiple attributes, separated by spaces. Order does not matter.
Common Attributes
id- Specifies a unique identifier for an element. Each `id` must be unique on a single page.class- Specifies one or more class names for an element. Classes are used to style elements with CSS and can be reused on multiple elements.src- Specifies the source (path) for media elements like `<img>`, `<audio>`, and `<video>`.href- Specifies the URL for linked resources, primarily used with the `<a>` (anchor) tag.alt- Specifies alternative text for an image. This is crucial for accessibility (screen readers) and SEO. It displays if the image fails to load.style- Used for inline CSS styling (e.g., `style="color: red;"`). Using a separate CSS file is generally preferred.
Empty Elements (Void Elements)
Some elements have no content. These are called **empty elements** or **void elements**. Because they don't wrap any content, they only have an opening tag and **must not** have a closing tag. The most common example is the `<img>` (image) tag. It embeds an image based on its attributes alone.
1<!-- Correct usage of empty elements -->
2<img src="cat.jpg" alt="A fluffy cat">
3<br> <!-- Line Break -->
4<hr> <!-- Horizontal Rule -->
5<input type="text">Common Mistakes
Incorrect Syntax- <img src="cat.jpg" alt="A fluffy cat"></img> <!-- This is wrong and will cause errors -->
Nesting Elements: Putting Elements Inside Other Elements
You can place elements inside other elements; this is called **nesting**. It's how you build a complex, hierarchical structure for your document.
1<p>My favorite animal is the <strong>capybara</strong>.</p>Rules for Proper Nesting
First In, Last Out- Elements must be closed in the reverse order that they were opened. The innermost element must be closed before the outer element is closed.Correct Nesting- <p>This is <strong>very <em>important</em></strong> text.</p> <!-- The <em> (emphasis) is closed first, then <strong>, then <p> -->Incorrect Nesting- <p>This is <strong>very <em>important</strong> text</em>.</p> <!-- The <strong> tag is closed before the <em> tag inside it. This is wrong. -->
Block-Level vs. Inline Elements
This is a fundamental concept in HTML layout. Understanding the difference is key to controlling how your page flows.
Element Types
Block-Level Elements- A **block-level element** always starts on a new line and takes up the full width available (stretching out to the left and right as far as it can). * **Behavior:** They form a visible "block" on the page, stacking vertically. * **Common Examples:** `<div>`, `<p>`, `<h1>` through `<h6>`, `<ul>`, `<ol>`, `<li>`, `<section>`, `<article>`, `<nav>` **Visualization:** ``` [--- Heading (h1) ---] [--- Paragraph (p) ---] [--- Another Paragraph (p) ---] ``` Each block-level element stacks on top of the next.Inline Elements- An **inline element** does not start on a new line and only takes up as much width as necessary. * **Behavior:** They flow within the text content, wrapping with other inline elements. * **Common Examples:** `<a>`, `<strong>`, `<em>`, `<img>`, `<span>`, `<br>` **Visualization:** ``` This is a paragraph with a [link] and some <strong>bold text</strong> all flowing together on one line. ```
Putting It All Together: A Practical Example
Let's create a small section of a webpage using various elements, nesting, and attributes.
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <title>Element Example</title>
6</head>
7<body>
8 <!-- A block-level heading element -->
9 <h1 id="main-title">My Pets</h1>
10
11 <!-- A block-level paragraph element containing inline elements -->
12 <p>I have two pets. A very <strong>lazy cat</strong> named Whiskers and a <em>hyperactive</em> dog named <a href="spot.html">Spot</a>.</p>
13
14 <!-- A block-level div (content division) element acting as a container -->
15 <div class="pet-profile">
16 <!-- A block-level heading, nested inside the div -->
17 <h2>Whiskers the Cat</h2>
18
19 <!-- An empty void element (img), nested inside the div -->
20 <img src="whiskers.jpg" alt="A gray tabby cat sleeping on a sofa" class="pet-photo">
21
22 <!-- Another paragraph, nested inside the div -->
23 <p>Whiskers enjoys napping and ignoring everyone.</p>
24 </div>
25</body>
26</html>Conclusion: You've Learned the Vocabulary
You are no longer just looking at a webpage; you are starting to see the underlying language. **HTML elements**, defined by their **tags**, are the literal building blocks you use to construct any web page. You now understand how to add extra information with **attributes**, how to combine elements through **nesting**, and how they flow on the page as either **block** or **inline** elements. This knowledge is the functional core of HTML. With it, you can now start creating rich, structured content. The next step is to learn the specific "words" – the most common and important HTML tags, which we will cover in the next module: **HTML Text Formatting**.