Skip to main content

Command Palette

Search for a command to run...

Understanding HTML Tags and Elements

Published
8 min read
Understanding HTML Tags and Elements

What is HTML and Why Do We Use It?

Think of building a website like constructing a house. If a house needs a foundation, walls, and a roof to stand up, a webpage needs HTML to provide its basic structure.

HTML stands for HyperText Markup Language. It's the skeleton or framework of every webpage you see online. HTML provides the structure—it tells the browser, "Here's a heading," "Here's a paragraph," "Here's an image," and so on.

But HTML is just one part of the web development puzzle. Imagine your webpage as a person:

  • HTML is the skeleton (the basic structure)

  • CSS is the skin, clothes, and makeup (the styling and appearance)

  • JavaScript is the movement and personality (the behavior and interactivity)


What is an HTML Tag?

An HTML tag is like a label you put on content to tell the browser what that content is. It's a set of instructions written inside angle brackets (< and >).

Think of tags like you'd label boxes when moving to a new house. If you write "FRAGILE: DISHES" on a box, the movers know to handle it carefully. Similarly, when you use an HTML tag, you're telling the browser how to handle and display that content.

Here's the simplest example:

<p>This is a paragraph.</p>

The <p> tag is your label. It says, "Everything between me and my closing partner should be treated as a paragraph."


The Parts of an HTML Tag

Every basic HTML tag has three parts. Let's break them down using the paragraph example:

<p>Hello, World!</p>

1. Opening Tag: <p>
This is where the tag starts. It uses angle brackets with the tag name inside. The opening tag says, "Start a paragraph here."

2. Content: Hello, World!
This is the actual text or material you want to display. It's the information between the opening and closing tags.

3. Closing Tag: </p>
This is where the tag ends. Notice the forward slash (/) before the tag name. The closing tag says, "End the paragraph here."

Here's another simple example to reinforce this:

<h1>My Website Title</h1>
  • Opening tag: <h1> (starts the largest heading)

  • Content: My Website Title (what you want to display)

  • Closing tag: </h1> (ends the heading)

Block-level vs Inline Elements: Key Differences


What is an HTML Element?

Now here's something important: tag and element are not the same thing, even though people sometimes use the words interchangeably. Let me clarify.

An HTML element is the complete package—it includes the opening tag, the content, AND the closing tag all together.

Think of it this way:

  • A tag is just the label (like <p> or </p>)

  • An element is the tag plus everything inside it (like <p>Hello, World!</p>)

When you write:

<p>Hello, World!</p>

You've created a paragraph element. The element consists of all three parts working together.

Here's another example:

<h2>Chapter 1</h2>

This is an h2 element. It contains:

  • The opening <h2> tag

  • The content "Chapter 1"

  • The closing </h2> tag

Quick Rule: An element = opening tag + content + closing tag.


Self-Closing (Void) Elements

Most HTML elements follow the pattern: opening tag → content → closing tag. But some elements are different—they don't wrap around content, so they don't need a closing tag.

These are called self-closing or void elements. They stand alone and close themselves.

Example 1: The Image Tag

<img src="photo.jpg" alt="My photo">

The <img> tag displays an image. It doesn't wrap around any content because the image itself is the content. There's no closing tag needed.

Example 2: The Line Break Tag

<br>

The <br> tag creates a line break (moves text to the next line). It doesn't wrap content, so it stands alone without a closing tag.

Example 3: The Horizontal Rule Tag

<hr>

The <hr> tag creates a horizontal line across the page. Again, it's a single, self-contained tag.

These self-closing tags are fewer in number, but they're still essential building blocks. Don't worry about memorizing which ones are self-closing—as you practice, you'll naturally learn them.


Block-Level vs. Inline Elements

Different HTML elements behave differently on a page. Some take up the full width of their container and start on a new line. Others fit inline with surrounding content. Understanding this distinction is crucial for organizing your webpage.

Block-Level Elements

Block-level elements are like paragraph breaks or boxes. They:

  • Start on a new line every time

  • Take up the full width available (stretch from left to right)

  • Create an invisible "box" around their content

Real-world analogy: Think of block elements like book chapters. Each chapter starts on a new page and takes up the entire page width until it ends.

Common block-level elements:

  • <p> – Paragraph

  • <h1>, <h2>, <h3> – Headings

  • <div> – A generic container (like a box for organizing content)

