Attributes are the settings you pass to an element inside its opening tag. You've used a few (href, src, alt); this chapter covers the ones that apply to every element, the special boolean kind, and custom data-* attributes for stashing your own data.
Attributes recap
An attribute is a name="value" pair in the opening tag. Values go in quotes; multiple attributes are space-separated:
<a href="/pricing" title="See our pricing" target="_blank">Pricing</a>
Global attributes
These work on any element:
| Attribute | Purpose |
|---|---|
id | A unique identifier (one per page) — for links, CSS, JS |
class | One or more space-separated names — the main CSS/JS hook |
title | Tooltip text on hover |
style | Inline CSS (use sparingly) |
lang | Language of the content (e.g. en, hi) |
hidden | Hides the element |
tabindex | Keyboard focus order |
Boolean attributes
Some attributes are on/off — their mere presence means true, and you don't give them a value:
<input type="text" required disabled><input type="checkbox" checked><button disabled>Can't click me</button><video controls autoplay muted></video><!-- Presence = true. There's no required="false" — just omit it. -->
Custom data-* attributes
When you need to attach your own data to an element (for JavaScript to read), use a data- prefix. It's valid HTML and reads cleanly from JS via the dataset property:
<button data-id="42" data-action="enrol">Enrol</button><script>const btn = document.querySelector("button");btn.dataset.id; // "42"btn.dataset.action; // "enrol" (data-action → dataset.action)</script>
Practice
Build a list of programme cards where each card has a data-slug and data-fee. Add a common class for styling and a unique id on one featured card. Read a card's data-* values from the console.
Key takeaways
- 01Attributes are name="value" settings in the opening tag.
- 02Global attributes (id, class, title, style, lang, hidden, tabindex) work on any element.
- 03id is unique per page; class repeats and is the main styling/JS hook.
- 04Boolean attributes (required, checked, disabled) are true simply by being present.
- 05Custom data-* attributes attach your own data, read in JS via element.dataset.
