Attributes, void elements, and comments
Attributes add details to elements, while some elements have no content to wrap and comments let developers leave notes in source.
✓ What you will learn in this topic
- Explain the purpose of Attributes, void elements, and comments in a real HTML document.
- Choose and apply the elements shown in both examples.
- Recognize the specific mistake described in this lesson.
Concept explanation
An attribute appears in the start tag as a name and value, such as href on a link or alt on an image. Global attributes such as id, class, title, and data-* can apply broadly, but each has a different purpose. A void element has no end tag or child content; img, input, and meta are common examples. HTML comments are not displayed as page content, but they remain visible in downloaded source, so never put secrets in them.
Syntax
Write attributes inside the opening tag and quote values. A comment uses ; a void element stands alone.
Example 1: a small page
id identifies this one element, class can be reused, and the comment is a source note.
<p id=\"welcome\" class=\"notice\">Welcome back.</p>
<!-- The welcome message is shown to every visitor. -->Expected result: Visitors see Welcome back. but not the comment; the attributes do not add visible text.
Example 2: a practical page
The img is a void element. Its attributes provide the file, alternative text, and intrinsic dimensions.
<img src=\"images/compass.jpg\" alt=\"A brass compass on a paper map\" width=\"640\" height=\"420\">Expected result: If the file exists, the browser shows the image at its declared dimensions and exposes its alt text when needed.
Common mistakes
Keep nesting properly ordered and do not write closing tags for void elements such as img, input, and meta.
Best practices
Quote attribute values, use unique ids, prefer classes for reusable styling hooks, and keep comments useful rather than leaving old content in the source.
Practice exercise
Build a short announcement with a class, a unique id, one nested emphasis element, one image with alt text, and a concise source comment.
Lesson summary
- Attributes describe elements, void elements do not wrap content, and comments are public source notes.
- Quote attribute values, use unique ids, prefer classes for reusable styling hooks, and keep comments useful rather than leaving old content in the source.
Knowledge check
Why does an informative image need an alt attribute?
Show the explanation
Alt provides a text alternative when the image cannot be seen and communicates its useful information to assistive technology.
Practical example
html<p id=\"welcome\" class=\"notice\">Welcome back.</p>
<!-- The welcome message is shown to every visitor. -->
Expected result:
Visitors see Welcome back. but not the comment; the attributes do not add visible text.
Hands-on practice task
Build a short announcement with a class, a unique id, one nested emphasis element, one image with alt text, and a concise source comment.
In this section
Other lessons in HTML Elements and Attributes
Broaden your knowledge