Example:

<h1>Welcome to My Site</h1>
<p>This is my first paragraph.</p>
<p>This is my second paragraph.</p>

Each heading and paragraph starts on its own line, taking up the full width. You can't fit them side-by-side without using advanced CSS.

Inline Elements

Inline elements flow with surrounding text. They:

  • Don't start a new line

  • Take up only the space they need

  • Sit alongside other content, just like words in a sentence

Real-world analogy: Inline elements are like words or phrases within a sentence. They flow naturally with the surrounding text.

Common inline elements:

  • <span> – A generic inline container (for highlighting or styling small parts of text)

  • <a> – A hyperlink (clickable link)

  • <img> – An image

Example:

<p>This is a <span>highlighted word</span> in a sentence.</p>
<p>Click <a href="https://example.com">here</a> to visit my site.</p>

In the first example, <span> doesn't force a line break—the word sits right in the middle of the sentence. In the second example, the link sits inline with the text around it.


Commonly Used HTML Tags for Beginners

Now let's explore the basic tags you'll use most often as you start building webpages. We'll keep it simple and practical.

Headings: <h1> through <h3>

Headings organize your content into levels. Think of them like book chapters, sections, and subsections.

<h1>Main Title of My Page</h1>
<h2>Section One</h2>
<h3>Subsection Under Section One</h3>
  • <h1> is the largest (use it once per page for your main title)

  • <h2> is medium (for main sections)

  • <h3> is smaller (for subsections)

<h4>, <h5>, and <h6> exist, but beginners usually work with <h1> through <h3>.

Paragraphs: <p>

Use the paragraph tag to wrap text content.

<p>This is a paragraph of text.</p>
<p>This is another paragraph.</p>

Each <p> element starts on a new line.

Containers: <div>

The <div> tag (short for "division") is a generic container. It doesn't add any special meaning—it's just a box you use to group related content together. Later, you'll use CSS to style it.

<div>
  <h2>About Me</h2>
  <p>I'm learning HTML and web development.</p>
</div>

Inline Container: <span>

Similar to <div>, but inline. Use it when you want to style or highlight a small part of text without breaking it onto a new line.

<p>This word is <span>highlighted</span> in this sentence.</p>

Images: <img>

Display images on your page. Remember, this is a self-closing tag—no closing tag needed.

<img src="photo.jpg" alt="A photo of my cat">

The src tells the browser where to find the image file. The alt describes the image (helpful if the image doesn't load or for screen readers).

Create clickable links to other pages or websites.

<a href="https://www.example.com">Click here to visit Example</a>

The href (hypertext reference) tells the browser where to go when someone clicks the link.


Quick Reference Table

TagTypePurposeExample
<h1> to <h3>BlockHeadings (largest to smaller)<h1>Main Title</h1>
<p>BlockParagraph of text<p>Hello world</p>
<div>BlockGeneric container<div>Content here</div>
<span>InlineInline container for text<span>highlighted</span>
<img>Self-closingDisplay an image<img src="pic.jpg">
<a>InlineHyperlink<a href="url">Link</a>
<br>Self-closingLine break<br>

Encourage Exploration: Use Your Browser's Developer Tools

The best way to learn HTML is by doing and exploring. Your browser has built-in developer tools that let you inspect any webpage and see the HTML behind it.

How to inspect HTML in your browser:

  1. Open any webpage (like Google, Wikipedia, or a news site)

  2. Right-click on any text or element on the page

  3. Select "Inspect" or "Inspect Element"

  4. A panel will open showing the HTML code that makes that page

Try this right now! Inspect a paragraph on any website. You'll see something like:

<p>This is a paragraph.</p>

Or inspect a link:

<a href="https://example.com">Click here</a>

Why do this? Because seeing real HTML from real websites helps you understand how tags work in practice. You'll notice patterns, see how professional developers structure code, and demystify web development.


Final Thoughts: Start Experimenting

Remember, you can't break anything by experimenting with HTML. The worst that happens is your page looks wrong—and that's how you learn!

Here are some ways to practice:

  • Create a simple HTML file on your computer with a few headings, paragraphs, and links

  • Use browser dev tools to inspect websites you love

  • Try changing some HTML and refresh the page to see what happens

  • Don't memorize tags—understand how they work, and you'll naturally remember them