Glossary · Beginner

HTML attribute

An HTML attribute adds a name-value setting or state to an element, such as a link destination or document language.

Published
Last checked

Definition

An attribute appears in an element’s start tag and supplies configuration, state, or a relationship. The href attribute of a link supplies its destination; the lang attribute supplies a language.

Syntax

<a href="/about">About this site</a>

Attribute values are data, not visual decoration. Use only attributes permitted for the element and purpose.

Names, values, and boolean attributes

Most attributes pair a name with a value, such as href="/about" or lang="en". Quote values consistently so spaces and special characters remain unambiguous. Attribute names are ASCII case-insensitive in HTML source, but lowercase is the conventional, readable form.

Boolean attributes work differently: their presence means true. disabled and disabled="" both disable a supported control; writing disabled="false" still leaves the attribute present and therefore true. Remove a boolean attribute when the state is false.

Global and element-specific attributes

Global attributes such as id, class, lang, and hidden can apply to many HTML elements. Other attributes belong to a particular semantic contract: href gives an anchor a destination, alt provides an image alternative, and type changes the expected behavior of some controls.

An attribute that is allowed syntactically may still be wrong for the task. Choose it because its defined behavior or relationship is needed, not because a framework makes it easy to add.

Attributes and DOM properties

After parsing, the browser exposes elements through the Document Object Model. An HTML attribute often supplies an initial value, while a corresponding DOM property may reflect current state. For example, an input’s value attribute describes its default value; the value property changes as a person types.

This distinction matters when debugging forms and scripted interfaces. Inspect both the serialized markup and the live element rather than assuming they always match.

Common mistakes

  • Duplicating the same attribute on one start tag.
  • Inventing unsupported attributes instead of using data-* for private data.
  • Placing untrusted text into an attribute without safe escaping.
  • Using ARIA to contradict native HTML state or behavior.
  • Treating class names as semantics exposed to assistive technology.

Review question

For each attribute, ask what browser behavior, relationship, or machine-readable fact it supplies. If the answer is only “the CSS selector expects it,” the attribute may be implementation plumbing rather than content meaning—and that is acceptable only when it remains valid and maintainable.