Attributes provide extra information and behavior to HTML elements, turning static text into interactive controls.
What is it?
HTML attributes are modifier words placed inside the opening tag of an element, specified as name="value" pairs.
Why is it used?
Attributes configure elements: they specify image sources, link destinations, element identifiers (`id`), styling hooks (`class`), and accessible descriptions.
Detailed Explanation
Global attributes can be used on any HTML element. The most important global attributes include:
• `id`: A unique identifier across the entire document. No two elements should share the same id.
• `class`: A reusable identifier used to target elements with CSS and JavaScript.
• `title`: Provides extra tooltip information on hover.
• `hidden`: Hides the element from the display.
• `data-*`: Custom data attributes for storing arbitrary application data.
<article id="featured-post" class="card blog-entry" data-category="tutorials">
<h2 title="Click to view chapter notes">Understanding HTML Attributes</h2>
<p class="lead-text">Attributes configure how elements behave and look.</p>
<a href="/tutorial/css" target="_blank" class="btn btn-primary">Next: CSS Tutorial →</a>
</article>
Explanation of the Code
• `id="featured-post"` is unique to this article.
• `class="card blog-entry"` allows multiple CSS classes separated by spaces.
• `data-category="tutorials"` stores metadata for JavaScript scripts.
• `target="_blank"` on the link instructs the browser to open the URL in a new tab.
Expected Output
An article element with title tooltip on hover and a styled link opening in a new tab.
Real-World Application
Modern frontend frameworks (React, Vue, Angular) and CSS architectures (Tailwind, Bootstrap) rely heavily on HTML attributes and data-* tags.
Common Mistakes to Avoid
• Duplicating the same `id` on multiple elements in the same page.
• Forgetting quotes around attribute values (e.g. `class=btn active` will treat `active` as an invalid separate attribute).
Quick Practice Challenge
Write an anchor tag pointing to https://google.com that opens in a new tab, has an id of 'search-link', and a tooltip 'Visit Google'.
Hint: Use `<a href="..." target="_blank" id="search-link" title="Visit Google">...</a>`.
Key Takeaways
• Attributes always go inside opening tags as `name="value"`.
• `id` must be unique on the page; `class` can be reused on multiple elements.
• Use `data-*` attributes to store custom data for JavaScript.