Hey there!
Now that you’ve dipped your toes into HTML, it’s time to dive a little deeper and understand how HTML actually works. If you’ve seen code like <h1>Welcome!</h1>
and wondered what’s a tag vs. an element?, or why does some HTML include words like href
or alt
?, this guide is for you.
New to Web Development?
If you’re brand new to coding or the web, I highly recommend checking out my earlier post:
1. What is HTML? A Beginner’s Guide to the Web’s Building Blocks
We’re going to break down HTML Tags, Elements, and Attributes, explain how they work together, and show you practical examples you can use right away.
What Are HTML Tags, Elements, and Attributes?
At a glance, HTML may look like just a bunch of angled brackets and words. But each piece serves a purpose, and understanding how they fit together will help you write better, cleaner code.
Let’s define the basics:
Term | What It Means |
---|---|
Tag | A keyword wrapped in angle brackets, like <p> or </p> , that tells the browser what to do. |
Element | A complete structure including a start tag, content (optional), and an end tag. |
Attribute | Extra information inside a tag that changes how an element behaves. |
Tags: The Building Blocks
Tags are the keywords in HTML that define how content should be structured or displayed.
There are two main types:
- Opening tag:
<p>
- Closing tag:
</p>
Together, they define the start and end of a paragraph element.
<p>This is a paragraph.</p>
Some tags are self-closing, meaning they don’t wrap content and don’t need an end tag:
h<br> <!-- Line break -->
<img src="photo.jpg" alt="A nice photo"> <!-- Image tag -->
Elements: Tags + Content
An element is the full package. It includes:
- An opening tag
- (Optional) content
- A closing tag
<h1>Hello, world!</h1>
This is an h1 element, and it tells the browser: “Display this text as a large heading.”
Even an empty paragraph is still a valid element:
<p>This is Paragraph</p>
Some more examples of elements:
<a href="https://example.com">Click Here</a> <!-- anchor element -->
<img src="image.jpg" alt="An image"> <!-- image element -->
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul> <!-- list element -->
Attributes: Adding Extra Info to Tags
Attributes provide extra details or customization for elements. They always appear inside the opening tag, and follow this format:
<tag attribute="value">Content</tag>
Common HTML Attributes:
Attribute | What It Does |
---|---|
href | Sets the destination URL for links (<a> ) |
src | Specifies the image source (<img> ) |
alt | Describes an image (important for accessibility) |
title | Adds a tooltip when you hover |
id | Uniquely identifies an element |
class | Applies styling or behavior via CSS/JS |
Example:
<a href="https://google.com" title="Go to Google">Google</a>
This anchor (<a>
) element:
- Uses the
href
attribute to define where the link goes - Uses the
title
attribute to show a tooltip on hover
Another example with an image:
<img src="cat.jpg" alt="A sleepy cat" width="200">
Putting It All Together
Let’s break down this HTML snippet and explain each part:
<a href="https://twitter.com" target="_blank">Follow me on Twitter</a>
Part | Meaning |
---|---|
<a> | Start of anchor element (link) |
href="https://twitter.com" | URL to go to |
target="_blank" | Opens the link in a new tab |
Follow me on Twitter | Text the user sees and clicks |
</a> | End of the link element |
Practice Example: Mini Profile Card
Try this full example in your code editor:
<!DOCTYPE html>
<html>
<head>
<title>Mini Profile</title>
</head>
<body>
<h1>Hi, I'm Alex!</h1>
<img src="alex.jpg" alt="Photo of Alex" width="150">
<p>I’m learning HTML and building awesome things on the web.</p>
<a href="https://github.com" target="_blank">Check out my GitHub</a>
</body>
</html>
Save it as profile.html
, open it in a browser, and admire your work!
HTML5 Semantic Elements Use Tags and Attributes Too
As you get more comfortable with basic elements, HTML5 introduces semantic tags to help clarify the meaning of your content.
Here’s an example of how elements and attributes still apply:
<header>
<h1>My Blog</h1>
<nav>
<a href="/home" class="active">Home</a>
<a href="/about">About</a>
</nav>
</header>
<header>
and<nav>
are semantic tagsclass="active"
is an attribute that could be styled via CSS
Semantic HTML makes your site more accessible and search engine–friendly. Win-win!
Real-Life Analogy: Tags, Elements & Attributes
Think of an HTML element like a product listing on an online store:
- The tag is like the product category (e.g.,
<book>
) - The element is the entire listing (including title, image, and description)
- The attributes are the product details—like price, color, or size
<book title="The Hobbit" author="J.R.R. Tolkien">Fantasy novel</book>
Not real HTML, but you get the idea!
Common Mistakes to Watch Out For
- Forgetting to close tags
Always include the closing tag for elements that need one (</p>
,</a>
, etc.) - Misspelling attributes
src
notscr
,href
notherf
- Using unquoted attribute values
Always wrap values in quotes:href="url"
nothref=url
- Nesting elements incorrectly
Avoid this: htmlCopyEdit<a><p>Wrong way!</p></a> <!-- Not valid HTML -->
Recap: What You Learned
- Tags are HTML keywords inside angle brackets
- Elements = opening tag + content + closing tag
- Attributes provide extra info about elements
- Tags and attributes work together to structure and enhance web pages
Next Steps: Where to Go From Here
Now that you understand how tags, elements, and attributes work together, you’re ready to:
- Explore more HTML elements like
<form>
,<input>
, and<button>
- Learn about CSS to style your elements
- Try building a small website using only HTML
You can also check out:
- HTML Reference by MDN
- W3C’s HTML Validator to check your code
Final Thoughts
HTML might look simple, but there’s real power in understanding how it all fits together. Tags are your tools, elements are your building blocks, and attributes are the special instructions you give your tools.
Once you get comfortable with these basics, building websites becomes way more fun—and a lot less confusing.
Keep practicing, stay curious, and happy coding!
One thought on “2. HTML Tags, Elements, and Attributes: How They Work Together”