HTML tags are the basic building blocks of an HTML document. They define the structure, content, and meaning of a web page by marking elements like headings, paragraphs, images, and links.
What are HTML tags?
Use guided tracks for structured prep, then practice company-specific question sets when you want targeted interview coverage.
The Core Idea
HTML (HyperText Markup Language) is built using tags that tell the browser how to structure and display content. Each tag has a specific role, such as defining a paragraph, a heading, or an image.
When a browser reads an HTML document, it processes tags from top to bottom and builds the page layout through the Document Object Model (DOM) — a structured tree of elements.
<!DOCTYPE html>
<html>
<head>
<title>My Web Page</title>
</head>
<body>
<h1>Welcome!</h1>
<p>This is a simple example.</p>
</body>
</html>
In this example:
- The
<html>tag defines the root of the document. - The
<head>tag contains metadata, scripts, and styles. - The
<body>tag holds the visible page content. - The
tag defines a heading.<h1> - The
tag defines a paragraph of text.<p>
Tag | Purpose | Example | |
|---|---|---|---|
… | Defines a heading (from h1 to h6). | Main Title | |
| Defines a paragraph of text. |
| |
<a>…</a> | Creates a hyperlink to another page or resource. | <a href='https://example.com'>Visit Site</a> | |
<img> | Embeds an image. | <img src='image.jpg' alt='Description'> | |
| Defines unordered and ordered lists. |
| |
| Creates tabular data. |
| |
<form> | Captures user input via fields and buttons. | <form><input type='text'></form> | |
| Generic containers for grouping and styling. | <div class='box'>Content |
Attributes
Tags can include attributes that modify their behavior or provide additional information.
Example:
`html
<a href="https://example.com" target="_blank">Open Example</a>
Here:
- href
defines the link destination. - target='_blank'` opens it in a new browser tab.
Tag Types
- Paired Tags — Have both an opening and closing tag (e.g.,
,<p></p>).<div></div> - Self-closing Tags — Do not require a closing tag (e.g.,
<img>,,<hr>).
Example:
``html<p>This is a paragraph.</p>
<img src='photo.jpg' alt='Example Image'>
Common Mistakes with Tags
- Forgetting to close tags properly, e.g.,
without<p>.</p> - Nesting tags incorrectly, like placing a
tag inside another<p>.<p> - Using generic tags (like
) instead of semantic ones (like<div><header>or<footer>). - Missing inline vs. block element distinctions, which can affect layout and styling.
How Tags Relate to the DOM
When a browser parses HTML, it turns tags into DOM nodes. For example:
`html
<body>
Hello
Hello
<p>World</p>
</body>
`…becomes a tree:
Document
└── body
├── h1 → 'Hello'
└── p → 'World'Each HTML tag corresponds to a node that can be accessed and modified via JavaScript.
Think of HTML tags as labels on boxes — each box tells the browser what’s inside and how to display it. For example, is a big label (a title box), <h1> is a small text box, and <p><img> is a picture frame. Together, these labeled boxes form your web page layout.
- HTML tags define the structure and meaning of webpage content.
- Each tag can have attributes that modify its behavior.
- Tags work together to form the DOM, which browsers use to render pages.
- Proper use of semantic tags improves accessibility and SEO.
- Common tags include
,<h1>,<p><a>,, and<div><img>.