Skip to main content
Albero Academy

HTML Attributes

Configure elements with attributes — the global ones every element shares, boolean attributes, and custom data-* attributes.

Chapter 5HTML13 min readBeginner friendly
  • #HTML
  • #Attributes

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:

attributes
<a href="/pricing" title="See our pricing" target="_blank">Pricing</a>

Global attributes

These work on any element:

AttributePurpose
idA unique identifier (one per page) — for links, CSS, JS
classOne or more space-separated names — the main CSS/JS hook
titleTooltip text on hover
styleInline CSS (use sparingly)
langLanguage of the content (e.g. en, hi)
hiddenHides the element
tabindexKeyboard focus order

Boolean attributes

Some attributes are on/off — their mere presence means true, and you don't give them a value:

boolean attributes
<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:

data attributes
<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

  1. 01Attributes are name="value" settings in the opening tag.
  2. 02Global attributes (id, class, title, style, lang, hidden, tabindex) work on any element.
  3. 03id is unique per page; class repeats and is the main styling/JS hook.
  4. 04Boolean attributes (required, checked, disabled) are true simply by being present.
  5. 05Custom data-* attributes attach your own data, read in JS via element.dataset.