Elements, Tags & Attributes
An HTML element is formed by an opening tag, attributes, inner content, and a closing tag:
<!--
Opening Tag: <p class="intro" id="first-para">
Attributes: class="intro", id="first-para"
Inner Text: Welcome to HTML5 Masterclass
Closing Tag: </p>
-->
<p class="intro" id="first-para">Welcome to HTML5 Masterclass</p>
Most HTML elements contain text or nested child nodes. However, Void Elements cannot contain inner text or child nodes, and therefore do not have closing tags:
| Void Tag | Function | Example Syntax |
|---|---|---|
<img> | Embeds an image graphic. | <img src="logo.png" alt="Logo"> |
<input> | Renders an interactive form input control. | <input type="text" name="user"> |
<br> | Inserts a visual line break inside text. | Line 1<br>Line 2 |
<hr> | Inserts a thematic horizontal rule divider. | <hr> |
<meta> | Defines document metadata. | <meta charset="UTF-8"> |
<link> | Links external CSS stylesheets or icons. | <link rel="stylesheet" href="style.css"> |
Standard Attributes: Key-value pairs written as name="value" (e.g. href="https://example.com" or type="password").
Boolean Attributes: Attributes whose presence implies a true value. You do not need to specify attribute="true":
<!-- Correct Boolean Attribute Examples -->
<input type="text" required disabled>
<option value="in" selected>India</option>
<audio controls autoplay muted></audio>
Global attributes can be applied to ANY valid HTML5 element:
| Global Attribute | Purpose | Example |
|---|---|---|
id | Unique identifier for single element targeted by CSS or JS. | id="header" |
class | Space-separated list of CSS styling classes. | class="btn primary" |
style | Inline CSS style rules. | style="color: red;" |
title | Advisory tooltip text shown on mouse hover. | title="Click to submit" |
hidden | Hides element from visual display and screen readers. | hidden |
tabindex | Controls keyboard focus tab order. | tabindex="0" |
lang | Sets element content language. | lang="en" |
Q1: Can I reuse the same id attribute on multiple elements?
No! An id must be unique within a single HTML document. If you need to target multiple elements with the same styles or JS, use class attributes instead.
Q2: Should I write trailing slashes on void tags like <img />?
In HTML5, trailing slashes like <img /> are optional and ignored by browser parsers. Writing clean <img> is standard HTML5 convention.