DEV Community

Cover image for Understanding HTML Tags and Elements: Building Blocks of the Web
Rajesh Kumar
Rajesh Kumar

Posted on

Understanding HTML Tags and Elements: Building Blocks of the Web

Imagine you're looking at a human body. You can see the skin, the hair, the clothes – but underneath it all, there's a skeleton. This skeleton provides the structure, holding everything in place and defining what "this thing" actually is.

In the world of the web, HTML (HyperText Markup Language) is the skeleton of every webpage you see. It's not about the colors or the fonts (that's CSS), or the interactive buttons (that's JavaScript). HTML is purely about structure and meaning.

So, how do we build this digital skeleton? We use tags and elements.

1. What is an HTML Tag? (The Labels)

Think of an HTML tag as a label you put around content to tell the browser what that content is. These labels always come in angle brackets, like <label>.

  • Opening Tag: <p> (This says, "A paragraph starts here!")
  • Closing Tag: </p> (This says, "The paragraph ends here!")

Notice the forward slash (/) in the closing tag? That's how HTML knows you're done with that specific piece of content.

2. What is an HTML Element? (The Labeled Box)

Now, let's combine the label with the actual thing. An HTML element is made up of:

  1. An opening tag
  2. The content inside it
  3. A closing tag

Analogy: If a tag is like a sticky label that says "FRAGILE," then an element is the whole box with the "FRAGILE" label on it, containing the actual fragile item.

Example:

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

Here, <p>is the opening tag, </p> is the closing tag, and "This is a paragraph of text." is the content. The entire thing, from <p> to </p> including the text, is one complete paragraph element.

3. Self-Closing (Void) Elements: The "One-Part" Tags

Not everything needs an opening and a closing. Some HTML tags are like single-item labels; they don't wrap around content, they insert something into the page. These are called self-closing or void elements.

Think of it like: You don't put a label around an image; you insert an image.

Common Examples:

  • <img src="image.jpg" alt="Description of image">: Used to embed an image.
  • <br>: Used to create a line break (like hitting Enter in a text document).
  • <hr>: Used to create a thematic break, often displayed as a horizontal line.

Notice these tags don't have a separate closing tag. They do their job and stand alone.

4. Block-Level vs. Inline Elements: How Content Behaves

HTML elements also behave differently on a page. Think of them as two types of building blocks:

a) Block-Level Elements:

These always start on a new line.

They take up the full available width of the page (like a block taking up an entire row).

They create "sections" or "paragraphs."

Common Block-Level Tags:

<h1> to <h6>: Headings

<p>: Paragraphs

<div>: A general-purpose container (like a big box for organizing other elements)

<section>, <article>, <footer>: Semantic layout elements

b) Inline Elements:

These do not start on a new line.

They only take up as much width as their content needs (like a single word within a sentence).

They are used for formatting small pieces of text within a block.

Common Inline Tags:

<a>: Links

<strong>: Bold text

<em>: Emphasized (italic) text

<span>: A general-purpose container for small pieces of text (like highlighting a few words)

Example:

<p>This is a <strong class="highlight">bold word</strong> in a paragraph.</p>
Enter fullscreen mode Exit fullscreen mode

Here, the <p> is block-level (it takes its own line), but <strong> is inline (it just makes the word "bold word" bold, without breaking the paragraph).

5. Commonly Used HTML Tags for Beginners

As you start building, you'll find yourself using these tags constantly:

<h1> to <h6>: For main titles, subtitles, and section headings. (<h1> is the most important, <h6> the least).

<p>: For regular paragraphs of text.

<a>: For creating links to other pages or websites. (It needs an href attribute: <a href="https://example.com">Link Text</a>)

<img>: For embedding images. (It needs src for the image path and alt for accessibility: <img src="cat.jpg" alt="A cute cat">)

<div>: Your go-to "container" when you just need to group elements for styling or layout.

<span>: Your go-to "container" for small pieces of inline text you want to style differently.

<ul> and <li>: For unordered (bulleted) lists. (<ul> is the list container, <li> is each list item).

<ol> and <li>: For ordered (numbered) lists.

Your First Mission: Inspect the Web!

Now that you know what tags and elements are, open any website (like this blog post!). Right-click anywhere on the page and select "Inspect" or "Inspect Element." You'll see a panel with the HTML code. Try to identify:

  • Opening and closing tags
  • Complete elements (like a whole paragraph)
  • Self-closing tags (like <img>)
  • Block-level vs. inline elements

This hands-on exploration will solidify your understanding of the web's foundational skeleton.

Resources

Top comments (0)