Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 6 of 10
HTML Elements, Tags & Attributes
To write HTML effectively, you need to understand the three fundamental building blocks: elements, tags, and attributes. An element is the complete unit (opening tag + content + closing tag). Tags are the markers that define where an element starts and ends. Attributes provide additional information about elements, like the destination of a link or the source of an image. Understanding these concepts thoroughly will make every other HTML topic easier to learn, because everything in HTML is built from these same building blocks.
Elements, Tags, and Attributes Explained
Let's break down the anatomy of HTML:
An HTML ELEMENT is the complete unit. For example: <p>Hello World</p> is a paragraph element. It consists of the opening tag (<p>), the content (Hello World), and the closing tag (</p>).
A TAG is the markup that wraps the content. The opening tag is <p> and the closing tag is </p>. Notice the closing tag has a forward slash (/) before the tag name.
Some elements are 'void' or 'self-closing' - they have no content and no closing tag. Examples include <br> (line break), <hr> (horizontal rule), <img> (image), <input> (form input), and <meta> (metadata). You simply write the opening tag: <img src='photo.jpg' alt='photo'>.
ATTRIBUTES are additional information added to the opening tag. They always follow the format: name="value". For example, in <a href="https://google.com">, the attribute name is 'href' and the attribute value is 'https://google.com'. Some attributes are Boolean - they don't need a value. For example, <input required> means the input is required; just having the word 'required' is enough.
HTML is case-insensitive (both <P> and <p> work), but the convention is to always use lowercase tags. In XHTML, lowercase was mandatory, and this convention carried over to modern HTML development.
Every HTML element follows this structure
Global Attributes (Available on Every Element)
- id - A unique identifier for the element. Used by CSS (#myId) and JavaScript (getElementById). Must be unique on the page.
- class - Assigns one or more CSS class names. Used for styling groups of elements. Example: class="card active"
- style - Inline CSS styling directly on the element. Example: style="color: red; font-size: 18px;" (use sparingly, prefer external CSS)
- title - Shows a tooltip when the user hovers over the element. Also used by screen readers for additional context.
- lang - Specifies the language of the element's content. Example: lang="hi" for Hindi text within an English page.
- dir - Sets text direction: 'ltr' (left-to-right) or 'rtl' (right-to-left, for Arabic/Hebrew).
- hidden - Boolean attribute. Hides the element from the page. Example: <p hidden>Secret text</p>
- tabindex - Controls keyboard Tab order. tabindex="0" adds to natural order. tabindex="-1" allows JS focus only.
- contenteditable - Makes any element editable by the user. Example: <div contenteditable="true">Edit me!</div>
- draggable - Enables drag-and-drop on the element. Example: draggable="true"
- data-* - Custom data attributes for storing extra information. Example: data-user-id="42". Access in JS: element.dataset.userId
- spellcheck - Enables/disables spell checking. Example: spellcheck="true" on <textarea>.
Pro Tip
In VS Code, type '!' and press Tab to auto-generate the complete HTML5 boilerplate structure. This shortcut uses Emmet - a built-in VS Code feature that also lets you create complex HTML with shortcuts like 'ul>li*5' (creates a list with 5 items). Learning Emmet shortcuts can 10x your HTML writing speed.
Element Anatomy Example
<!DOCTYPE html>
<html lang="en">
<body>
<!-- Regular element: opening tag + content + closing tag -->
<h1>This is an element</h1>
<!-- ^opening tag^ ^content^ ^closing tag^ -->
<!-- Element with attributes -->
<a href="https://google.com" target="_blank" rel="noopener noreferrer">
Click Me
</a>
<!-- href, target, rel are all ATTRIBUTES -->
<!-- Void (self-closing) elements - no closing tag needed -->
<br>
<hr>
<img src="https://picsum.photos/200/100" alt="Sample image" width="200">
<input type="text" placeholder="Type here..." required>
<meta charset="UTF-8">
<!-- Boolean attributes - no value needed -->
<input type="text" required disabled readonly>
<!-- required, disabled, readonly are boolean attributes -->
<!-- data-* custom attributes -->
<div data-product-id="42" data-price="29.99" data-category="electronics">
Product Card
</div>
<!-- contenteditable - try editing this text! -->
<p contenteditable="true" style="border: 1px dashed gray; padding: 8px;">
Click this paragraph and type to edit it!
</p>
</body>
</html>Try It Yourself: Elements & Attributes
Common Mistakes with Tags & Attributes
- Forgetting to close tags - <p>Text without closing leads to broken layouts. Always write </p>, </div>, </a>, etc.
- Improper nesting - <b><i>text</b></i> is WRONG. Correct: <b><i>text</i></b>. Tags must close in reverse order (like Russian dolls).
- Missing quotes on attribute values - <a href=https://google.com> may break. Always use quotes: href="https://google.com"
- Using id more than once - Each id must be unique on the page. For styling multiple elements, use class instead.
- Confusing void elements - <img>, <br>, <hr>, <input>, <meta> NEVER have a closing tag. Writing </img> or </br> is incorrect.
- Using style attribute excessively - Inline styles are hard to maintain. Prefer external CSS files for styling.
Quick Quiz
Key Takeaways
- To write HTML effectively, you need to understand the three fundamental building blocks: elements, tags, and attributes.
- id - A unique identifier for the element. Used by CSS (#myId) and JavaScript (getElementById). Must be unique on the page.
- class - Assigns one or more CSS class names. Used for styling groups of elements. Example: class="card active"
- style - Inline CSS styling directly on the element. Example: style="color: red; font-size: 18px;" (use sparingly, prefer external CSS)
Finished this lesson?